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

class Messaging extends MY_BackendController
{

    //    protected $authorizedRoutes = ['index', 'addpending', 'add', 'approval', 'editpending', 'edit', 'delete'];

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

    public function index()
    {
        $t = $this->input->get('t');
        // Template, data switch
        switch (true) {
            case $this->hasAdminRight():
                $data['items'] = $this->Messaging_model->get_all_messaging();
                $template = 'backend/messaging/approval';
                break;
            case isset($t) && $t === 'readonly':
                $data['items'] = $this->Messaging_model->get_all_messaging();
                $template = 'backend/messaging/index';
                break;
            default:
                $data['items'] = $this->Messaging_model->get_all_messaging();
                $template = 'backend/messaging/index';
                break;
        }
        $this->view($template, $data);
    }

    public function add()
    {
        $this->load->helper('form');
        $data['flag'] = intval($this->input->get('flag'));
        $template = 'backend/messaging/add';
        $this->view($template, $data);
    }

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

    public function edit()
    {
        $id = $this->input->get('id');
        //        $data['flag'] = intval($this->input->get('flag'));
        $data['item'] = $this->Messaging_model->get_messaging($id);
        $template = 'backend/messaging/edit';
        $this->view($template, $data);
    }

    public function save()
    {
        $data = $this->input->post();
        $date = date('Y-m-d', strtotime($data['issued_date']));
        $id = isset($data['id']) ? $data['id'] : 0;

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

        $params = array(
            'issued_date' => $date,
            'issued_time' => $data['issued_time'],
            'title' => $data['title'],
            'content' => $data['content'],
            'push' => $data['push'] ?? 0,
            'approval' => $this->hasAdminRight() ? 3 : 0
        );

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

        if ($data['mode'] == 'c') {
            $this->load->model('Push_notification_model', 'pm');
            $this->pm->title = '您有一封新的訊息';
            $this->pm->body = $params['title'];
            $this->pm->payload($params);
        }

        echo json_encode('0');
    }

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