<?php
namespace App\Controllers\listfax;

use App\Controllers\BaseController;
use App\Libraries\Common;
use App\Models\listfax\ListFaxModel;


class ListFax extends BaseController
{

    private $listfaxModel;

    private $todo = '';				// 검색, 엑셀다운 구분
    private $menuid = '';			// 레프트메뉴아이디

    private $page = '';				// 현재페이지
    private $pagesize = '';			// 페이징 개수

    private $atcl_no = '';			// 매물번호
    private $stat_cd = '';			// 현재상태
    private $chk_atcl_no = '';		// 매물번호 입력/미입력 구분
    private $realtor_nm = '';		// 중개소
    private $recv_stime = '';		// 수신기간(시간)
    private $recv_etime = '';		// 수신기간(시간)
    private $complete_stime = '';	// 처리기간(전송)
    private $complete_etime = '';	// 처리기간(전송)
    private $sido = '';				// 시도
    private $gugun = '';			// 시군구
    private $dong = '';				// 읍면동
    private $target_yn = '';		// 작업대상여부(정상여부)
    private $cpid = '';				// 매체사(CP)ID
    private $caller_no = '';		// 발신번호
    private $charger = '';			// 담당자
    private $charger_gbn = '';		// 담당구분 1: 서류/전화, 2: 등기부등본
    private $dept1_sq = '';			// 본부
    private $dept2_sq = '';			// 팀
    private $assign_yn = '';		// 배정여부 1: 배정, 2: 미배정 
    private $checking = '0';		// 조회여부 확인

    private $pars = [];

    public function __construct()
    {
        $this->listfaxModel = new ListFaxModel();
    }

    public function index()
    {
        $this->initParams();
    }


    public function initParams()
    {
        // parent::__construct();

        $this->todo = $this->request->getGet('todo', TRUE);
        $this->menuid = $this->request->getGet('m', TRUE);

        $this->page = $this->request->getGet('page', TRUE);
        $this->pagesize = $this->request->getGet('pagesize', TRUE);

        if (empty($this->todo)) {
            $this->todo = 'inq';
        }

        $this->atcl_no = $this->request->getGet('atcl_no', TRUE);
        $this->stat_cd = $this->request->getGet('stat_cd', TRUE);
        $this->chk_atcl_no = $this->request->getGet('chk_atcl_no', TRUE);
        $this->realtor_nm = $this->request->getGet('realtor_nm', TRUE);
        $this->recv_stime = $this->request->getGet('recv_stime', TRUE);
        $this->recv_etime = $this->request->getGet('recv_etime', TRUE);
        $this->complete_stime = $this->request->getGet('complete_stime', TRUE);
        $this->complete_etime = $this->request->getGet('complete_etime', TRUE);
        $this->sido = $this->request->getGet('sido', TRUE);
        $this->gugun = $this->request->getGet('gugun', TRUE);
        $this->dong = $this->request->getGet('dong', TRUE);
        $this->target_yn = $this->request->getGet('target_yn', TRUE);
        $this->cpid = $this->request->getGet('cpid', TRUE);
        $this->caller_no = $this->request->getGet('caller_no', TRUE);
        $this->charger = $this->request->getGet('charger', TRUE);
        $this->charger_gbn = $this->request->getGet('charger_gbn', TRUE);
        $this->dept1_sq = $this->request->getGet('dept1_sq', TRUE);
        $this->dept2_sq = $this->request->getGet('dept2_sq', TRUE);
        $this->assign_yn = $this->request->getGet('assign_yn', TRUE);
        $this->checking = $this->request->getGet('checking', TRUE);

        // 기본 선택 - 기본값을 수신시간을 당일로 처리한다.
        $toDay = date('Y-m-d');
        if (empty($this->recv_stime))
            $this->recv_stime = date("Y-m-d", strtotime("-7 days")) . ' 00:00:00';
        if (empty($this->recv_etime))
            $this->recv_etime = $toDay . ' 23:59:59';

        if (empty($this->assign_yn))
            $this->assign_yn = 'A';

        $this->pars = array(
            'm' => $this->menuid,
            'todo' => $this->todo,
            'page' => $this->page,
            'pagesize' => $this->pagesize,

            'atcl_no' => $this->atcl_no,
            'stat_cd' => $this->stat_cd,
            'chk_atcl_no' => $this->chk_atcl_no,
            'realtor_nm' => $this->realtor_nm,
            'recv_stime' => $this->recv_stime,
            'recv_etime' => $this->recv_etime,
            'complete_stime' => $this->complete_stime,
            'complete_etime' => $this->complete_etime,
            'sido' => $this->sido,
            'gugun' => $this->gugun,
            'dong' => $this->dong,
            'target_yn' => $this->target_yn,
            'cpid' => $this->cpid,
            'caller_no' => $this->caller_no,
            'charger' => $this->charger,
            'charger_gbn' => $this->charger_gbn,
            'dept1_sq' => $this->dept1_sq,
            'dept2_sq' => $this->dept2_sq,
            'assign_yn' => $this->assign_yn,
            'checking' => $this->checking,
        );


    }


    /**
     * 홍보확인서 팩스 목록
     */
    public function lists()
    {
        // CI3 __construct 대체
        $this->initParams();

        // CI3: $base_uri = '/listfax/listfax/lists';
        $base_uri = '/listfax/listfax/lists';

        // CI3: $this->load->model('v2/listfax_model', 'listfax_model');        

        // CI3: $this->load->helper('array_helper');
        helper(filenames: 'array'); // array_helper => array 로 이름 바꿨다면 이렇게

        // CI3: $this->load->library('common');
        $common = new Common();

        // checking 값에 따라 조회 여부 결정
        if ($this->pars['checking'] === '0') {
            $resultData = [
                'data' => [],
                'total' => 0,
            ];
        } else {
            $resultData = $this->listfaxModel->getLists(
                $this->todo,
                $this->page,
                $this->pagesize,
                $this->recv_stime,
                $this->recv_etime,
                $this->caller_no
            );
        }

        // total 기본값 처리
        if (!isset($resultData['total'])) {
            $resultData['total'] = 0;
        }

        // 페이지네이션 (기존 common 라이브러리 그대로 사용)
        $pagination = $common->make_pagenation(
            $base_uri,
            $this->pars,
            $resultData['total'],
            $this->page,
            $this->pagesize
        );

        $data = [
            'pars' => $this->pars,
            'dataList' => $resultData['data'] ?? [],
            'total' => $resultData['total'],
            'pagination' => $pagination,
        ];

        // BaseController에서 메뉴 등 공통 $this->data 세팅했다면 같이 합쳐서 넘겨도 됨
        // return view('listfax/listfax/lists', array_merge($this->data, $data));
        return view('listfax/listfax/lists', $data);
    }


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


        $data = [
            'caller_no' => $this->request->getGet('caller_no'),
        ];

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

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

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

}