<?php
namespace App\Controllers\Article;

use App\Controllers\BaseController;
use App\Models\article\DeptModel;
use App\Models\common\CodeModel;

class Dept2 extends BaseController
{

  private $model, $codeModel;
  public function __construct()
  {
    $this->codeModel = new CodeModel();
    $this->model = new DeptModel();
  }

  public function lists(): string
  {

    $codes = $this->codeModel->getCodeLists(['NHN_PRODUCT_TYPE', 'RECEIPT_STATUS1', 'RESERVED_APM']); // 코드조회
    $sido = $this->model->getAreaList(); // 지역조회
    $bonbu = $this->model->getBonbuList();
    $team = $this->model->getTeamList();
    $user = $this->model->getUserList();

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


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

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

    $data = [
      'rcpt_atclno' => $this->request->getGet('rcpt_atclno'), // 매물ID

      'sdate' => $this->request->getGet('sdate'),           // 시작일
      'edate' => $this->request->getGet('edate'),           // 종료일

      'rsrv_tm_ap' => $this->request->getGet('rsrv_tm_ap'),   // 유형
      'rsrv_sdate' => $this->request->getGet('rsrv_sdate'),   // 시작일
      'rsrv_edate' => $this->request->getGet('rsrv_edate'),   // 종료일

      'bonbu' => $this->request->getGet('bonbu'),           // 본부
      'team' => $this->request->getGet('team'),           // 팀
      'user' => $this->request->getGet('user'),             // 담당자

      'sido' => $this->request->getGet('sido'),          // 시도
      'gugun' => $this->request->getGet('gugun'),        // 시군구
      'dong' => $this->request->getGet('dong'),         // 읍면동

      'ground_plan_yn' => $this->request->getGet('ground_plan_yn'),         // 평면도유무
      'ground_plan' => $this->request->getGet('ground_plan'),               // 평면도요청
      'direct_trad_yn' => $this->request->getGet('direct_trad_yn'),         // 직거래

      'isSiteVRVerification' => $this->request->getGet('isSiteVRVerification'), // 검증방식
      'conf_img_yn' => $this->request->getGet('conf_img_yn'), // 홍보확인서 여부

      'stat' => $this->request->getGet('stat'),

      'srchType' => $this->request->getGet('srchType'),                     // 검색유형
      'srchTxt' => $this->request->getGet('srchTxt'),                       // 검색어
    ];

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

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

    $deptStatistics = $this->model->getDeptStatistics($data); // 조직별통계
    $areaStatistics = $this->model->getAreaStatistics($data); // 지역별통계

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


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

      $data = [
        'req_rec_yn' => 'Y',
        'rcpt_atclno' => $this->request->getGet('rcpt_atclno'), // 매물ID

        'sdate' => $this->request->getGet('sdate'),           // 시작일
        'edate' => $this->request->getGet('edate'),           // 종료일

        'rsrv_tm_ap' => $this->request->getGet('rsrv_tm_ap'),   // 유형
        'rsrv_sdate' => $this->request->getGet('rsrv_sdate'),   // 시작일
        'rsrv_edate' => $this->request->getGet('rsrv_edate'),   // 종료일

        'bonbu' => $this->request->getGet('bonbu'),           // 본부
        'team' => $this->request->getGet('team'),           // 팀
        'user' => $this->request->getGet('user'),             // 담당자

        'sido' => $this->request->getGet('sido'),          // 시도
        'gugun' => $this->request->getGet('gugun'),        // 시군구
        'dong' => $this->request->getGet('dong'),         // 읍면동

        'ground_plan_yn' => $this->request->getGet('ground_plan_yn'),         // 평면도유무
        'ground_plan' => $this->request->getGet('ground_plan'),               // 평면도요청
        'direct_trad_yn' => $this->request->getGet('direct_trad_yn'),         // 직거래

        'isSiteVRVerification' => $this->request->getGet('isSiteVRVerification'), // 검증방식
        'conf_img_yn' => $this->request->getGet('conf_img_yn'), // 홍보확인서 여부

        'stat' => $this->request->getGet('stat'),

        'srchType' => $this->request->getGet('srchType'),                     // 검색유형
        'srchTxt' => $this->request->getGet('srchTxt'),                       // 검색어
      ];

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

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

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

  // 배정내역 excel
  public function excel2()
  {
    try {

      $data = [
        'req_rec_yn' => 'Y',
        'rcpt_atclno' => $this->request->getGet('rcpt_atclno'), // 매물ID

        'sdate' => $this->request->getGet('sdate'),           // 시작일
        'edate' => $this->request->getGet('edate'),           // 종료일

        'rsrv_tm_ap' => $this->request->getGet('rsrv_tm_ap'),   // 유형
        'rsrv_sdate' => $this->request->getGet('rsrv_sdate'),   // 시작일
        'rsrv_edate' => $this->request->getGet('rsrv_edate'),   // 종료일

        'bonbu' => $this->request->getGet('bonbu'),           // 본부
        'team' => $this->request->getGet('team'),           // 팀
        'user' => $this->request->getGet('user'),             // 담당자

        'sido' => $this->request->getGet('sido'),          // 시도
        'gugun' => $this->request->getGet('gugun'),        // 시군구
        'dong' => $this->request->getGet('dong'),         // 읍면동

        'ground_plan_yn' => $this->request->getGet('ground_plan_yn'),         // 평면도유무
        'ground_plan' => $this->request->getGet('ground_plan'),               // 평면도요청
        'direct_trad_yn' => $this->request->getGet('direct_trad_yn'),         // 직거래

        'isSiteVRVerification' => $this->request->getGet('isSiteVRVerification'), // 검증방식
        'conf_img_yn' => $this->request->getGet('conf_img_yn'), // 홍보확인서 여부

        'stat' => $this->request->getGet('stat'),

        'srchType' => $this->request->getGet('srchType'),                     // 검색유형
        'srchTxt' => $this->request->getGet('srchTxt'),                       // 검색어
      ];

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

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

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