<?php
namespace App\Models\common;

use CodeIgniter\Model;

class LoginModel extends Model
{

    // 로그인 유저 체크
    public function getUserByIdPw($userId, $userPw)
    {
        $sql = "SELECT a.usr_sq, a.usr_id, a.usr_nm, a.usr_pw, b.dept_sq, b.dept_nm, SHA2(?,256) chk_pw, a.usr_level, b.depth, c.dept_sq bonbu_sq, c.dept_nm bonbu_nm, a.usr_tel1, a.usr_tel2" .
            "	, CASE WHEN HOUR(TIMEDIFF(NOW(), a.last_login_tm)) < 4 THEN 'X' ELSE a.sms_auth_yn END sms_auth_yn" .	//인증후 4시간 이내면 X
            " FROM users a" .
            " INNER JOIN departments b ON b.dept_sq = a.dept_sq AND b.use_yn = 'Y'" .
            " LEFT JOIN departments c ON c.lft <= b.lft AND c.rgt >= b.rgt AND c.depth = 1 AND c.use_yn = 'Y'" .
            " WHERE a.usr_id = ? ";

        $data = array(
            $userPw,
            $userId
        );

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

        return $row;
    }

    public function insertUserLog($data)
    {
        $sql = "INSERT INTO user_login_log 
                ( success , usr_sq , usr_id_in , ip , useragent, regdate, reason )   
                VALUES
                (?, ?, ?, ?, ?, NOW(), ?)              
                ";

        $params = [
            $data['results'],
            $data['usr_sq'],
            $data['usr_id'],
            $data['userIp'],
            $data['userAgent'],
            $data['reason'],
        ];

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

    }

}