<?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()
    {
        $data=array();
        if ($this->session->userdata('admin_another') !== false) {
            redirect(site_url($_SERVER['HTTP_REFERER']));
        }
        $this->load->view('header', $data);
        $this->load->view('/public/login');
        $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();
            foreach ($this->form_validation->_error_array as $key => $value) {
                $data[] = array('name' => $key, 'value' => $value);
            }
            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'));
            $this->session->set_userdata('admin_another', $userdata);
            $data[] = array('name' => 'go', 'value' => site_url('apps/partners/index/index'));
        }
        else
        {
            $data[] = array('name' => 'login_warning', 'value' => $this->lang->line('login_warning'));
        }        

        echo json_encode($data);
        return TRUE;
    }

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

}
