<?php

use app\libraries\CommonHelper;
use app\libraries\DateHelper;
use app\libraries\Tenants;
use app\libraries\Userstamp;

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

/**
 * @property Student_parent_model $Student_parent_model
 * @property Parent_model $Parent_model
 */
class User_model extends MY_Model
{

    public function __construct()
    {
        parent::__construct();
    }

    public function validation($email, $password)
	{
		$user = $this->db->where(['username' => $email, 'password' => sha1($password)])
			->get($this->table_prefix . 'parent')->row_array();
		if (is_null($user)) {
			return false;
		}
		$this->session->set_userdata('user_info', [
			'user_id' => $user['id'],
			'chi_name' => $user['chi_name'],
		]);
		return true;
	}

	public function getUser()
	{
		return $this->db->where(['id' => $this->session->get_userdata('user_info')['user_info']['user_id']])
			->get($this->table_prefix . 'parent')->row_array();
	}

	public function editAccountInformation($params)
	{
		$data = [
			'chi_name' => $params['chi_name'],
			'eng_name' => $params['eng_name'],
			'email' => $params['email'],
			'mobile' => $params['phone'],
		];
		if (array_key_exists('password', $params)) {
			if (trim($params['password']) != '') {
				$data['password'] = sha1($params['password']);
			}
		}
		$this->db->set('update_time', 'NOW()', false);
		$this->db->where(['id' => $this->session->get_userdata('user_info')['user_info']['user_id']])
			->update($this->table_prefix . 'parent', $data);
	}

	public function getStudentID()
	{
		return $this->db->where(['parent_id' => $this->session->get_userdata('user_info')['user_info']['user_id']])
			->from($this->table_prefix . 'student_parent')
			->get()->row_array();
	}
}
