<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MX_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->model("user_model");
    }

    function index()
    {
        $this->template->set_template('login');
        $this->template->write_view('content', 'login', TRUE);
        $this->template->write_view('level_script', 'login/loginscript');
        $this->template->render();
    }

    function checkLogin()
    {

        $username = $this->input->post('username');
        $password = $this->input->post('password');

        if (empty($username) && empty($password)) return NULL;

        $user = $this->user_model
            ->where(array("user_username"=>$username,"user_password"=>md5($password)))
            ->get();

//        pre($user);exit;

        if (isset($user)) {
            $this->setInfoUser($user);
            $this->updateLastLogin($user);
            echo 200;
        } else {
            echo 100;
        }
    }

    function setInfoUser($member = '')
    {
        if (empty($member)) return NULL;

        if ($member->user_username != "")
            setcookie(getcookiename('user'), $member->user_username, time() + COOKIE_TIME, '/');    //  (60*60*24*100) 100 วัน

        if ($member->user_lastlogin != "")
            setcookie(getcookiename('userlastlogin'), $member->user_lastlogin, time() + COOKIE_TIME, '/');

        if ($member->user_langid != "")
            setcookie(getcookiename('userlang'), $member->user_langid, time() + COOKIE_TIME, '/');
        else
            setcookie(getcookiename('userlang'), 2, time() + COOKIE_TIME, '/');

                if ($member->user_id != "")
            setcookie(getcookiename('userid'), $member->user_id, time() + COOKIE_TIME, '/');

    }

    function updateLastLogin($member = '')
    {
        $data = array('user_lastlogin' => date('Y-m-d H:i:s'));
        $this->user_model->update($data, $member->user_id);
    }

    function logout()
    {

        $this->clearAllCookie();
        echo "<script>window.location.href='" . base_url() . "login'</script>";


    }

    function clearAllCookie()
    {
        if (isset($_SERVER['HTTP_COOKIE'])) {
            $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
            foreach ($cookies as $cookie) {
                $parts = explode('=', $cookie);
                $name = trim($parts[0]);
                setcookie($name, '', time() - (COOKIE_TIME) * 2, "/");      // 1,000 วัน
            }
        }
    }

    function addTimeCookie()
    {
        foreach ($_COOKIE as $key => $value) {
            if (@$_COOKIE[getcookiename('user')] != "")
                setcookie(getcookiename('user'), @$_COOKIE[getcookiename('user')], time() + COOKIE_TIME, '/');
            if (@$_COOKIE[getcookiename('userid')] != "")
                setcookie(getcookiename('userid'), @$_COOKIE[getcookiename('userid')], time() + COOKIE_TIME, '/');
            if (@$_COOKIE[getcookiename('userlastlogin')] != "")
                setcookie(getcookiename('userphoto'), @$_COOKIE[getcookiename('userlastlogin')], time() + COOKIE_TIME, '/');
            if (@$_COOKIE[getcookiename('userlang')] != "")
                setcookie(getcookiename('userlang'), @$_COOKIE[getcookiename('userlang')] , time() + COOKIE_TIME, '/');

        }
    }

    function logpage()
    {
        $this->load->view('login/logpage');
    }

}

?>
