<?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(['email' => $email, 'password' => sha1($password)])
			->get($this->table_prefix . 'member')->row_array();
		if (is_null($user)) {
			return false;
		}
		$this->session->set_userdata('user_info', [
			'user_id' => $user['id'],
			'name' => $user['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 . 'member')->row_array();
	}

	public function editAccountInformation($params)
	{
		$data = [
			'name' => $params['name'],
			'email' => $params['email'],
			'phone' => $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 . 'member', $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();
	}
}
