<?php
namespace App\Controllers\manage;

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



class Phone extends BaseController
{
    private $phoneModel;

    public function __construct()
    {
        $this->phoneModel = new PhoneModel();
    }

    public function lists(): string
    {
        $codes = $this->phoneModel->getCodes();

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

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

    // 전화확인 목록조회
    public function getDuplPhoneList()
    {
        $start = (int) $this->request->getGet('start') ?: 0;
        $end = (int) $this->request->getGet('length') ?: 10;

        $data = [
            'cpid' => $this->request->getGet('cpid') ?: '',
            's_date' => $this->request->getGet('s_date') ?: '',
            'e_date' => $this->request->getGet('e_date') ?: '',
            'phone' => $this->request->getGet('phone') ?: '',
            'useYn' => $this->request->getGet('useYn') ?: '',
        ];

        $totalCount = $this->phoneModel->getTotalCount($data);
        $datas = $this->phoneModel->getDuplPhoneList($start, $end, $data);

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

    }


    // 전화정보 저장
    public function savePhone()
    {
        try {

            if ($this->validPhone($this->request->getPost('phone_number'))) {
                return $this->response->setJSON([
                    'code' => '1',
                    'msg' => '유효하지 않은 연락처'
                ]);
            }

            $type = $this->request->getPost('type');
            $data = [
                $this->request->getPost('phone_number'), // 연락처
                $this->request->getPost('use_yn'),
                $this->request->getPost('s_date'),
                $this->request->getPost('e_date'),
                $this->request->getPost('address'),
                $this->request->getPost('owner'),
                $this->request->getPost('applicant'),
                $this->request->getPost('relation'),
                $this->request->getPost('cpid'),
                $this->request->getPost(index: 'memo'),
                session('usr_id'),
            ];

            if ($type === "create") {

                // INSERT dupl_phone_list
                $this->phoneModel->insertDuplPhone($data);

            } else if ($type === "update") {

                array_push($data, $this->request->getPost("phone_number"));

                // UPDATE dupl_phone_list
                $this->phoneModel->updateDuplPhone($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 = [
                'cpid' => $this->request->getGet('cpid') ?: '',
                's_date' => $this->request->getGet('s_date') ?: '',
                'e_date' => $this->request->getGet('e_date') ?: '',
                'phone' => $this->request->getGet('phone') ?: '',
                'useYn' => $this->request->getGet('useYn') ?: '',
            ];

            $datas = $this->phoneModel->getExcelPhoneList($data);

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

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


    }


    // 연락처 유효성검사
    private function validPhone($phone)
    {
        if (preg_match('/^01[0-9]-?\d{3,4}-?\d{4}$/', $phone)) {
            return true;
        } else {
            return false;
        }
    }

}