<?php
namespace App\Controllers\results;

use App\Controllers\BaseController;
use App\Models\results\M410Model;

class M410 extends BaseController
{
    private $model;

    public function __construct()
    {
        $this->model = new M410Model();
    }

    public function stats(): string
    {
        $codes = $this->model->getCodeList(['VRFCREQ_WAY', 'CP_ID']);

        $CODE_VRFCREQ_WAY = convertArrayToHashTable($codes['VRFCREQ_WAY'], 'cd', 'cd_nm', []);
        $CODE_CP_ID = convertArrayToHashTable($codes['CP_ID'], 'cd', 'cd_nm', []);
        $department = $this->model->getDepart();

        $this->data['code_vrfcreq_way'] = $CODE_VRFCREQ_WAY;
        $this->data['code_cp_id'] = $CODE_CP_ID;
        $this->data['department'] = $department;

        return view("pages/results/m410/stats", $this->data);
    }

    public function getResultList()
    {
        $data = [
            'sdate' => $this->request->getGet('sdate'),
            'edate' => $this->request->getGet('edate'),
            'dept_sq' => $this->request->getGet('dept_sq'),
            'vrfcreq_way' => $this->request->getGet('vrfcreq_way'),
        ];

        $totalCount = $this->model->getTotalCount($data);

        $datas = $this->model->getResultList($data);

        return $this->response->setJSON(body: [
            'recordsTotal' => $totalCount,
            'recordsFiltered' => $totalCount,
            'data' => $datas,
        ]);

    }

    // 엑셀 다운로드
    public function excel()
    {
        try {

            $data = [
                'sdate' => $this->request->getGet('sdate'),
                'edate' => $this->request->getGet('edate'),
                'dept_sq' => $this->request->getGet('dept_sq'),
                'vrfcreq_way' => $this->request->getGet('vrfcreq_way'),
            ];

            $datas = $this->model->getResultList($data);

            return $this->response->setJSON(body: [
                'data' => $datas,
            ]);

        } catch (\Exception $e) {
            $e->getPrevious()->getTraceAsString();
        }
    }
}