<?php

use app\libraries\FileUpload;

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Promotion extends MY_BackendController
{
    //    protected $authorizedRoutes = ['index', 'add', 'edit', 'delete'];

    public function __construct()
    {
        parent::__construct();
        $this->load->model('Promotion_model');
    }

    public function index()
    {
        $t = $this->input->get('t');

        // Template, data switch
        switch (true) {
            case $this->hasAdminRight():
                $template = 'backend/promotion/approval';
                $data['items'] = $this->Promotion_model->get_all_promotion();
                break;
            case isset($t) && $t === 'readonly':
                $template = 'backend/promotion/readonly/index';
                $data['items'] = $this->Promotion_model->get_all_promotion();
                break;
            default:
                $template = 'backend/promotion/index';
                $data['items'] = $this->Promotion_model->get_all_promotion();
                break;
        }
        $this->view($template, $data);
    }

    public function add()
    {
        $template = 'backend/promotion/add';
        $this->view($template, []);
    }

    public function edit()
    {
        $data['id'] = $id = $this->input->get('id');
        $data['promotion'] = $this->Promotion_model->get_promotion($id);

        $this->load->model('Promotion_setting_model');
        $data['promotion']['image_url'] = base_url() . $this->Promotion_setting_model->get_path() . '/' . $data['promotion']['id'] . '/' . $data['promotion']['filename'];
        return $this->view('backend/promotion/edit', $data);
    }

    public function save()
    {
        $data = $this->input->post();

        $date = explode(' ', $data['issuedt'])[0];
        $time = explode(' ', $data['issuedt'])[1];
        $id = isset($data['id']) ? $data['id'] : 0;

        $createAndIsAdmin = $data['mode'] == 'c' && $this->hasAdminRight();
        //print_r($_FILES); die();

        if ($_FILES['fileToUpload']['size'] == 0 && $_FILES['fileToUpload']['error'] == 4) {
            $params =  array(
                'title' => $data['title'],
                'content' => $data['content'],
                'issued_date' => $date,
                'issued_time' => $time,
                'due_datetime' => $data['due_datetime'],
                'show_in_banner' => $data['show_in_banner'] ?? 0,
                'push' => $data['push'] ?? 0,
                'remark' => $data['remark'],
            );

            if ($createAndIsAdmin) {
                $params['approval'] = 3;
            }

            $this->Promotion_model->save_promotion(
                $params,
                $data['mode'],
                $id
            );
        } else {
            //print_r($upload); die();
            $params = array(
                'title' => $data['title'],
                'content' => $data['content'],
                'issued_date' => $date,
                'issued_time' => $time,
                'due_datetime' => $data['due_datetime'],
                'show_in_banner' => $data['show_in_banner'] ?? 0,
                'push' => $data['push'] ?? 0,
                'remark' => $data['remark'],
            );

            if ($createAndIsAdmin) {
                $params['approval'] = 3;
            }
            $id = $this->Promotion_model->save_promotion(
                $params,
                $data['mode'],
                $id
            );

            $f = new FileUpload();
            $this->load->model('Promotion_setting_model');

            $upload = $f->setPath(getcwd() . '/' . $this->Promotion_setting_model->get_path())->image('/' . $id, $_FILES['fileToUpload'], 768, 400);
            $this->Promotion_model->save_promotion([
                'filename' => $upload['filename'],
                'pathname' => $upload['pathname'],
            ], 'u', $id);
        }

        $action = $data['mode'] === 'c' ? 'Saved' : 'Updated';
        $this->session->set_flashdata('response', "已儲存變更");

        $url = $data['mode'] === 'c' ? 'add' : 'edit?id=' . $id;
        redirect('promotion/' . $url);
    }

    public function delete()
    {
        $this->Promotion_model->delete_promotion($this->input->get('id'));
        echo json_encode('0');
    }

    public function approval()
    {
        $id = $this->input->get('id');
        $action = $this->input->get('ar');
        $this->Promotion_model->approval(array('approval' => $action), $id);
        echo json_encode('0');
    }
}
