<?php
namespace App\Models\manage;

use CodeIgniter\Model;

class LoginLogModel extends Model
{

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

        $sql = "SELECT COUNT(*) AS cnt FROM user_login_log WHERE 1=1 ";
        if (!empty($data["srchTxt"])) {
            $sql .= "AND (
                        usr_id_in LIKE CONCAT('%', ?, '%')
                        OR usr_id_in IN (SELECT usr_id FROM users WHERE usr_nm LIKE CONCAT('%', ?, '%'))
                        OR ip LIKE CONCAT('%', ?, '%')
                        OR useragent LIKE CONCAT('%', ?, '%')
                    ) ";
            $params[] = $data["srchTxt"];
            $params[] = $data["srchTxt"];
            $params[] = $data["srchTxt"];
            $params[] = $data["srchTxt"];
        }

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

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

    }

    public function getLoginLogList($start, $end, $data)
    {
        $sql = "SELECT id, success, usr_sq, usr_id_in, (SELECT usr_nm FROM users WHERE usr_id = usr_id_in) AS usr_nm, ip, useragent, reason, regdate FROM user_login_log ";
        $sql .= "WHERE 1=1 ";

        if (!empty($data["srchTxt"])) {
            $sql .= "AND (
                        usr_id_in LIKE CONCAT('%', ?, '%')
                        OR usr_id_in IN (SELECT usr_id FROM users WHERE usr_nm LIKE CONCAT('%', ?, '%'))
                        OR ip LIKE CONCAT('%', ?, '%')
                        OR useragent LIKE CONCAT('%', ?, '%')
                    ) ";
            $params[] = $data["srchTxt"];
            $params[] = $data["srchTxt"];
            $params[] = $data["srchTxt"];
            $params[] = $data["srchTxt"];
        }

        $sql .= "ORDER BY id DESC ";
        $sql .= "LIMIT ?, ?";

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

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

        return $query->getResultArray();
    }

    // 엑셀 다운로드 조회
    public function getExcelDownList($data)
    {
        $params = [];

        $sql = "SELECT 
                usr_id_in AS '입력아이디', 
                (SELECT usr_nm FROM users WHERE usr_id = usr_id_in) AS '사용자명', 
                ip AS '아이피', useragent AS '사용자 에이전트', 
                reason AS '사유', 
                regdate AS '접속일시' 
                FROM user_login_log ";
        $sql .= "WHERE 1=1 ";

        if (!empty($data["srchTxt"])) {
            $sql .= "AND (
                        usr_id_in LIKE CONCAT('%', ?, '%')
                        OR usr_id_in IN (SELECT usr_id FROM users WHERE usr_nm LIKE CONCAT('%', ?, '%'))
                        OR ip LIKE CONCAT('%', ?, '%')
                        OR useragent LIKE CONCAT('%', ?, '%')
                    ) ";
            $params[] = $data["srchTxt"];
            $params[] = $data["srchTxt"];
            $params[] = $data["srchTxt"];
            $params[] = $data["srchTxt"];
        }

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

        return $query->getResultArray();
    }
}