<?php
namespace App\Controllers\results;

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

class Dept extends BaseController
{
    private $deptModel;
    private $schDateGb = '';	// 일자구분
    private $sdate = '';		// 시작일자
    private $edate = '';		// 종료일자

    private $bonbu = '';		// 본부
    private $dept_sq = '';		// 팀

    public function __construct()
    {
        $this->deptModel = new DeptModel();
    }

    public function index()
    {


    }


    public function lists(): string
    {


        $this->schDateGb = $this->request->getPost('schDateGb') ?? '1';

        $this->sdate = $this->request->getPost('sdate');
        if (empty($this->sdate)) {
            $this->sdate = date('Y-m-01'); // 이번달 1일
        }

        $this->edate = $this->request->getPost('edate');
        if (empty($this->edate)) {
            $this->edate = date('Y-m-t');  // 이번달 말일
        }

        $this->bonbu = $this->request->getPost('bonbu');
        if (empty($this->bonbu)) {
            $this->bonbu = '';
        }

        $this->dept_sq = $this->request->getPost('dept_sq');
        if (empty($this->dept_sq)) {
            $this->dept_sq = '';
        }

        $data = [
            'schDateGb' => $this->schDateGb,
            'sdate' => $this->sdate,
            'edate' => $this->edate,
            'bonbu' => $this->bonbu,
            'dept_sq' => $this->dept_sq,
        ];

        $bonbu = $this->deptModel->getBonbuList();
        $team = $this->deptModel->getTeamList();

        $res = $this->deptModel->st_d01($data);

        $this->data['pBonbu'] = $this->bonbu;
        $this->data['pDeptSq'] = $this->dept_sq;
        $this->data['schDateGb'] = $this->schDateGb;
        $this->data['sdate'] = $this->sdate;
        $this->data['edate'] = $this->edate;
        $this->data['bonbu'] = $bonbu;
        $this->data['team'] = $team;
        $this->data['st_list'] = $res;

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

    // 엑셀 다운로드
    public function excel()
    {
        $data = [
            'schDateGb' => $this->request->getGet('schDateGb'),
            'sdate' => $this->request->getGet('sdate'),
            'edate' => $this->request->getGet('edate'),
            'bonbu' => $this->request->getGet('bonbu'),
            'dept_sq' => $this->request->getGet('dept_sq'),
        ];


        $res = $this->deptModel->getExcelList($data);

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