<?php
namespace App\Controllers\Article;

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

class Record extends BaseController
{

  private $model, $codeModel;

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

  public function lists(): string
  {

    $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;

    return view("pages/article/record/lists", $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'),           // 종료일

      'photo_sdate' => $this->request->getGet('photo_sdate'),           // 촬영완료일자 시작일
      'photo_edate' => $this->request->getGet('photo_edate'),           // 촬영완료일자 종료일

      'record_sdate' => $this->request->getGet('record_sdate'),           // 녹취완료일자 시작일
      'record_edate' => $this->request->getGet('record_edate'),           // 녹취완료일자 종료일
      'rec_yn' => $this->request->getGet('rec_yn'), // 녹취완료여부

      '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'),         // 읍면동

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

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

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

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


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

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

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

        'photo_sdate' => $this->request->getGet('photo_sdate'),           // 촬영완료일자 시작일
        'photo_edate' => $this->request->getGet('photo_edate'),           // 촬영완료일자 종료일

        'record_sdate' => $this->request->getGet('record_sdate'),           // 녹취완료일자 시작일
        'record_edate' => $this->request->getGet('record_edate'),           // 녹취완료일자 종료일
        'rec_yn' => $this->request->getGet('rec_yn'), // 녹취완료여부

        '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'),         // 읍면동

        '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();
    }
  }


  // 상세화면
  public function detail($id): string
  {

    $id = (string) $id;

    if ($id === '') {
      throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
    }

    $codes = $this->codeModel->getCodeLists(['TRADE_TYPE', 'RECEIPT_STATUS2', 'RECEIPT_STATUS3', 'SMS_MSG_TYPE']); // 코드조회
    $bonbu = $this->model->getBonbuList();
    $team = $this->model->getTeamList();

    $data = $this->model->getDetail($id);
    $history = $this->model->getHistory($id);

    $aptGround = $this->model->getAptGround($data['rcpt_dong']);

    // 시간대별통계
    $tmCount = $this->model->getUsrRsrvDateTmCount($id);

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

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

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

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