<?php
namespace App\Controllers\results;

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

class M416 extends BaseController
{
    private $model;
    public function __construct()
    {
        $this->model = new M416Model();
    }

    public function stats(): string
    {

        $bonbu = $this->model->getBonbuList();
        $team = $this->model->getTeamList();
        $sido = $this->model->getAreaList();

        $this->data['bonbu'] = $bonbu;
        $this->data['team'] = $team;
        $this->data['sido'] = $sido;

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


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

        $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'),
                'bonbu' => $this->request->getGet('bonbu'),
                'dept_sq' => $this->request->getGet('dept_sq'),
                'srchType' => $this->request->getGet('srchType'),
                'srchTxt' => $this->request->getGet('srchTxt'),
            ];

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

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

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