<?php

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

class Brcustomerserviceend extends CI_Controller {

	public function __construct() {
		parent::__construct();
		// $this->load->model('RoomModel', 'room_model');

		// $this->load->model('ServicesModel', 'services_model');
		$this->load->model('BrservicesModel', 'services_model');
		$this->load->model('Brcustomermodel', 'customer_model');
		$this->load->model('BrBookingModel', 'booking_model');
		$this->load->model('BrservicesuserModel', 'servicesuser_model');

	}
	//add customer service end

	public function getService() {
		$cust_id = $this->input->post('cust_id');
		$servicelist = $this->customer_model->getCustomerService($cust_id);

		//new added data
		$data = array();
		foreach ($servicelist as $servicerow) {
			$datarow = array();
			$datarow['id'] = $servicerow->id;
			$datarow['purchased_qty'] = $servicerow->purchased_qty;
			// //calculate available qty
			$available_qty = $servicerow->available_qty;
			$usedQuantity = $this->servicesuser_model->getAvailableServiceUser($servicerow->id);
			if ($usedQuantity != null) {
				$outAvailable_qty = $available_qty - $usedQuantity;
			} else {
				$outAvailable_qty = $available_qty;
			}
			// //end calculate available qty
			// $datarow['available_qty']= $servicerow->available_qty;

			$datarow['available_qty'] = $outAvailable_qty;
			// $datarow['available_qty']= $usedQuantity;
			// $datarow['available_qty']= $servicerow->available_qty;

			$datarow['time'] = $servicerow->time;
			$datarow['code'] = $servicerow->code;
			$datarow['category_id'] = $servicerow->category_id;
			$datarow['name'] = $servicerow->name;
			$datarow['work'] = $this->services_model->serviceConvert($servicerow->work);
			$datarow['price'] = $servicerow->price;
			$datarow['time'] = $servicerow->time;
			$datarow['remark'] = $servicerow->remark;

			$datarow['no_credit_rebate'] = $servicerow->no_credit_rebate;
			//for booking list row
			$datarow['checkedoption'] = 0;
			// foreach ($bookinglist as $bookingerow) {
			//     if($bookingerow->service_user_id==$servicerow->id){
			//         $datarow['checkedoption']=1;
			//     }
			// }//foreach ($bookinglist as $bookingerow) {
			//end for booking list row

			//if(quantity can larger than 0)
			//than show to select in first page
			if ($outAvailable_qty > 0) {
				array_push($data, $datarow);
			}
		} //foreach ($servicelist as $servicerow) {
		//end new added data

		header('Content-Type: application/json');
		echo json_encode($data);
	}
	public function getServiceByBooking() {
		$cust_id = $this->input->post('cust_id');
		$booking_id = $this->input->post('booking_id');

		$servicelist = $this->customer_model->getCustomerService($cust_id);

		$bookinglist = $this->booking_model->getBookingService($booking_id);
		$data = array();
		foreach ($servicelist as $servicerow) {
			$datarow = array();
			$datarow['id'] = $servicerow->id;
			$datarow['purchased_qty'] = $servicerow->purchased_qty;
			// //calculate available qty
			//     $available_qty=$servicerow->available_qty;
			//     $usedQuantity=$this->servicesuser_model->getAvailableServiceUser($servicerow->id);
			// //end calculate available qty

			//calculate available qty
			$available_qty = $servicerow->available_qty;
			$usedQuantity = $this->servicesuser_model->getAvailableServiceUser($servicerow->id);
			if ($usedQuantity != null) {
				$outAvailable_qty = $available_qty - $usedQuantity;
			} else {
				$outAvailable_qty = $available_qty;
			}
			// //end calculate available qty

			// $datarow['available_qty']= $servicerow->available_qty;
			$datarow['available_qty'] = $outAvailable_qty;
			// $datarow['available_qty']= $usedQuantity;

			$datarow['time'] = $servicerow->time;
			$datarow['code'] = $servicerow->code;
			$datarow['category_id'] = $servicerow->category_id;
			$datarow['name'] = $servicerow->name;
			$datarow['work'] = $this->services_model->serviceConvert($servicerow->work);

			$datarow['price'] = $servicerow->price;
			$datarow['time'] = $servicerow->time;

			$datarow['remark'] = $servicerow->remark;
			$datarow['no_credit_rebate'] = $servicerow->no_credit_rebate;
			//for booking list row
			$datarow['checkedoption'] = 0;
			foreach ($bookinglist as $bookingerow) {
				if ($bookingerow->service_user_id == $servicerow->id) {
					$datarow['checkedoption'] = 1;
				}
			} //foreach ($bookinglist as $bookingerow) {
			//end for booking list row
			// if($outAvailable_qty>0){

			//cancel if quantity larger than 0 to show
			//if booking has become negative, it will hide the item

			array_push($data, $datarow);
			// }

		} //foreach ($servicelist as $servicerow) {

		header('Content-Type: application/json');
		echo json_encode($data);
	}

