<?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('parent/dashboard'));
		}
		$this->load->view('member/login', [
			'base_url' => base_url(),
			'page_title' => '舞台小種籽 - 會員區',
			'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('parent/User_model');
		if ($this->User_model->validation($email, $password)) {
			header('location: ' . base_url('parent/dashboard'));
		} else
			$this->index();
	}


	public function dashboard()
	{
		$this->checkUserSession();
		$this->load->view('member/includes/main', [
			'base_url' => base_url(),
			'page_title' => '舞台小種籽 - 會員區',
			'vendors' => [
				'css' => [],
				'js' => [],
			],
			'class' => $this->router->method,
			'breadcrumb' => [
				[
					'link' => base_url() . 'parent/dashboard',
					'name' => '會員區',
				],
			],
		]);
	}

	public function receipt()
	{
		$this->checkUserSession();
		$this->load->model('parent/user_model');
		$student = $this->user_model->getStudentID();
		$this->load->view('member/includes/main', [
			'base_url' => base_url(),
			'page_title' => '舞台小種籽 - 會員區',
			'vendors' => [
				'css' => [],
				'js' => [],
			],
			'class' => $this->router->method,
			'breadcrumb' => [
				[
					'link' => base_url() . 'parent/dashboard',
					'name' => '會員區',
				],
				[
					'link' => '',
					'name' => '課程記錄',
				],
			],
			'student_id' => $student['student_id'],
			'paid_records' => [],
		]);
	}

	public function nonpaid()
	{
		$this->checkUserSession();
		$this->load->view('member/includes/main', [
			'base_url' => base_url(),
			'page_title' => '舞台小種籽 - 會員區',
			'vendors' => [
				'css' => [],
				'js' => [],
			],
			'class' => $this->router->method,
			'breadcrumb' => [
				[
					'link' => base_url() . 'parent/dashboard',
					'name' => '會員區',
				],
				[
					'link' => '',
					'name' => '筆記記錄',
				],
			],
			'paid_records' => [],
		]);
	}

	public function payment()
	{
		$this->checkUserSession();
		$this->load->view('member/includes/main', [
			'base_url' => base_url(),
			'page_title' => '舞台小種籽 - 會員區',
			'vendors' => [
				'css' => [],
				'js' => [],
			],
			'class' => $this->router->method,
			'breadcrumb' => [
				[
					'link' => base_url() . 'parent/dashboard',
					'name' => '會員區',
				],
				[
					'link' => '',
					'name' => '筆記記錄',
				],
			],
			'paid_records' => [],
		]);
	}

	public function account()
	{
		$this->checkUserSession();
		$this->load->model('parent/User_model');
		$userInfo = $this->User_model->getUser();
		$this->load->view('member/includes/main', [
			'base_url' => base_url(),
			'page_title' => '舞台小種籽 - 會員區',
			'vendors' => [
				'css' => [],
				'js' => [],
			],
			'class' => $this->router->method,
			'breadcrumb' => [
				[
					'link' => base_url() . 'parent/dashboard',
					'name' => '會員區',
				],
				[
					'link' => '',
					'name' => '帳戶資料',
				],
			],
			'userInfoObject' => $userInfo,
		]);
	}
	public function checkUserSession()
	{
		if (!$this->session->has_userdata('user_info')) {
			header('location: ' . base_url('parent'));
		}
	}

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

		$this->load->model('parent/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' => '舞台小種籽 - 會員區',
				'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() . 'parent');
		}
		else {
			$this->load->model('User_Model', 'user');
			if ($this->user->forget_password($request['email'])) {
				$this->load->view('parent/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' => '舞台小種籽 - 會員區',
					'vendors' => [
						'css' => [],
						'js' => [],
					],
					'class' => $this->router->method,
				]);
			}
		}
	}

	public function logout(){
	
		
		$this->session->sess_destroy();
		redirect(base_url('parent'));
	}
}
