<?php
namespace App\Controllers\Article;

use App\Controllers\BaseController;

use App\Libraries\Common;
use App\Libraries\MyUpload;
use App\Models\article\ReceiptModel;
use App\Models\common\CodeModel;

class Receipt 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/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
            '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촬영여부
            '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촬영여부
                '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)
    {
        $id = (string) $id;

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

        $t0 = microtime(true);
        $codes = $this->codeModel->getCodeLists(['TRADE_TYPE', 'RECEIPT_STATUS2', 'RECEIPT_STATUS3', 'SMS_MSG_TYPE', 'SMS_MSG_TYPE2']); // 코드조회
        log_message('info', '[Receipt::detail] getCodeLists {ms}ms', ['ms' => (int) ((microtime(true) - $t0) * 1000)]);

        $t1 = microtime(true);
        $bonbu = $this->model->getBonbuList();
        log_message('info', '[Receipt::detail] getBonbuList {ms}ms', ['ms' => (int) ((microtime(true) - $t1) * 1000)]);

        $t2 = microtime(true);
        $team = $this->model->getTeamList();
        log_message('info', '[Receipt::detail] getTeamList {ms}ms', ['ms' => (int) ((microtime(true) - $t2) * 1000)]);

        // sms 코드
        $sms = [];
        foreach ($codes as $c) {
            if ($c['category'] === "SMS_MSG_TYPE2")
                array_push($sms, $c);
        }

        $t3 = microtime(true);
        $data = $this->model->getDetail($id);
        log_message('info', '[Receipt::detail] getDetail {ms}ms', ['ms' => (int) ((microtime(true) - $t3) * 1000)]);

        $t4 = microtime(true);
        $history = $this->model->getHistory($id);
        log_message('info', '[Receipt::detail] getHistory {ms}ms', ['ms' => (int) ((microtime(true) - $t4) * 1000)]);

        $t5 = microtime(true);
        if ($data['rcpt_jibun_addr']) {
            $dupleGroundPlan = $this->model->getDupleGP_na($id, $data['rcpt_sido'], $data['rcpt_gugun'], $data['rcpt_dong'], $data['rcpt_hscp_nm'], $data['rcpt_dtl_addr'], $data['rcpt_li_addr'], $data['rcpt_jibun_addr'], $data['rcpt_etc_addr']);
        } else {
            $dupleGroundPlan = $this->model->getDupleGP($id, $data['rcpt_sido'], $data['rcpt_gugun'], $data['rcpt_dong'], $data['rcpt_hscp_nm'], $data['rcpt_dtl_addr'], $data['rcpt_ho']);
        }
        log_message('info', '[Receipt::detail] getDupleGP {ms}ms', ['ms' => (int) ((microtime(true) - $t5) * 1000)]);

        $t6 = microtime(true);
        $aptGround = $this->model->getAptGround($data['rcpt_dong'] ?? '');
        log_message('info', '[Receipt::detail] getAptGround {ms}ms', ['ms' => (int) ((microtime(true) - $t6) * 1000)]);


        // 이미지 파일리스트
        $t7 = microtime(true);
        $images = $this->model->getImageList2($data['rsrv_sq']);
        log_message('info', '[Receipt::detail] getImageList2 {ms}ms', ['ms' => (int) ((microtime(true) - $t7) * 1000)]);

        $t8 = microtime(true);
        $imgs_count = $this->model->getImageCountByType($data['rsrv_sq']);
        log_message('info', '[Receipt::detail] getImageCountByType {ms}ms', ['ms' => (int) ((microtime(true) - $t8) * 1000)]);
        $imgs_count = convertArrayToHashTable($imgs_count, 'img_type', 'img_cnt');

        //녹취파일
        $t9 = microtime(true);
        $record = $this->model->getRecordInfo($data['rsrv_sq']);
        log_message('info', '[Receipt::detail] getRecordInfo {ms}ms', ['ms' => (int) ((microtime(true) - $t9) * 1000)]);

        // 시간대별통계
        $t10 = microtime(true);
        $tmCount = $this->model->getUsrRsrvDateTmCount($id);
        log_message('info', '[Receipt::detail] getUsrRsrvDateTmCount {ms}ms', ['ms' => (int) ((microtime(true) - $t10) * 1000)]);

        // 체크리스트 조회
        $t11 = microtime(true);
        if ($data['exp_photo_yn'] === "N") {
            $result_check = $this->model->getChecklist($data['rsrv_sq']);
        } else {
            $result_check = [];
        }

        $complexList = [];
        $ptpList = [];

        if ($data['comp_sq'] == '2') {
            // $callApi = new CallApi();
        }

        log_message('info', '[Receipt::detail] getChecklist {ms}ms', ['ms' => (int) ((microtime(true) - $t11) * 1000)]);

        log_message('info', '[Receipt::detail] total {ms}ms', ['ms' => (int) ((microtime(true) - $t0) * 1000)]);

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

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

        $this->data['dupleGroundPlan'] = $dupleGroundPlan;
        $this->data['apt_ground'] = $aptGround;

        $this->data['images'] = $images;
        $this->data['imgs_count'] = $imgs_count;

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

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


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


    // 연락처 저장
    public function saveTel()
    {
        try {

            $tel = $this->request->getPost('agent_tel');

            $this->model->saveTel($tel);


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

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

    // 평면도요청 저장
    public function resGround()
    {
        try {

            $rcpt_sq = $this->request->getPost('rcpt_sq');
            $ground_plan = $this->request->getPost('ground_plan');

            $this->model->saveGround($rcpt_sq, $ground_plan);


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

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

    // 예약확정 저장
    public function assignRegist()
    {
        try {


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

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

    // 동영상촬영여부 저장
    public function requestMovie()
    {
        try {

            $rcpt_sq = $this->request->getPost('rcpt_sq');
            $rsrv_sq = $this->request->getPost('rsrv_sq');
            $exp_movie_yn = $this->request->getPost('exp_movie_yn');

            $this->model->saveRequestMovie($rcpt_sq, $rsrv_sq, $exp_movie_yn);


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

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

    // 중개사메모 저장
    public function requestMessage()
    {
        try {

            $rcpt_sq = $this->request->getPost('rcpt_sq');
            $rsrv_sq = $this->request->getPost('rsrv_sq');
            $request_msg = $this->request->getPost('request_msg');

            $this->model->saveRequestMessage($rcpt_sq, $rsrv_sq, $request_msg);


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

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

    // 예약취소
    public function rsrvcancel()
    {
        try {




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

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


    // 문자발송
    public function sendSms()
    {
        try {
            $rcpt_key = $this->request->getPost('rcpt_key');
            $rcpt_sq = $this->request->getPost('rcpt_sq');
            $rsrv_sq = $this->request->getPost('rsrv_sq');
            $cd = $this->request->getPost('cd');

            $send_phone = "1600-5749";
            $phone = $this->request->getPost('phone');
            $content = $this->request->getPost('content');
            $send_nm = session('usr_nm');

            $data = $this->model->getDetail($rcpt_key);

            $dest_name = "";
            if ($cd == "S7" || $cd == "S14") {
                $dest_name = "(거주인)" . $data['rec_nm'];
            } else {
                $dest_name = "(중개인)" . $data['agent_nm'];
            }

            $this->model->sendSms($phone, "", $send_phone, $send_nm, $dest_name, $rsrv_sq, $rcpt_sq, $cd, $data);

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

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

    // 거주인정보저장
    public function saveRecInfo()
    {
        $lib = new MyUpload();

        try {

            $rcpt_sq = $this->request->getPost('rcpt_sq');
            $rcpt_key = $this->request->getPost('rcpt_key');
            $rsrv_sq = $this->request->getPost('rsrv_sq');

            $rec_tel1 = $this->request->getPost('rec_tel1');
            $rec_tel2 = $this->request->getPost('rec_tel2');
            $rec_tel3 = $this->request->getPost('rec_tel3');
            $rec_tel = $rec_tel1 . '-' . $rec_tel2 . '-' . $rec_tel3;

            $rec_nm = $this->request->getPost('rec_nm');
            $rec_remark = $this->request->getPost('rec_remark');

            $file = $this->request->getFile('rec_file');

            $data = [
                'rcpt_sq' => $rcpt_sq,
                'rsrv_sq' => $rsrv_sq,
                'rcpt_key' => $rcpt_key,
                'rec_tel' => $rec_tel,
                'rec_nm' => $rec_nm,
                'rec_remark' => $rec_remark,
            ];

            if ($file && $file->isValid() && !$file->hasMoved()) {

                $uploadPath = "/upload/result/" . $rsrv_sq . "/";

                $arrUploadfile = [];
                if ($file->isValid() && !$file->hasMoved()) {
                    $uploadData = $lib->do_upload2($file, $uploadPath);

                    if ($uploadData !== false) {
                        $arrUploadfile[] = $uploadData;
                    }
                }

                if (!empty($arrUploadfile)) {
                    foreach ($arrUploadfile as $key => $uploadFile) {
                        $data['file'] = [
                            'orig_name' => $uploadFile['origin_name'],
                            'new_name' => $uploadFile['file_name'],
                            'file_path' => $uploadPath, // 필요에 따라 상대경로로만 저장
                            'ext' => '.' . $uploadFile['ext'],
                            'size' => $file->getSize(),
                        ];
                    }

                }


            }

            $this->model->saveRecInfo($data);

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

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


    // 파일업로드
    public function uploadFile()
    {
        $lib = new MyUpload();
        $common = new Common();

        try {

            $rcpt_key = $this->request->getPost('rcpt_key');
            $rsrv_sq = $this->request->getPost('rsrv_sq');
            $rcpt_sq = $this->request->getPost('rcpt_sq');
            $img_type = $this->request->getPost('img_type');

            $files = $this->request->getFiles();
            $uploadPath = "/upload/result/" . $rsrv_sq . "/";

            if (!isset($files['files'])) {
                return $this->response->setJSON([
                    'success' => false,
                    'msg' => '파일 없음'
                ]);
            }

            $uploadFiles = $files['files'];
            if ($uploadFiles instanceof \CodeIgniter\HTTP\Files\UploadedFile) {
                $uploadFiles = [$uploadFiles];
            }

            // 매물정보 가져오기
            $receipt = $this->model->getDetail($rcpt_key);

            // 워터마크 이미지 조회
            $watermark_info = [];
            if (!empty($receipt) && ($receipt['comp_sq'] ?? '') == '2') {
                $watermark_info = $this->model->getWatermarkList($receipt['rcpt_cpid']);
            }

            $arrUploadfile = [];
            foreach ($uploadFiles as $file) {

                log_message('info', '[Receipt::uploadFile] upload start name={name} size={size} type={type} error={error}:{errorString} path={path}', [
                    'name' => $file->getName(),
                    'size' => $file->getSize(),
                    'type' => $file->getMimeType(),
                    'error' => $file->getError(),
                    'errorString' => $file->getErrorString(),
                    'path' => $uploadPath,
                ]);

                $uploadData = $lib->do_upload2($file, $uploadPath);

                if ($uploadData !== false) {
                    $arrUploadfile[] = $uploadData;
                    log_message('info', '[Receipt::uploadFile] upload success name={name} stored={stored}', [
                        'name' => $file->getName(),
                        'stored' => $uploadData['file_name'] ?? '(unknown)',
                    ]);
                } else {
                    log_message('error', '[Receipt::uploadFile] do_upload2 failed name={name} error={error}:{errorString}', [
                        'name' => $file->getName(),
                        'error' => $file->getError(),
                        'errorString' => $file->getErrorString(),
                    ]);
                }

            }

            $gps_lat = null;
            $gps_lon = null;
            $camDate = null;
            if (!empty($arrUploadfile)) {
                foreach ($arrUploadfile as $key => $uploadFile) {
                    $object_storage_url = $uploadFile['object_storage_url'];
                    $imgWidth = '';
                    $imgHeight = '';

                    if ($img_type !== 'V1') {
                        $base = $uploadFile['base_name'];     // xxxx
                        $dir = rtrim(dirname($uploadFile['object_key']), '/'); // upload/result/*
                        $thumbKey = $dir . '/' . $base . '_thumb.jpg';

                        $imageDataBlob = file_get_contents($object_storage_url);
                        $im = new \Imagick();
                        $im->readImageBlob($imageDataBlob);
                        $imgWidth = $im->getImageWidth();
                        $imgHeight = $im->getImageHeight();
                        $im->thumbnailImage(105, 80, false);
                        $thumb_im = $im->getImageBlob();
                        // 썸네일 s3 전송
                        $lib->upload_object_storage_imagick2($thumbKey, $thumb_im);
                        $im->destroy();
                    }


                    // 워터마크 삽입
                    $watermark_imgs = ['I3', 'I4'];
                    if (in_array($img_type, $watermark_imgs)) {
                        if (!empty($watermark_info)) {
                            $common->watermarking($object_storage_url, $watermark_info, '', $receipt['rcpt_cpid'], $uploadFile['object_key']);
                        }
                    }

                    // 촬영정보 가져오기
                    $arrExifData = @exif_read_data($object_storage_url);
                    if (!is_array($arrExifData)) {
                        $arrExifData = [];
                    }
                    if (!isset($arrExifData['COMPUTED']) || !is_array($arrExifData['COMPUTED'])) {
                        $arrExifData['COMPUTED'] = [];
                    }
                    $notFound = "Unavailable";

                    // Make - 카메라 제조사
                    if (@array_key_exists('Make', $arrExifData)) {
                        $camMake = $arrExifData['Make'];
                    } else {
                        $camMake = $notFound;
                    }

                    // Model - 카메라 모델
                    if (@array_key_exists('Model', $arrExifData)) {
                        $camModel = $arrExifData['Model'];
                    } else {
                        $camModel = $notFound;
                    }

                    // Date - 촬영일시
                    if (@array_key_exists('DateTime', $arrExifData)) {
                        $camDate = $arrExifData['DateTime'];
                    } else {
                        $camDate = $notFound;
                    }

                    // COMPUTED.Height - 세로크기
                    if (@array_key_exists('Height', $arrExifData['COMPUTED'])) {
                        $camHeight = $arrExifData['COMPUTED']['Height'];
                    } else {
                        $camHeight = $notFound;
                    }

                    // COMPUTED.Width - 가로크기
                    if (@array_key_exists('Width', $arrExifData['COMPUTED'])) {
                        $camWidth = $arrExifData['COMPUTED']['Width'];
                    } else {
                        $camWidth = $notFound;
                    }

                    $imageMetaData = "카메라 제조사: " . $camMake;
                    $imageMetaData .= "\n카메라 모델: " . $camModel;
                    $imageMetaData .= "\n촬영일시: " . $camDate;
                    $imageMetaData .= "\n가로: " . $camWidth;
                    $imageMetaData .= "\n세로: " . $camHeight;

                    $metaData = $imageMetaData;

                    /** 
                     * 파일업로드 내용 저장
                     */
                    $uploadParam = [
                        'rcpt_key' => $rcpt_key, // 접수번호
                        'rsrv_sq' => $rsrv_sq, // 예약일련번호
                        'rcpt_sq' => $rcpt_sq, // 접수일련번호
                        // 'gps_lat' => $gps_lat, // latitude
                        // 'gps_lon' => $gps_lon, // longitude
                        'origin_name' => $uploadFile['origin_name'], // 원본파일명
                        'file_name' => $uploadFile['file_name'], // 저장파일명
                        'upload_path' => $uploadPath, // 저장경로
                        'size' => $file->getSize(),
                        'width' => $imgWidth,
                        'height' => $imgHeight,
                        // 'thumb_name' => $base . '_thumb.jpg',
                        'cam_date' => $camDate, // 촬영일
                        'meta_data' => $metaData, // 이미지메타데이터
                        'receipt' => $receipt,
                        'img_type' => $img_type,
                    ];

                    $this->model->saveImg($uploadParam);

                }
            }

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

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

        }
    }

    // 매물사진 일괄 다운로드 (zip)
    public function downloadAllImages()
    {
        $rsrv_sq = $this->request->getGet('rsrv_sq');
        $img_type = $this->request->getGet('img_type') ?? 'I4';

        if (empty($rsrv_sq)) {
            return $this->response->setStatusCode(400)->setBody('rsrv_sq required');
        }

        $images = $this->model->getImageListByType($rsrv_sq, $img_type);
        if (empty($images)) {
            return $this->response->setStatusCode(404)->setBody('No files');
        }

        $zip = new \ZipArchive();
        $zipName = 'images_' . $rsrv_sq . '_' . $img_type . '_' . date('Ymd_His') . '.zip';
        $zipPath = WRITEPATH . 'cache/' . $zipName;

        if ($zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== true) {
            return $this->response->setStatusCode(500)->setBody('Failed to create zip');
        }

        foreach ($images as $index => $img) {
            $filePath = $img['img_path'] . $img['img_filenm'];
            $zipFileName = sprintf('%02d_%s', $index + 1, basename($img['img_filenm']));

            if (($img['cloud_upload_yn'] ?? 'N') === 'Y') {
                $sourceUrl = NCLOUD_OBJECT_STORAGE_URL . $filePath;
                $content = @file_get_contents($sourceUrl);
                if ($content !== false) {
                    $zip->addFromString($zipFileName, $content);
                }
                continue;
            }

            $localPath = FCPATH . ltrim($filePath, '/');
            if (is_file($localPath)) {
                $zip->addFile($localPath, $zipFileName);
                continue;
            }

            $content = @file_get_contents($filePath);
            if ($content !== false) {
                $zip->addFromString($zipFileName, $content);
            }
        }

        $zip->close();

        if (!is_file($zipPath)) {
            return $this->response->setStatusCode(500)->setBody('Zip not found');
        }

        register_shutdown_function(static function () use ($zipPath) {
            if (is_file($zipPath)) {
                @unlink($zipPath);
            }
        });

        return $this->response->download($zipPath, null)->setFileName($zipName);
    }

    // 촬영위치 저장
    public function saveImgLocation()
    {
        try {

            $img_sq = $this->request->getPost('img_sq');
            $rsrv_sq = $this->request->getPost('rsrv_sq');
            $location = $this->request->getPost('location');

            $this->model->saveImgLocation($img_sq, $rsrv_sq, $location);

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

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

    // 업로드파일 삭제
    public function removeUploadFile()
    {

        try {

            $rcpt_sq = $this->request->getPost('rcpt_sq');
            $img_sq = $this->request->getPost('img_sq');

            // 파일정보 조회
            $file = $this->model->getUploadFileInfo($img_sq);
            if (!empty($file)) {
                $lib = new MyUpload();

                $path = $file['img_path'] . "" . $file['img_filenm'];


                $thumb = explode(".", $file['img_filenm']);
                $thumbPath = $file['img_path'] . "" . $thumb[0] . "_thumb." . $thumb[1];

                // if ($file['cloud_upload_yn'] == "Y") {
                //     $path = NCLOUD_OBJECT_STORAGE_URL . $path;
                // }

                $lib->deleteFile($path);
                $lib->deleteFile($thumbPath);
            }

            $this->model->removeUploadFile($rcpt_sq, $img_sq);


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