	public function getServiceItemByBooking() {
		$booking_id = $this->input->post('booking_id');
		$useServiceList = $this->booking_model->getUseServiceItemByBooking($booking_id);
		$serviceList = $this->booking_model->getServiceItemByBooking($booking_id);

		$data = array();
		foreach ($useServiceList as $useServiceRow) {
			$datarow = array();
			$datarow['id'] = $useServiceRow->itemid;

			$ticketnum = $this->servicesuser_model->getTicketNum($booking_id, $useServiceRow->itemid);

			$datarow['name'] = $useServiceRow->name . " " . $ticketnum . "次 ->" . ($ticketnum - 1) . "次";
			$datarow['code_1'] = $useServiceRow->code_1;
			$datarow['code_2'] = $useServiceRow->code_2;
			$datarow['code_area'] = $useServiceRow->code_area;
			$datarow['code_remark'] = $useServiceRow->code_remark;

			array_push($data, $datarow);
		} //foreach ($servicelist as $servicerow) {

		foreach ($serviceList as $serviceRow) {
			$datarow = array();
			$datarow['id'] = $serviceRow->itemid;
			$datarow['name'] = $serviceRow->name;
			$datarow['code_1'] = $serviceRow->code_1;
			$datarow['code_2'] = $serviceRow->code_2;
			$datarow['code_area'] = $serviceRow->code_area;
			$datarow['code_remark'] = $serviceRow->code_remark;

			array_push($data, $datarow);
		} //foreach ($servicelist as $servicerow) {

		header('Content-Type: application/json');
		echo json_encode($data);
	} //public function getServiceByBooking(){
	//ended add customer service end

	public function get($id = Null) {
		$partnerlist = $this->frontenduser_model->getBannerAll();
		echo '1';
		print_r($partnerlist);
	}

