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

class Member extends CI_Controller
{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{
		if ($this->session->has_userdata('user_info')) {
			header('location: ' . base_url('member/dashboard'));
		}
		$this->load->view('member/login', [
			'base_url' => base_url(),
			'page_title' => 'Jonson - 会员区',
			'vendors' => [
				'css' => [],
				'js' => [],
			],
			'class' => $this->router->method,
		]);
	}

	public function checkuser()
	{
		$email = $this->input->post("email");
		$password = $this->input->post("password");

		// for debug use
		/* echo "<pre>";
		var_dump($this->input->post());
		echo "</pre>"; */



		$this->load->model('member/User_model');
		if ($this->User_model->validation($email, $password)) {
			header('location: ' . base_url('member/dashboard'));
		} else
			$this->index();
	}


	public function dashboard()
	{
		$this->checkUserSession();
		$this->load->view('member/includes/main', [
			'base_url' => base_url(),
			'page_title' => 'Jonson - 会员区',
			'vendors' => [
				'css' => [],
				'js' => [],
			],
			'class' => $this->router->method,
			'breadcrumb' => [
				[
					'link' => base_url() . 'member/dashboard',
					'name' => '会员区',
				],
			],
		]);
	}

	public function history()
	{
		$this->checkUserSession();
		$this->load->view('member/includes/main', [
			'base_url' => base_url(),
			'page_title' => 'Jonson - 会员区',
			'vendors' => [
				'css' => [],
				'js' => [],
			],
			'class' => $this->router->method,
			'breadcrumb' => [
				[
					'link' => base_url() . 'member/dashboard',
					'name' => '会员区',
				],
				[
					'link' => '',
					'name' => '会员书柜',
				],
			],
			'paid_records' => [],
		]);
	}

	public function bookshell()
	{
		$this->checkUserSession();
		$this->load->view('member/includes/main', [
			'base_url' => base_url(),
			'page_title' => 'Jonson - 会员区',
			'vendors' => [
				'css' => [],
				'js' => [],
			],
			'class' => $this->router->method,
			'breadcrumb' => [
				[
					'link' => base_url() . 'member/dashboard',
					'name' => '会员区',
				],
				[
					'link' => '',
					'name' => '会员书柜',
				],
			],
			'paid_records' => [],
		]);
	}

	public function account()
	{
		$this->checkUserSession();
		$this->load->model('member/User_model');
		$userInfo = $this->User_model->getUser();
		$this->load->view('member/includes/main', [
			'base_url' => base_url(),
			'page_title' => 'Jonson - 会员区',
			'vendors' => [
				'css' => [],
				'js' => [],
			],
			'class' => $this->router->method,
			'breadcrumb' => [
				[
					'link' => base_url() . 'member/dashboard',
					'name' => '会员区',
				],
				[
					'link' => '',
					'name' => '帐户资料',
				],
			],
			'userInfoObject' => $userInfo,
		]);
	}
	public function checkUserSession()
	{
		if (!$this->session->has_userdata('user_info')) {
			header('location: ' . base_url('member'));
		}
	}

	public function changeAccountInformation()
	{
		$this->checkUserSession();

		$this->load->model('member/User_model');
		$this->User_model->editAccountInformation($this->input->post());
		$this->load->view('member/account/updated');

	}

	public function forget_password() {
		$this->load->view('member/forget_password', [
			'base_url' => base_url(),
			'page_title' => 'Jonson - 会员区',
			'vendors' => [
				'css' => [],
				'js' => [],
			],
			'class' => $this->router->method,
		]);
	}

	public function send_password() {
		$request = $this->input->post();
		if (! array_key_exists('email', $request)) {
			header('location: ' . base_url() . 'member');
		}
		else {
			$this->load->model('User_Model', 'user');
			if ($this->user->forget_password($request['email'])) {
				$this->load->view('member/forget_password_success', [
					'base_url' => base_url(),
					'page_title' => '舞台小种籽 - 会员区',
					'vendors' => [
						'css' => [],
						'js' => [],
					],
					'class' => $this->router->method,
				]);
			}
			else {
				$this->load->view('member/forget_password_fail', [
					'base_url' => base_url(),
					'page_title' => 'Jonson - 会员区',
					'vendors' => [
						'css' => [],
						'js' => [],
					],
					'class' => $this->router->method,
				]);
			}
		}
	}

	public function logout(){


		$this->session->sess_destroy();
		redirect(base_url('member'));
	}
}
