<?php

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

/**
 * @property CI_Session $session
 * @property Shift_approval_model $Shift_approval_model
 * @property Menu_badge_notification_model $Menu_badge_notification_model
 */
class Shift_approval extends MY_BackendController
{

    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->model('Menu_badge_notification_model');
        $this->Menu_badge_notification_model->read($this->Menu_badge_notification_model::NAME_MAKEUP);
        $this->view('backend/shift_approval/index', []);
    }

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

    public function insert()
    {
        $input = $this->input->post();
        if (!empty($input)) {
            if (count($_FILES) > 0) { //mandatory
                $fileExt = pathinfo($_FILES['shift_approval']['name'], PATHINFO_EXTENSION);
                $tenantno = $this->session->userdata('tenant_no');
                //                log_message('debug','ext:'.$fileExt);//pdf
                $data = array(
                    'unit_id' => $this->input->post('unit_id'),
                    'title' => $this->input->post('title'),
                    'remark' => $this->input->post('remark'),
                    'tenant_no' => $tenantno,
                    'ext' => $fileExt,
                );

                $this->load->model('Shift_approval_model');
                $shift_approvalCode = $this->Shift_approval_model->insert($data);
                //                $shift_approvalCode = $this->shift_approvals_model->add($data);
                $newName = UPLOADSPATH . DS . $tenantno . DS . "shift_approvals" . DS . $shift_approvalCode . "." . $fileExt;
                //        $newName = "..\\..\\cache\\" . $shift_approvalCode . "." . $fileExt;
                //                log_message('debug', 'path:' . $newName);
                rename($_FILES['shift_approval']['tmp_name'], $newName);
                chmod($newName, 0644);
            }
            header('location: /shift_approval');
        }
    }

    public function edit($id)
    {
        $this->load->model('Shift_approval_model');
        $data = $this->Shift_approval_model->one($id);
        $this->view('backend/shift_approval/edit', ['status' => $this->Shift_approval_model->status, 'data' => $data]);
    }

    // Insert / Update
    public function save()
    {
        $input = $this->input->post();
        if (!empty($input)) {
            $id = $input['id'];
            $tenantno = $this->session->userdata('tenant_no');
            $fileExt = '';
            if (count($_FILES) > 0) {
                $fileExt = pathinfo($_FILES['shift_approval']['name'], PATHINFO_EXTENSION);
                $shift_approvalCode = $this->Shift_approval_model->one($id)['shift_approval_code'];
                $newName = UPLOADSPATH . DS . $tenantno . DS . "shift_approvals" . DS . $shift_approvalCode . "." . $fileExt;
                rename($_FILES['shift_approval']['tmp_name'], $newName);
                chmod($newName, 0644);
            }
            $data = array(
                'unit_id' => $this->input->post('unit_id'),
                'title' => $this->input->post('title'),
                'remark' => $this->input->post('remark'),
                'tenant_no' => $tenantno,
                'status' => $this->input->post('status'),
                'ext' => $fileExt,
            );

            $this->load->model('Shift_approval_model');
            $this->Shift_approval_model->save($data, $id);

            // $this->Shift_approval_model->trigger_push_notification($id);
        }
        header('location:/shift_approval');
    }

    public function delete()
    {
        $request = $this->input->post();
        $ids = explode(',', $request['ids']);
        //       log_message('debug','is ids array?'.is_array($ids));//true
        $this->Shift_approval_model->deleteMultiple($ids);
        header("location:/shift_approval");
    }

    public function deleteall()
    {
        $this->Shift_approval_model->deleteAll();
        header("location:/shift_approval");
    }
}
