<?php

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


class Login extends CI_Controller
{
    public function __construct() 
    {
        parent::__construct();
        $this->load->model('Operator_model');
    }

    public function index($client_ip=false)
    {
        if ($this->session->userdata('login_referer')==false) {
            $this->session->set_userdata('login_referer', site_url('index/index'));
        } 

        //SESSION没过期的话直接登录进去
        if ($this->session->userdata('admin_chtcdn') !== false) {
            redirect($this->session->userdata('login_referer'));
        }

        $ip=$this->Operator_model->get_client_ip();

        //内部网络直接用IP登录
        if ($client_ip!==false) 
        {
            // 用IP自动登录
            $client_ip=pack("H*",$client_ip);
            $webht_user=$this->Operator_model->get_webhtuser_by_ip($client_ip,'*');
            if (!empty($webht_user)) {
                $OPI_Name=preg_replace("/\(.*\)/", '', $webht_user->whu_uname);
                $userdata=$this->Operator_model->get_user_by_name(trim($OPI_Name));
                if (!empty($userdata)) {
                    foreach ($webht_user as $key=>$whu) {
                        $userdata[$key]=$whu;
                    }
                    $this->session->set_userdata('admin_chtcdn', $userdata);
                    redirect($this->session->userdata('login_referer'));
                }
            }
        }
        //办公室用户
        else
        {
            $webht_user=$this->Operator_model->get_webhtuser_by_ip($ip,'*');
            //在办公室访问的用户，跳转到内部网络获取IP
            if ($ip=='221.7.151.39') 
            {
                redirect('http://202.103.68.123:65001/webht.php/login/set_inner_ip');
            }
            //直接访问内部网络地址的用IP登录
            elseif (!empty($webht_user)) 
            {
                $OPI_Name=preg_replace("/\(.*\)/", '', $webht_user->whu_uname);
                $userdata=$this->Operator_model->get_user_by_name(trim($OPI_Name));
                if (!empty($userdata)) {
                    foreach ($webht_user as $key=>$whu) {
                        $userdata[$key]=$whu;
                    }
                    $this->session->set_userdata('admin_chtcdn', $userdata);
                    redirect($this->session->userdata('login_referer'));
                }
            } 
        }

        //外网用户及不能识别用户手动登录
        $data=array();
        $data['navtitle']='登陆';
        $this->load->view('login/login', $data);
        $this->load->view('footer');
    }

    //登录操作
    public function sign_in() 
    {
        $this->form_validation->set_rules('user_code', 'lang:login_user_code', 'required');
        $this->form_validation->set_rules('password', 'lang:login_password', 'required');
        if ($this->form_validation->run() == FALSE) 
        {
            $data = array();
            $data = array('status' => 'no', 'msg' => '用户名或者密码不正确！');
            echo json_encode($data);
            return FALSE;
        }

        $check_login = $this->Operator_model->check_login($this->input->post('user_code'), $this->input->post('password'));
        if ($check_login) 
        {
            $userdata = $this->Operator_model->get_user($this->input->post('user_code'));            
            if (!empty($userdata)) {
                $webht_user=$this->Operator_model->get_webhtuser_by_name(trim($userdata['OPI_Name']),'*');
                if (!empty($webht_user)) {
                    foreach ($webht_user as $key=>$whu) {
                        $userdata[$key]=$whu;
                    }
                }
            }
            $this->session->set_userdata('admin_chtcdn', $userdata);
            $data = array('status' => 'ok_go', 'msg'=>'登陆成功', 'url' => $this->session->userdata('login_referer'));
        }
        else
        {
            $data = array('status' => 'no', 'msg' => '用户名或者密码不正确');
        }
        
        echo json_encode($data);
        return TRUE;
    }

    public function get_app()
    {
        Header("Content-type:application/vnd.android.package-archive"); 
        header('Content-Disposition: attachment; filename="HT在线.apk"'); 
        header('Content-Transfer-Encoding: binary'); 
        readfile('/sc/webht.apk');
    }

     //获取在办公室访问的用户deIP
    public function set_inner_ip()
    {
        $ip=$this->Operator_model->get_client_ip();
        $ip=bin2hex($ip);
        redirect('http://www.chtcdn.com/webht.php/login/index/'.$ip);
    }

    public function logout()
    {
        $this->session->unset_userdata('admin_chtcdn');
        redirect(site_url('login/index'));
    }

}
