<?php
namespace App\Models\receipt;

use CodeIgniter\Model;

class ReceiptModel extends Model
{

    /**
     * 홍보확인서 fax 목록에서 선택할 경우.
     */
    public function saveFaxImage($faxSq, $rcpt_sq, $rsrv_sq, $img_type, $img_path, $img_filenm, $img_nm, $img_size, $img_width, $img_height, $receipt)
    {
        $this->db->transStart();

        $usr_sq = session('usr_sq');
        $usr_id = session('usr_id');

        $sql = "";
        $data = [];
        $res = "";
        $remark = "";

        $result_img = [];

        if ($img_type == 'I8') {
            $yn_sql = "update receipt " .
                " set parcel_out_yn = 'Y' " .
                " where rcpt_sq = ? ";
            $yn_data = [$rcpt_sq];
            $this->db->query($yn_sql, $yn_data);

            $sql = "update fax_imgs " .
                " set bunyang_yn = 'Y' " .
                " where fax_sq = ? ";
            $data = [$faxSq];
            $this->db->query($sql, $data);
        }

        //홍보확인서, 현장확인 내역서 저장
        if ($img_type == 'I1' || $img_type == 'I2' || $img_type == 'I10') {
            //기존에 있던 이미지는 사용유무 N으로 변경
            $sql = "select img_sq from result_imgs" .
                " WHERE rsrv_sq = ?" .
                "   AND img_type = ?" .
                "   AND use_yn = 'Y'";
            $data = [$rsrv_sq, $img_type];
            $res = $this->db->query($sql, $data);
            $row = $res->getRowArray();
            if (!empty($row)) {
                $this->updateImgSqNullOnFaxImgs($row['img_sq']);		// 홍보확인서 재지정일
            }

            $sql = "UPDATE result_imgs" .
                "   SET use_yn = 'N'" .
                " WHERE rsrv_sq = ?" .
                "   AND img_type = ?" .
                "   AND use_yn = 'Y'";
            $data = [$rsrv_sq, $img_type];
            $res = $this->db->query($sql, $data);

            //새로운 이미지 생성
            $sql = "INSERT INTO result_imgs(rsrv_sq, use_yn, img_type, view_odr, img_path, img_filenm, img_nm, img_size, img_width, img_height, insert_usr, insert_tm)" .
                "VALUES				   (?, 'Y', ?, 1, ?, ?, ?, ?, ?, ?, ?, NOW())";
            $data = [$rsrv_sq, $img_type, $img_path, $img_filenm, $img_nm, $img_size, $img_width, $img_height, $usr_sq];
            $res = $this->db->query($sql, $data);

            if ($img_type == 'I1')
                $remark = "홍보확인서 사진 업로드";
            else if ($img_type == 'I2')
                $remark = "현장확인 내역서 사진 업로드";
            else if ($img_type == 'I10')
                $remark = "촬영동의서 사진 업로드";
            else if ($img_type == 'I11')
                $remark = "체크리스트 사진 업로드";
        } else {
            //이미지 순번 검색
            $odr_sql = "SELECT COALESCE(MAX(view_odr), 0) + 1 as seq FROM result_imgs WHERE rsrv_sq = ? AND img_type = ? AND use_yn = 'Y'";
            $odr_data = [$rsrv_sq, $img_type];
            $query = $this->db->query($odr_sql, $odr_data);
            $row = $query->getRowArray();
            $view_odr = $row['seq'];

            $sql = "INSERT INTO result_imgs(rsrv_sq, use_yn, img_type, view_odr, img_path, img_filenm, img_nm, img_size, img_width, img_height, insert_usr, insert_tm)" .
                "VALUES				   (?, 'Y', ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";
            $data = array(
                $rsrv_sq,
                $img_type,
                $view_odr,
                $img_path,
                $img_filenm,
                $img_nm,
                $img_size,
                $img_width,
                $img_height,
                $usr_sq
            );

            $res = $this->db->query($sql, $data);

            if ($img_type == 'I3')
                $remark = "건물외관 사진 업르도";
            else if ($img_type == 'I4')
                $remark = "내부 사진 업로드";
            else if ($img_type == 'I8')
                $remark = "분양권 사진 업로드";

        }


        $imgSq = $this->db->insertID();
        $this->updateImgSqOnFaxImgs($faxSq, $imgSq);

        if (!$res) {
            return false;
        }

        $this->db->transComplete();

        return true;

    }


    /**
     * 홍보확인서를 재지정했을 경우 기존의 홍보확인서는 선택되지 않음으로 ...
     */
    public function updateImgSqNullOnFaxImgs($imgSq)
    {
        $sql = "UPDATE fax_imgs" .
            " SET img_sq = null" .
            " WHERE img_sq = ?";
        $data = [$imgSq];
        $this->db->query($sql, $data);
    }

    /**
     * FAX이미지를 홍보확인서에서 선택할 경우...
     */
    public function updateImgSqOnFaxImgs($faxSq, $imgSq)
    {
        $sql = "UPDATE fax_imgs" .
            " SET img_sq = ?" .
            " WHERE fax_sq = ?";

        $data = [$imgSq, $faxSq];
        $this->db->query($sql, $data);
    }

    // receipt 에서 지정한 1건만 가져온다.
    public function selectReceiptOne($rcpt_sq)
    {
        $sql = "SELECT a.rcpt_sq,a.comp_sq,a.rcpt_rating,a.rcpt_key,a.rcpt_atclno,a.rcpt_type,a.rcpt_product,a.rcpt_product_nm,a.rcpt_product_area,a.rcpt_product_price,a.rcpt_product_info1,a.rcpt_product_info2,a.rcpt_product_info3,a.rcpt_product_info4,a.rcpt_product_info5,a.rcpt_office,a.rcpt_agent,a.rcpt_sido,a.rcpt_gugun,a.rcpt_dong,a.rcpt_hscp_nm,a.rcpt_dtl_addr,a.rcpt_floor,a.rcpt_bunji,a.rcpt_ho,a.rcpt_tm,a.rcpt_stat,a.rcpt_x,a.rcpt_y,a.rcpt_living_yn,a.cust_nm,a.cust_tel1,a.cust_tel2,a.cust_zip,a.cust_addr1,a.cust_addr2,a.remark,a.agent_id,a.agent_nm,a.agent_head,a.agent_head_tel,a.agent_contact,a.agent_contact_tel,a.agent_fax,a.rsrv_date,a.rsrv_tm_ap,a.insert_usr,a.insert_tm,a.update_usr,a.update_tm,a.svc_type1,a.svc_type2,a.reconf_yn,a.rcpt_exps_type" .
            ", b.region_nm" .
            ", c.rsrv_sq" .
            " FROM receipt a" .
            " LEFT JOIN region_codes b ON b.region_cd = a.rcpt_dong" .
            " LEFT JOIN result c on c.rcpt_sq = a.rcpt_sq" .
            " WHERE a.rcpt_sq = ?" .
            " limit 1";
        $data = [$rcpt_sq];
        $query = $this->db->query($sql, $data);

        return $query->getRowArray();
    }
}