	public function add() {

		// $request= new Request();
		// $admin=$request->getBody();

		// 'inputadminname':$('#inputadminname').val(),
		//     'inputloginname':$('#inputloginname').val(),
		//     'inputPassword'
		// $name=$this->input->post('name');

		//add security trim input-todo
		//hash password for security
		$category_code = $this->input->post('category_code');
		$name = $this->input->post('name');
		$work = $this->input->post('work');
		$description = $this->input->post('description');
		$price = $this->input->post('price');
		$no_credit_rebate = $this->input->post('no_credit_rebate');
		$time = $this->input->post('time');
		$remark = $this->input->post('remark');

		$data = array(
			'code' => $name,
			'name' => $description,
			'work' => $work,
			// 'inputadminname'=>$inputadminname,
			'price' => $price,
			'time' => $time,
			'time_type' => 'minutes',
			'category_id' => $category_code,
			'no_credit_rebate' => $no_credit_rebate,
			'status' => 1,
			'display' => 1,
			'remark' => $remark,

		);

		$code = "";
		// if($this->frontenduser_model->admin_exists($inputadminname)==false){
		//     $code="FAILURE";
		//     $msg="已有管理員姓名".$inputadminname;
		// }
		// else
		if ($this->services_model->services_exists($name) == false) {
			$code = "FAILURE";
			$msg = "已有服務名稱" . $username;
		} else {
			$result = $this->services_model->insertAdmin($data);
			if ($result == false) {
				$code = "FAILURE";
				$msg = "記錄不能加入";
			} else {
				$code = "SUCCESS";
				$msg = "記錄成功加入";
				//for add user it will add a secret key to database

				//end

			}
		}

		header('Content-Type: application/json');
		// $code="SUCCESS";
		// $code="FAILURE";
		// $msg="已有相關記錄";
		// $myArray = ['id'=>12, 'name'=>'Topi'];
		$myArray = ['recordstatus' => $code, 'msg' => $msg];
		echo json_encode($myArray);
		// echo "posted";

	}
	public function edit() {

		// $request= new Request();
		// $admin=$request->getBody();

		// 'inputadminname':$('#inputadminname').val(),
		//     'inputloginname':$('#inputloginname').val(),
		//     'inputPassword'
		// $name=$this->input->post('name');

		//add security trim input-todo
		//hash password for security
		$inputcompanynameedit = $this->input->post('inputcompanynameedit');
		$inputfullnameedit = $this->input->post('inputfullnameedit');
		$inputteledit = $this->input->post('inputteledit');
		// $inputadminname=$this->input->post('inputadminnameedit');
		$inputloginname = $this->input->post('inputloginnameedit');
		$inputPassword = $this->input->post('inputPasswordedit');
		$id = $this->input->post('inputadminid');

		$data = array(

			'inputcompanynameedit' => $inputcompanynameedit,
			'inputfullnameedit' => $inputfullnameedit,
			'inputteledit' => $inputteledit,
			// 'inputadminname'=>$inputadminname,
			'inputloginname' => $inputloginname,
			'inputPassword' => $inputPassword,
			'id' => $id,
		);

		$code = "";
		// if($this->frontenduser_model->admin_existsbyid($inputadminname,$id)==false){
		//     $code="FAILURE";
		//     $msg="已有管理員姓名".$inputadminname;
		// }
		// else
		if ($this->frontenduser_model->login_existsbyid(strtolower($inputloginname), $id) == false) {
			$code = "FAILURE";
			$msg = "已有登入名稱" . $inputloginname;
		} else {
			$result = $this->frontenduser_model->updateAdmin($data);
			if ($result == false) {
				$code = "FAILURE";
				$msg = "記錄不能更改";
			} else {
				$code = "SUCCESS";
				$msg = "記錄成功更改";
			}

		}

		header('Content-Type: application/json');
		// $code="SUCCESS";
		// $code="FAILURE";
		// $msg="已有相關記錄";
		// $myArray = ['id'=>12, 'name'=>'Topi'];
		$myArray = ['recordstatus' => $code, 'msg' => $msg];
		echo json_encode($myArray);
		// echo "posted";

	}
	public function delete() {
		$inputdeletecheckboxvalue = $this->input->post('inputdeletecheckboxvalue');
		$result = true;
		foreach ($inputdeletecheckboxvalue as $key => $values) {
			if ($this->frontenduser_model->deleteAdmin($values) == false) {
				$result = false;
			}

		} //foreach ($inputdeletecheckboxvalue as $key => $values){

		if ($result == true) {
			$code = "SUCCESS";
			$msg = "相關記錄成功删徐";
		} else {
			$code = "FAILURE";
			$msg = "相關記錄不能删徐";
		}

		// $msg=$inputdeletecheckboxvalue;
		header('Content-Type: application/json');
		$myArray = ['recordstatus' => $code, 'msg' => $msg];
		echo json_encode($myArray);
	}
	public function getadmindata() {
		$id = $this->input->post('user_id');
		$data = $this->frontenduser_model->getAdmindateByid($id);
		header('Content-Type: application/json');
		echo json_encode($data);
	}

} //class Admins {
