<?php
namespace App\Controllers\results;

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

class M409 extends BaseController
{

    private $model;

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

    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', []);

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

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

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

        $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'),
                'vrfcreq_way' => $this->request->getGet('vrfcreq_way'),
                'cp_id' => $this->request->getGet('cp_id'),
            ];

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

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

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