<?php
namespace App\Models\manage;

use CodeIgniter\Model;

class ScomplexModel extends Model
{


    public function getCodeList()
    {
        $sql = "SELECT  * FROM codes WHERE category = 'ARTICLE_TYPE' ";

        $query = $this->db->query($sql);

        return $query->getResultArray();
    }


    public function getTotalCount($data)
    {
        $params = [];

        $sql = "SELECT COUNT(*) AS cnt
                FROM scomplex_manage AS sm            
                LEFT JOIN codes AS c ON c.cd = sm.codes AND c.category = 'ARTICLE_TYPE' ";

        $sql .= "WHERE 1=1 ";

        if (!empty($data['name'])) {
            $sql .= "AND sm.sm_name LIKE CONCAT('%', ?, '%') ";
            array_push($params, $data['name']);
        }

        if (!empty($data['apporval_date'])) {
            $sql .= "AND sm.sm_apporval_date >= DATE(?) ";
            array_push($params, $data['apporval_date']);
        }

        if (!empty($data['end_date'])) {
            $sql .= "AND sm.sm_end_date <= DATE(?) ";
            array_push($params, $data['end_date']);
        }

        if (!empty($data['code'])) {
            $sql .= "AND sm.sm_code LIKE CONCAT('%', ?, '%') ";
            array_push($params, $data['code']);
        }

        if (!empty($data['cd'])) {
            $sql .= "AND sm.codes = ? ";
            array_push($params, $data['cd']);
        }

        if (!empty($data['address'])) {
            $sql .= "AND sm.sm_address LIKE CONCAT('%', ?, '%') ";
            array_push($params, $data['address']);
        }

        $query = $this->db->query($sql, $params ?: []);

        return $query->getRow()->cnt;
    }

    public function getScomplexList($start, $end, $data)
    {

        $params = [];

        $sql = "SELECT
                    sm.sm_seq AS sm_seq, 
                    sm.sm_name AS sm_name, 
                    sm.sm_code AS sm_code, 
                    sm.sm_address AS sm_address,
                    c.cd AS cd , 
                    c.cd_nm AS cd_nm , 
                    sm.sm_apporval_date AS sm_apporval_date, 
                    sm.sm_end_date AS sm_end_date, 
                    sm.sm_memo AS sm_memo, 
                    sm.insert_tm AS insert_tm,
                    sm.update_tm AS update_tm,
                    sm.insert_usr AS insert_usr,
                    sm.update_usr AS update_usr
                FROM
                    scomplex_manage AS sm            
                LEFT JOIN
                    codes AS c ON c.cd = sm.codes AND c.category = 'ARTICLE_TYPE' ";

        $sql .= "WHERE 1=1 ";

        if (!empty($data['name'])) {
            $sql .= "AND sm.sm_name LIKE CONCAT('%', ?, '%') ";
            array_push($params, $data['name']);
        }

        if (!empty($data['apporval_date'])) {
            $sql .= "AND sm.sm_apporval_date >= DATE(?) ";
            array_push($params, $data['apporval_date']);
        }

        if (!empty($data['end_date'])) {
            $sql .= "AND sm.sm_end_date <= DATE(?) ";
            array_push($params, $data['end_date']);
        }

        if (!empty($data['code'])) {
            $sql .= "AND sm.sm_code LIKE CONCAT('%', ?, '%') ";
            array_push($params, $data['code']);
        }

        if (!empty($data['cd'])) {
            $sql .= "AND sm.codes = ? ";
            array_push($params, $data['cd']);
        }

        if (!empty($data['address'])) {
            $sql .= "AND sm.sm_address LIKE CONCAT('%', ?, '%') ";
            array_push($params, $data['address']);
        }

        $sql .= "ORDER BY sm.insert_tm DESC ";

        $sql .= "LIMIT ?, ? ";

        $params[] = (int) $start;
        $params[] = (int) $end;

        $query = $this->db->query($sql, $params ?: []);

        return $query->getResultArray();

    }

    public function insertScomplex($data)
    {
        $sql = "INSERT INTO scomplex_manage ";
        $sql .= "(sm_name, sm_code, sm_address, codes, sm_apporval_date, sm_end_date, sm_memo, insert_tm, insert_usr) ";
        $sql .= "VALUES ";
        $sql .= "(?, ?, ?, ?, ?, ?, ?, NOW(), ?)";

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

        if ($this->db->transStatus() === false) {
            return [
                'success' => false,
                'msg' => '저장실패',
            ];
        }

        // 성공
        return [
            'success' => true,
        ];

    }

    public function updateScomplex($data)
    {
        $sql = "UPDATE scomplex_manage SET ";
        $sql .= "sm_name = ?, 
                sm_code = ?, 
                sm_address = ?, 
                codes = ?, 
                sm_apporval_date = ?, 
                sm_end_date = ?, 
                sm_memo = ?, 
                update_tm = ?, 
                update_usr = NOW() ";

        $sql .= "WHERE sm_seq = ?";

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

        if ($this->db->transStatus() === false) {
            return [
                'success' => false,
                'msg' => '저장실패',
            ];
        }

        // 성공
        return [
            'success' => true,
        ];
    }

    public function getExcelScomplexList($data)
    {

        $params = [];

        $sql = "SELECT
                    sm.sm_name AS '단지명', 
                    sm.sm_code AS '단지코드', 
                    sm.sm_address AS '단지주소',
                    c.cd_nm AS '매물종류' , 
                    sm.sm_apporval_date AS '사용승인일', 
                    sm.sm_end_date AS '승인종료일', 
                    sm.sm_memo AS '메모', 
                    sm.insert_tm AS '등록일'
                FROM
                    scomplex_manage AS sm            
                LEFT JOIN
                    codes AS c ON c.cd = sm.codes AND c.category = 'ARTICLE_TYPE' ";

        $sql .= "WHERE 1=1 ";

        if (!empty($data['name'])) {
            $sql .= "AND sm.sm_name LIKE CONCAT('%', ?, '%') ";
            array_push($params, $data['name']);
        }

        if (!empty($data['apporval_date'])) {
            $sql .= "AND sm.sm_apporval_date >= DATE(?) ";
            array_push($params, $data['apporval_date']);
        }

        if (!empty($data['end_date'])) {
            $sql .= "AND sm.sm_end_date <= DATE(?) ";
            array_push($params, $data['end_date']);
        }

        if (!empty($data['code'])) {
            $sql .= "AND sm.sm_code LIKE CONCAT('%', ?, '%') ";
            array_push($params, $data['code']);
        }

        if (!empty($data['cd'])) {
            $sql .= "AND sm.codes = ? ";
            array_push($params, $data['cd']);
        }

        if (!empty($data['address'])) {
            $sql .= "AND sm.sm_address LIKE CONCAT('%', ?, '%') ";
            array_push($params, $data['address']);
        }

        $sql .= "ORDER BY sm.insert_tm DESC ";


        $query = $this->db->query($sql, $params ?: []);

        return $query->getResultArray();
    }

}