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

class BrservicesModel extends CI_Model {

	function __construct() {
		parent::__construct();
		date_default_timezone_set('Asia/Hong_Kong');
	}
	//new added services
	function getAllServices() {
		$this->db->from('brservices');
		$this->db->where('delete_time', NULL);
		$this->db->where('type', 'product');
		$this->db->order_by('code');
		$query = $this->db->get();
		return $query->result();
	}
	function getServices($id) {
		$this->db->from('brservices');
		$this->db->where('delete_time', NULL);
		$this->db->where('type', 'product');
		$this->db->where('id', $id);
		// $this->db->order_by('code');
		$query = $this->db->get();
		$ret = $query->row();
		return $ret;
	}
	function serviceConvert($num) {
		//工數convert
		//0=0
		//1=半工
		//2=1工
		//3=0.2工
		//4=0.3工
		if ($num == 0) {
			return 0;
		}
		if ($num == 1) {
			return 0.5;
		}
		if ($num == 2) {
			return 1;
		}
		if ($num == 3) {
			return 0.2;
		}
		if ($num == 4) {
			return 0.3;
		}
		//added modified at 2021-03-24
		if ($num == 5) {
			return 2;
		}
		//end added modified at 2021-03-24

		//added modified at 2021-04-09
		if ($num == 6) {
			return 0.1;
		}
		if ($num == 7) {
			return 0.25;
		}
		if ($num == 8) {
			return 1.5;
		}
		//end added modified at 2021-04-09

	}
	function getFTServices() {
		$this->db->from('brservices');
		$this->db->where('delete_time', NULL);
		$this->db->where('category_id', 1);
		$this->db->where('type', 'product');
		$this->db->order_by('code');
		$query = $this->db->get();
		return $query->result();
	}
	function getTServices() {
		$this->db->from('brservices');
		$this->db->where('delete_time', NULL);
		$this->db->where('category_id', 2);
		$this->db->where('type', 'product');
		$this->db->order_by('code');
		$query = $this->db->get();
		return $query->result();
	}
	function services_exists($code) {
		// $this->db->from('adminUsers');
		// $this->db->where('LOWER(name)', $name);
		$this->db->where('LOWER(code)', strtolower($code));
		$this->db->where('delete_time', NULL);
		$this->db->where('type', 'product');
		$query = $this->db->get('brservices');
		if ($query->num_rows() == 1) {
			return false;
		} else {
			return true;
		}
	}
	function services_existsbyid($code, $id) {
		// $this->db->from('adminUsers');
		// $this->db->where('LOWER(name)', $name);
		$this->db->where('LOWER(code)', strtolower($code));
		$this->db->where('delete_time', NULL);
		$this->db->where('type', 'product');
		$this->db->where('id !=', $id);
		$query = $this->db->get('brservices');
		if ($query->num_rows() >= 1) {
			return false;
		} else {
			return true;
		}
	}
	function insertAdmin($data) {
		// 'inputadminname'=>$inputadminname,
		//         'inputloginname'=>$inputloginname,
		//         'inputPassword'=>$inputPassword,
		$data = array(
			'code' => $data['code'],
			'name' => $data['name'],
			'description' => $data['description'],
			'work' => $data['work'],
			'price' => $data['price'],
			// 'fullName' => $data['inputadminname'] ,
			'time' => $data['time'],
			'time_type' => $data['time_type'],
			'category_id' => $data['category_id'],
			// 'no_credit_rebate' => $data['no_credit_rebate'],
			'status' => $data['status'],
			'display' => $data['display'],
			'remark' => $data['remark'],
			'service_type' => $data['service_type'],
			'type' => 'product',
			'point' => $data['point'],
			'direct_to_point' => $data['direct_to_point'],
		);
		date_default_timezone_set('UTC');

		$this->db->set('update_time', time(), FALSE);
		$this->db->set('create_time', time(), FALSE);
		// $this->db->set('update_time', 'NOW()', FALSE);
		// $this->db->set('create_time', 'NOW()', FALSE);
		$query = $this->db->insert('brservices', $data);
		return $query;
	}
	function updateAdmin($data) {
		$data2 = array(
			'code' => $data['code'],
			'name' => $data['name'],
			'description' => $data['description'],
			'work' => $data['work'],
			'price' => $data['price'],
			// 'fullName' => $data['inputadminname'] ,
			'time' => $data['time'],
			'time_type' => $data['time_type'],
			'category_id' => $data['category_id'],
			// 'no_credit_rebate' => $data['no_credit_rebate'],
			'status' => $data['status'],
			'display' => $data['display'],
			'remark' => $data['remark'],
			'service_type' => $data['service_type'],
			'type' => 'product',
			'point' => $data['point'],
			'direct_to_point' => $data['direct_to_point'],

		);
		date_default_timezone_set('UTC');

		$this->db->set('update_time', time(), FALSE);

		$this->db->where('id', $data['id']);
		$query = $this->db->update('brservices', $data2);
		return $query;

	}

	function getCategoryid($id) {
		$this->db->from('brservices');
		$this->db->where('id', $id);
		$category_id = $this->db->get()->row()->category_id;
		return $category_id;
	}
	function updateStatus($data) {
		$data2 = array(
			'status' => $data['status'],
		);
		date_default_timezone_set('UTC');

		$this->db->set('update_time', time(), FALSE);

		$this->db->where('id', $data['id']);
		$query = $this->db->update('brservices', $data2);
		return $query;
	}
	function deleteService($id) {
		date_default_timezone_set('UTC');

		$this->db->set('delete_time', time(), FALSE);
		$this->db->where('id', $id);
		$query = $this->db->update('brservices');
		return $query;
	}
	function isDirectToPoint($id) {
		$this->db->select('direct_to_point');
		$this->db->from('brservices');
		$this->db->where('id', $id);
		$direct_to_point = $this->db->get()->row()->direct_to_point;
		$isDirectToPoint = false;
		if ($direct_to_point == 1) {
			$isDirectToPoint = true;
		}
		return $isDirectToPoint;

	}
	//end new added services

	function getBannerAll() {

		$this->db->from('frontendUsers');
		// $this->db->where(array('show' => 1));
		// $this->db->order_by('rank');
		$query = $this->db->get();
		return $query->result();
	}

	function checking() {

		$userid = $this->session->userdata('userid');
		$username = $this->session->userdata('username');
		$fullName = $this->session->userdata('fullName');
		$logged_in = $this->session->userdata('logged_in');
		$secret = $this->session->userdata('secret');
		$usertype = $this->session->userdata('usertype');

		$passLogin = false;
		if ($usertype == "frontenduser") {
			if ($logged_in == false) {
				return false;
			}
			if (empty($userid)) {
				return false;
			}
			if (empty($secret)) {
				return false;
			}
			$this->db->where('id', $userid);
			$this->db->where('secret', $secret);
			if ($this->db->get('frontendUsers')->row_array()) {
				$passLogin = true;
				return true;
			} else {
				return false;
			}

		} //if($usertype=="frontenduser"){
		if ($usertype == "backenduser") {
			if ($logged_in == false) {
				return false;
			}
			if (empty($userid)) {
				return false;
			}
			if (empty($secret)) {
				return false;
			}
			$this->db->where('id', $userid);
			$this->db->where('secret', $secret);
			if ($this->db->get('adminUsers')->row_array()) {
				$passLogin = true;
				return true;
			} else {
				return false;
			}

		}
	}
}