<?php

/**
 * @property Student_model $Student_model
 * @property Tenant_model $Tenant_model
 */
class Event_enrollment_item_reception_model extends MY_Model
{
    public $table;
    public $tbl;
    public $short = "eir";
    public $fullTbl;
    public $fullTable;

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

        $this->database = $this->getTenantNo();
        $this->tbl = 'event_enrollment_item_reception';
        $this->table = $this->tbl . ' ' . $this->short;
        $this->fullTbl = $this->table_prefix . $this->tbl;
        $this->fullTable = $this->table_prefix . $this->table;
    }

    public function create($params)
    {
        $params['id'] = $this->insertDB($this->database, $this->tbl, $params);
        return $params;
    }

    public function update($params, $id)
    {
        $this->saveDB($this->database, $this->tbl, $params, $id);
        $params['id'] = $id;
        return $params;
    }

    public function in($studentNo)
    {
        $this->db = $this->load->database($this->database, true);

        $existing = $this->db
            ->from($this->fullTable)
            ->join($this->table_prefix . 'student s', 'eir.student_id = s.id')
            ->where('s.student_no', $studentNo)
            ->where('FROM_UNIXTIME(in_datetime, "%Y-%m-%d") = "' . date('Y-m-d') . '"', null, false)
            ->get()
            ->row_array();

        if ($existing) {
            unset($existing['out_datetime']);
            unset($existing['delete_time']);
            unset($existing['update_time']);
            unset($existing['create_time']);
            unset($existing['created_by']);
            unset($existing['updated_by']);

            $existing['in_datetime'] = date('Y-m-d H:i', $existing['in_datetime']);
            return $existing;
        } else {
            $this->load->model('Student_model');
            $this->Student_model->setTenantNo($this->getTenantNo());

            $existing = $this->Student_model->detail_studentNo($studentNo);

            if ($existing) {
                $datetime = time();

                $res = $this->create([
                    'student_id' => $existing['id'],
                    'in_datetime' => $datetime
                ]);

                $date = date('Y-m-d H:i', $datetime);
                return $existing + ['in_datetime' => $date];

                return $res;
            } else {
                return [];
            }
        }
    }

    public function out($studentNo)
    {
        $this->load->model('Student_model');
        $this->Student_model->setTenantNo($this->getTenantNo());

        $existing = $this->Student_model->detail_studentNo($studentNo);
        $studentId = $existing['id'];

        $datetime = time();

        $this->db = $this->load->database($this->database, true);
        $this->db
            ->from($this->fullTbl)
            ->where('student_id', $studentId)
            ->where('FROM_UNIXTIME(in_datetime, "%Y-%m-%d") = "' . date('Y-m-d') . '"', null, false)
            ->set(['out_datetime' => $datetime])
            ->update();

        // TODO: remove this ad-hoc fix and change the app and listen to out_datetime instead of in_datetime
        $existing['in_datetime'] = date('Y-m-d H:i', $datetime);
        $existing['out_datetime'] = date('Y-m-d H:i', $datetime);
        return $existing;
    }

    // public function trigger_push_notification($id, $reception_in = true)
    // {
    //     $tenant_no = $this->getTenantNo();

    //     $existing = $this->getOne($this->database, $this->tbl, [], $id);

    //     $this->load->model('Student_model');
    //     $this->Student_model->setTenantNo($tenant_no);
    //     $student = $this->Student_model->getOne($this->Student_model->database, $this->Student_model->tbl, ['id', 'chi_name', 'eng_name'], $existing['student_id']);

    //     $this->load->model('Tenant_model');
    //     $tenant = $this->Tenant_model->getOne($this->Tenant_model->database, $this->Tenant_model->tbl, ['name'], $tenant_no, 'code');

    //     $this->push_notification($data = [
    //         'title' => '分校訊息',
    //         'body' => sprintf(
    //             '學生 %s 已在 %s 分校 %s 報%s',
    //             !empty($student['chi_name']) ? $student['chi_name'] :  $student['eng_name'],
    //             $tenant['name'],
    //             date('Y-m-d H:i', $existing[$reception_in ? 'in_datetime' : 'out_datetime']),
    //             $reception_in ? '到' : '走'
    //         )
    //     ], __CLASS__, $existing['id'], [$existing['student_id']], $reception_in ? 'in_sent' : 'out_sent');
    // }
}
