<?php

use app\libraries\DateHelper;
use app\libraries\DefinedException;
use app\libraries\Json;

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

class Event_qrcode_scanned extends MY_BackendController
{
    use Json;

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

        $this->load->model('Student_model');
        $this->load->model('Event_timeslot_model');
        $this->load->model('Event_enrollment_item_model');
    }

    public function index()
    {
        $eventId = $this->input->get('eventId');
        $timeslotId = $this->input->get('timeslotId');
        $action = $this->input->get('action');
        $studentNo = $this->input->get('studentNo');

        try {

            $student = $this->Student_model->detail_studentNo($studentNo);
            if (!$student) {
                throw new DefinedException('無效的學生編號');
            }

            $studentId = $student['id'];

            /**
             * @var EventEnrollmentItem
             */
            $eei = $this->Event_enrollment_item_model->detail_in_qrcode_scanned($eventId, $timeslotId, $studentId);

            if (!$eei) {
                throw new DefinedException('找不到學生報讀課程紀錄');
            }

            // if ($action == 'in' && $eei['in_datetime']) {
            //     throw new DefinedException('已有該學生的報到紀錄, 不能重覆報到');
            // }

            // if ($action == 'out' && !$eei['in_datetime']) {
            //     throw new DefinedException('沒有該學生的報到紀錄, 不能報走');
            // }

            // if ($action == 'out' && $eei['out_datetime']) {
            //     throw new DefinedException('已有該學生的報走紀錄, 不能重覆報走');
            // }

            $hk = new DateTimeZone('Asia/Hong_Kong');

            switch ($action) {
                case "in":
                    $eei = array_merge($eei, $this->Event_enrollment_item_model->update([
                        'in_datetime' => (new DateTime('now', $hk))->getTimestamp(),
                        'txn_status' => 'P'
                    ], $eei['id']));
                    break;
                case "out":
                    $eei = array_merge($eei, $this->Event_enrollment_item_model->update([
                        'out_datetime' => (new DateTime('now', $hk))->getTimestamp(),
                    ], $eei['id']));
                    break;
            }
            $data = [
                'id' => $eei['id'],
                'student_id' => $studentId,
                'student_no' => $studentNo,
                'student_name' => $student['chi_name'],
                'in_datetime' => date('Y-m-d H:i', $eei['in_datetime']),
                'out_datetime' => date('Y-m-d H:i', $eei['out_datetime'])
            ];

            return $this->json_response([
                'error' => false,
                'success' => true,
                'message' => '紀錄已更新',
                'data' => $data
            ]);
        } catch (DefinedException $ex) {
            return $this->json_response([
                'error' => true,
                'success' => false,
                'message' => $ex->getMessage()
            ]);
        } catch (Exception $ex) {
            return $this->json_response([
                'error' => true,
                'success' => false,
                'message' => '記錄更新失敗'
            ]);
        }
    }
}
