<?php
namespace App\Controllers\Article;

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

class Receipt2 extends BaseController
{
  private $model, $codeModel;

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

  public function lists(): string
  {
    $codes = $this->codeModel->getCodeLists(['NHN_DEAL_TYPE', 'CP_ID', 'ARTICLE_TYPE', 'VRFCREQ_WAY', 'STEP_VERIFICATION']); // 코드조회
    $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/receipt/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
      'schDateGb' => $this->request->getGet('schDateGb'), // 일자유형
      'sdate' => $this->request->getGet('sdate'),           // 시작일
      'edate' => $this->request->getGet('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'),         // 읍면동

      'rcpt_stat1' => $this->request->getGet('rcpt_stat1'),     // 상태1
      'rcpt_stat2' => $this->request->getGet('rcpt_stat2'),     // 상태2
      'rcpt_stat3' => $this->request->getGet('rcpt_stat3'),     // 상태3

      'rcpt_product_info1' => $this->request->getGet('rcpt_product_info1'), // 거래구분
      'exp_movie_yn' => $this->request->getGet('exp_movie_yn'),             // 동영상촬영여부
      'conf_img_yn' => $this->request->getGet('conf_img_yn'),               // 홍보확인서여부
      'parcel_out_yn' => $this->request->getGet('parcel_out_yn'),           // 분양권
      'rcpt_cpid' => $this->request->getGet('rcpt_cpid'),                   // CPID
      'rcpt_product' => $this->request->getGet('rcpt_product'),             // 매물종류
      'exp_spc_yn' => $this->request->getGet('exp_spc_yn'),                 // 면적확인
      'check_list_img_yn' => $this->request->getGet('check_list_img_yn'),   // 체크리스트
      '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'),         // 직거래
      'image_360_yn' => $this->request->getGet('image_360_yn'),             // 360촬영여부

      'isSiteVRVerification' => $this->request->getGet('isSiteVRVerification'), // 검증방식
      'isPromotionApply' => $this->request->getGet('isPromotionApply'), // 프로모션

      '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
        'schDateGb' => $this->request->getGet('schDateGb'), // 일자유형
        'sdate' => $this->request->getGet('sdate'),           // 시작일
        'edate' => $this->request->getGet('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'),         // 읍면동

        'rcpt_stat1' => $this->request->getGet('rcpt_stat1'),     // 상태1
        'rcpt_stat2' => $this->request->getGet('rcpt_stat2'),     // 상태2
        'rcpt_stat3' => $this->request->getGet('rcpt_stat3'),     // 상태3

        'rcpt_product_info1' => $this->request->getGet('rcpt_product_info1'), // 거래구분
        'exp_movie_yn' => $this->request->getGet('exp_movie_yn'),             // 동영상촬영여부
        'conf_img_yn' => $this->request->getGet('conf_img_yn'),               // 홍보확인서여부
        'parcel_out_yn' => $this->request->getGet('parcel_out_yn'),           // 분양권
        'rcpt_cpid' => $this->request->getGet('rcpt_cpid'),                   // CPID
        'rcpt_product' => $this->request->getGet('rcpt_product'),             // 매물종류
        'exp_spc_yn' => $this->request->getGet('exp_spc_yn'),                 // 면적확인
        'check_list_img_yn' => $this->request->getGet('check_list_img_yn'),   // 체크리스트
        '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'),         // 직거래
        'image_360_yn' => $this->request->getGet('image_360_yn'),             // 360촬영여부

        'isSiteVRVerification' => $this->request->getGet('isSiteVRVerification'), // 검증방식
        'isPromotionApply' => $this->request->getGet('isPromotionApply'), // 프로모션

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