<?php
namespace App\Controllers\manage;

use App\Controllers\BaseController;
use App\Models\manage\ScomplexModel;

class Scomplex extends BaseController
{
    private $model;

    public function __construct()
    {
        $this->model = new ScomplexModel();
    }

    public function lists(): string
    {
        $codes = $this->model->getCodeList();

        $this->data['code'] = $codes;

        return view("pages/manage/scomplex/lists", $this->data);
    }


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

        $data = [
            'name' => $this->request->getGet('name'), // 단지명
            'apporval_date' => $this->request->getGet('apporval_date'), // 사용승인일
            'end_date' => $this->request->getGet('end_date'), // 승인종료일
            'code' => $this->request->getGet('code'), // 단지코드
            'cd' => $this->request->getGet('cd'), // 매물종류
            'address' => $this->request->getGet('address'), // 주소
        ];

        $totalCount = $this->model->getTotalCount($data);
        $datas = $this->model->getScomplexList($start, $end, $data);

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


    // 특이단지 정보 저장
    public function saveScomplex()
    {
        try {

            $type = $this->request->getPost('type');

            $data = [
                $this->request->getPost('sm_name'),
                $this->request->getPost('sm_code'),
                $this->request->getPost('sm_address'),
                $this->request->getPost('codes'),
                $this->request->getPost('sm_apporval_date'),
                $this->request->getPost('sm_end_date'),
                $this->request->getPost('sm_memo'),
                session('usr_sq'),
            ];


            if ($type === "create") {
                // INSERT scomplex_manage
                $this->model->insertScomplex($data);

            } else if ($type === "update") {
                array_push($data, $this->request->getPost('sm_seq'));

                // UPDATE scomplex_manage
                $this->model->updateScomplex($data);
            }


            return $this->response->setJSON([
                'code' => '0',
                'msg' => 'success'
            ]);

        } catch (\Exception $e) {
            return $this->response->setJSON([
                'code' => '9',
                'msg' => $e->getMessage(),
            ]);
        }
    }

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

        try {

            $data = [
                'name' => $this->request->getGet('name'), // 단지명
                'apporval_date' => $this->request->getGet('apporval_date'), // 사용승인일
                'end_date' => $this->request->getGet('end_date'), // 승인종료일
                'code' => $this->request->getGet('code'), // 단지코드
                'cd' => $this->request->getGet('cd'), // 매물종류
                'address' => $this->request->getGet('address'), // 주소
            ];

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

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

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


    }
}