<?php
namespace App\Models\results;

use CodeIgniter\Model;

class M410Model extends Model
{
    public function getCodeList($data)
    {
        $builder = $this->db->table('codes')
            ->select('category, cd, cd_nm, use_yn')
            ->whereIn('category', $data)
            ->where('use_yn', 'Y')
            ->orderBy('category', 'ASC')
            ->orderBy('view_odr', 'ASC');

        // if ($useYn !== null && $useYn !== '') {
        //     $builder->where('use_yn', $useYn);
        // }

        $rows = $builder->get()->getResultArray();

        $codes = [];
        foreach ($rows as $row) {
            $codes[$row['category']][] = [
                'cd' => $row['cd'],
                'cd_nm' => $row['cd_nm'],
            ];
        }

        return $codes;
    }

    public function getDepart()
    {
        $sql = "SELECT * from departments " .
            "WHERE pdept_sq = 3 " .
            "ORDER BY dept_nm";

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

        return $query->getResultArray();
    }

    public function getTotalCount($data)
    {
        $sql = "SELECT COUNT(*) AS cnt FROM ( ";
        $sql .= "SELECT 
                    a.usr_sq, a.usr_id, a.usr_nm, IFNULL(c.mobile_upload, 0)mobile_upload , b.*
                FROM users AS a
                LEFT JOIN (
                    SELECT CASE a.insert_user WHEN 0 THEN 1 ELSE a.insert_user END insert_user, 
                        COUNT(*) total_cnt
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '10' THEN a.vr_sq ELSE NULL END ) receipt_cnt
                        , COUNT( DISTINCT CASE WHEN b.stat_cd = '10' THEN a.vr_sq ELSE NULL END ) notassign_cnt
                        , COUNT( DISTINCT CASE WHEN b.stat_cd = '10' THEN NULL ELSE a.vr_sq END ) assign_cnt
                        , COUNT( DISTINCT CASE WHEN b.stat_cd = '19' THEN a.vr_sq ELSE NULL END ) cancel_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '30' THEN a.vr_sq ELSE NULL END ) phone_checking_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'D' AND a.stat_cd = '30' THEN a.vr_sq ELSE NULL END ) paper_checking_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '35' THEN a.vr_sq ELSE NULL END ) phone_complete_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'D' AND a.stat_cd = '35' THEN a.vr_sq ELSE NULL END ) paper_complete_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '38' THEN a.vr_sq ELSE NULL END ) phone_all_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '39' THEN a.vr_sq ELSE NULL END ) phone_fail_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'D' AND a.stat_cd = '39' THEN a.vr_sq ELSE NULL END ) paper_fail_cnt
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '40' THEN a.vr_sq ELSE NULL END ) reg_checking_cnt
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '45' THEN a.vr_sq ELSE NULL END ) reg_complete_cnt
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '70' THEN a.vr_sq ELSE NULL END ) reg_none
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '86' THEN a.vr_sq ELSE NULL END ) real_top_RS
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '88' THEN a.vr_sq ELSE NULL END ) real_top_RF
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '87' THEN a.vr_sq ELSE NULL END ) real_top_GS
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '89' THEN a.vr_sq ELSE NULL END ) real_top_GF
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '49' THEN a.vr_sq ELSE NULL END ) reg_fail_cnt
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '60' THEN a.vr_sq ELSE NULL END ) complete_cnt
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '69' THEN a.vr_sq ELSE NULL END ) fail_cnt
                        , COUNT( CASE WHEN (a.stat_cd = '85') THEN a.vr_sq ELSE NULL END ) reg_fail_tempOpen
                        , COUNT( CASE WHEN (a.stat_cd = '77') THEN a.vr_sq ELSE NULL END ) reg_fail_Open
                        , COUNT( DISTINCT CASE WHEN b.stat_cd = '60' AND a.stat_cd = '80' THEN a.vr_sq ELSE NULL END ) same_reg_tempOpen
                        , COUNT( DISTINCT CASE WHEN b.stat_cd = '60' AND a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) same_reg_open
                        , COUNT( distinct c.vr_sq ) AS final_fail_cnt
                    FROM v2_chg_stat_stats a
                    INNER JOIN v2_vrfc_req b ON b.vr_sq = a.vr_sq ";

        if (!empty($data['vrfcreq_way'])) {
            $sql .= "AND b.vrfc_type = '{$data['vrfcreq_way']}' ";
        } else {
            $sql .= "AND b.vrfc_type <> 'N' ";
        }


        $sql .= " LEFT OUTER JOIN v2_article_fail c ON c.vr_sq = a.vr_sq

                    WHERE 1=1 

                    AND a.insert_tm BETWEEN concat('{$data['sdate']} 00:00:00') AND concat('{$data['edate']} 23:59:59')

                    GROUP BY CASE a.insert_user WHEN 0 THEN 1 ELSE a.insert_user END
                ) AS b on b.insert_user = a.usr_sq

                LEFT JOIN (
                            select f1.insert_user, count(distinct f1.vr_sq) mobile_upload
                            from v2_files f1
                            INNER JOIN v2_vrfc_req b ON b.vr_sq = f1.vr_sq and b.vrfc_type = 'M' 
                            where f1.insert_tm between concat('2025-12-16 00:00:00') AND concat('2025-12-16 23:59:59')
                            and f1.file_type = '2'
                            and f1.use_yn = 'Y' group by f1.insert_user
                        ) AS c on c.insert_user = a.usr_sq 
                WHERE (b.insert_user is not null or c.insert_user is not null) ";

        if (!empty($data['dept_sq'])) {
            $sql .= "AND a.dept_sq = {$data['dept_sq']} ";
        }

        $sql .= ") AS t ";

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

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

    public function getResultList($data)
    {
        $sql = "SELECT 
                    a.usr_sq, a.usr_id, a.usr_nm, IFNULL(c.mobile_upload, 0) mobile_upload, b.*
                FROM users AS a
                LEFT JOIN (
                    SELECT CASE a.insert_user WHEN 0 THEN 1 ELSE a.insert_user END insert_user, 
                        COUNT(*) total_cnt
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '10' THEN a.vr_sq ELSE NULL END ) receipt_cnt
                        , COUNT( DISTINCT CASE WHEN b.stat_cd = '10' THEN a.vr_sq ELSE NULL END ) notassign_cnt
                        , COUNT( DISTINCT CASE WHEN b.stat_cd = '10' THEN NULL ELSE a.vr_sq END ) assign_cnt
                        , COUNT( DISTINCT CASE WHEN b.stat_cd = '19' THEN a.vr_sq ELSE NULL END ) cancel_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '30' THEN a.vr_sq ELSE NULL END ) phone_checking_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'D' AND a.stat_cd = '30' THEN a.vr_sq ELSE NULL END ) paper_checking_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '35' THEN a.vr_sq ELSE NULL END ) phone_complete_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'D' AND a.stat_cd = '35' THEN a.vr_sq ELSE NULL END ) paper_complete_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '38' THEN a.vr_sq ELSE NULL END ) phone_all_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'T' AND a.stat_cd = '39' THEN a.vr_sq ELSE NULL END ) phone_fail_cnt
                        , COUNT( DISTINCT CASE WHEN b.vrfc_type = 'D' AND a.stat_cd = '39' THEN a.vr_sq ELSE NULL END ) paper_fail_cnt
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '40' THEN a.vr_sq ELSE NULL END ) reg_checking_cnt
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '45' THEN a.vr_sq ELSE NULL END ) reg_complete_cnt
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '70' THEN a.vr_sq ELSE NULL END ) reg_none
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '86' THEN a.vr_sq ELSE NULL END ) real_top_RS
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '88' THEN a.vr_sq ELSE NULL END ) real_top_RF
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '87' THEN a.vr_sq ELSE NULL END ) real_top_GS
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '89' THEN a.vr_sq ELSE NULL END ) real_top_GF
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '49' THEN a.vr_sq ELSE NULL END ) reg_fail_cnt
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '60' THEN a.vr_sq ELSE NULL END ) complete_cnt
                        , COUNT( DISTINCT CASE WHEN a.stat_cd = '69' THEN a.vr_sq ELSE NULL END ) fail_cnt
                        , COUNT( CASE WHEN (a.stat_cd = '85') THEN a.vr_sq ELSE NULL END ) reg_fail_tempOpen
                        , COUNT( CASE WHEN (a.stat_cd = '77') THEN a.vr_sq ELSE NULL END ) reg_fail_Open
                        , COUNT( DISTINCT CASE WHEN b.stat_cd = '60' AND a.stat_cd = '80' THEN a.vr_sq ELSE NULL END ) same_reg_tempOpen
                        , COUNT( DISTINCT CASE WHEN b.stat_cd = '60' AND a.stat_cd = '76' THEN a.vr_sq ELSE NULL END ) same_reg_open
                        , COUNT( distinct c.vr_sq ) AS final_fail_cnt
                    FROM v2_chg_stat_stats a
                    INNER JOIN v2_vrfc_req b ON b.vr_sq = a.vr_sq ";

        if (!empty($data['vrfcreq_way'])) {
            $sql .= "AND b.vrfc_type = '{$data['vrfcreq_way']}' ";
        } else {
            $sql .= "AND b.vrfc_type <> 'N' ";
        }


        $sql .= " LEFT OUTER JOIN v2_article_fail c ON c.vr_sq = a.vr_sq

                    WHERE 1=1 

                    AND a.insert_tm BETWEEN concat('{$data['sdate']} 00:00:00') AND concat('{$data['edate']} 23:59:59')

                    GROUP BY CASE a.insert_user WHEN 0 THEN 1 ELSE a.insert_user END
                ) AS b on b.insert_user = a.usr_sq

                LEFT JOIN (
                            select f1.insert_user, count(distinct f1.vr_sq) mobile_upload
                            from v2_files f1
                            INNER JOIN v2_vrfc_req b ON b.vr_sq = f1.vr_sq and b.vrfc_type = 'M' 
                            where f1.insert_tm between concat('2025-12-16 00:00:00') AND concat('2025-12-16 23:59:59')
                            and f1.file_type = '2'
                            and f1.use_yn = 'Y' group by f1.insert_user
                        ) AS c on c.insert_user = a.usr_sq 
                WHERE (b.insert_user is not null or c.insert_user is not null) ";

        if (!empty($data['dept_sq'])) {
            $sql .= "AND a.dept_sq = {$data['dept_sq']} ";
        }

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

        return $query->getResultArray();
    }
}