<?php
namespace App\Controllers\results;

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

class Person extends BaseController
{
    private $personModel;
    public function __construct()
    {
        $this->personModel = new PersonModel();
    }

    public function index()
    {
    }

    public function lists(): string
    {
        $bonbu = $this->personModel->getBonbuList();
        $team = $this->personModel->getTeamList();
        $sido = $this->personModel->getAreaList();

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

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

    public function getUserList()
    {
        $start = (int) $this->request->getGet('start') ?: 0;
        $end = (int) $this->request->getGet('length') ?: 10;

        $data = [
            'bonbu' => $this->request->getGet('bonbu'),
            'team' => $this->request->getGet('team'),
            'schDateGb' => $this->request->getGet('schDateGb'),
            'sdate' => $this->request->getGet('sdate'),
            'edate' => $this->request->getGet('edate'),
            'srchType' => $this->request->getGet('srchType'),
            'srchTxt' => $this->request->getGet('srchTxt'),
        ];

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


        $datas = $this->personModel->getUserList($start, $end, $data);

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

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

            $data = [
                'bonbu' => $this->request->getGet('bonbu'),
                'team' => $this->request->getGet('team'),
                'schDateGb' => $this->request->getGet('schDateGb'),
                'sdate' => $this->request->getGet('sdate'),
                'edate' => $this->request->getGet('edate'),
                'srchType' => $this->request->getGet('srchType'),
                'srchTxt' => $this->request->getGet('srchTxt'),
            ];

            $datas = $this->personModel->getExcelUserList($data);

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

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



    }
}