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


class User extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
    }

    public function login()
    {
        $this->load->view('templates/others/login');
    }

    public function register($submit = '')
    {
        if ($submit) {
            $this->load->model('user');
        } else $this->load->view('templates/register/register');
    }

    public function mobile_auth()
    {
        $this->load->view('templates/others/mobile_auth');
    }

    public function mobile_auth_encode()
    {
        $this->load->library('mobileCertification');
        $this->mobilecertification->mobile_auth_encode();
    }

    public function find($what)
    {
        $this->load->view('templates/others/find-' . $what);
    }

    public function auth($name = '')
    {
        $this->load->library('auth');
        $this->auth->setIsRequest(true)->auth($name);
        if ($this->auth->getResponse()) {
            $this->output
                ->set_status_header(200)
                ->set_content_type('application/json')
                ->set_output(json_encode($this->auth->getResponse()));
        }
    }

    public function logout()
    {
        $this->load->library('session');
        $data = array(
            'userID'    =>     '',
            'userName'  =>     '',
            'is'    =>     false,
        );
        $this->session->unset_userdata($data);
        $this->session->sess_destroy();
        header('location:'. $_SERVER['HTTP_REFERER']);
    }

    public function check_username($username)
    {
        log_message('info', 'checking username ====');
        log_message('info', $username);
        if (count($this->user_model->findByUserName($username)) == 0) {
            $this->output
                ->set_status_header(200)
                ->set_content_type('application/json')
                ->set_output(json_encode(array('code' => 200)));
        } else {
            $this->output
                ->set_status_header(200)
                ->set_content_type('application/json')
                ->set_output(json_encode(array('code' => 404)));
        }
    }

    public function check_email()
    {
        log_message('info', 'checking email ====');
        log_message('info', $this->input->post('email'));
        if (count($this->user_model->findByEmail($this->input->post('email'))) == 0) {
            $this->output
                ->set_status_header(200)
                ->set_content_type('application/json')
                ->set_output(json_encode(array('code' => 200)));
        } else {
            $this->output
                ->set_status_header(200)
                ->set_content_type('application/json')
                ->set_output(json_encode(array('code' => 404)));
        }
    }

    public function submit()
    {
        $params = [];
        foreach (['userName', 'password', 'gender', 'birthday', 'birth_cal', 'email', 'mobile', 'church'] as $k) {
            $params[$k] = $this->input->post($k);
        }
        $params['password'] = md5($params['password']);
        if ($this->user_model->save([$params])) {
            $this->output
                ->set_status_header(200)
                ->set_content_type('application/json')
                ->set_output(json_encode(array('code' => 200)));
        } else {
            $this->output
                ->set_status_header(200)
                ->set_content_type('application/json')
                ->set_output(json_encode(array('code' => 404)));
        }
    }

    public function oauth($sns = '')
    {
        $this->load->library('auth');
        $sns = $sns ? $sns : $this->uri->segment(2);
        $this->auth->auth($sns);
    }

    public function check_session() {
        $this->output
                ->set_status_header(200)
                ->set_content_type('application/json')
                ->set_output(json_encode(array('code' => (getSessionUser()->is? 200: 400))));
    }
}
