<?php
namespace App\Controllers\results;

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


class M417 extends BaseController
{
    private $model;

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

    public function stats(): string
    {
        $department = $this->model->getDepart();

        $this->data['department'] = $department;

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


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

        $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(index: 'dept_sq'),
            ];

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

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

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