<?php

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

class Bremployeewagelevelend extends CI_Controller {

	public function __construct() {
		parent::__construct();
		date_default_timezone_set('Asia/Hong_Kong');
		// $this->load->model('Bremployeemodel', 'employee_model');
		$this->load->model('Bremployeewagemodel', 'employeewage_model');
		$this->load->model('Bremployeewagelevelmodel', 'employeewagelevel_model');
		$this->load->library(array('session'));
	}
	//new added employeeend add
	public function add() {

		$wageid = $this->input->post('wageid');
		$sales_amount = $this->input->post('sales_amount');
		$precentage = $this->input->post('precentage');
		$job_pay = $this->input->post('job_pay');

		$data = array(
			'wageid' => $wageid,
			'sales_amount' => $sales_amount,
			'precentage' => $precentage,
			'job_pay' => $job_pay,

		);

		$result = $this->employeewagelevel_model->insertWageLevel($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 add(){
	public function update() {

		//upload photo
		//need to improve the check file type 2020-07-03

		//end upload photo

		$wagelevelid = $this->input->post('wagelevelid');
		$sales_amount = $this->input->post('sales_amount');
		$precentage = $this->input->post('precentage');
		$job_pay = $this->input->post('job_pay');
		$remark = $this->input->post('remark');

		$data = array(
			'id' => $wagelevelid,
			'sales_amount' => $sales_amount,
			'precentage' => $precentage,
			'job_pay' => $job_pay,
			'remark' => $remark,

		);

		$result = $this->employeewagelevel_model->updateWageLevel($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];
		$myArray = ['recordstatus' => $code, 'msg' => $msg];
		echo json_encode($myArray);
		// echo "posted";

	} //public function update(){

	public function delete() {
		$id = $this->input->post('id');
		$result = true;

		if ($this->employeewagelevel_model->deleteWageLevel($id) == false) {
			$result = false;
		}

		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 status() {
		$action = $this->input->post('action');
		$personid = $this->input->post('personid');
		if ($action == "add") {

			//insert first level id
			$data = array(

				'id' => $personid,
				'status' => 1,
				// 'level1id'=>$level1id,
				// // 'inputadminname'=>$inputadminname,
				// 'level2id'=>$level2id,
				// 'level3id'=>$level3id,
				// 'level4id'=>$level4id
			);

			$result = $this->employee_model->updateStatus($data);
		}
		if ($action == "remove") {
			$data = array(

				'id' => $personid,
				'status' => 0,
			);

			$result = $this->employee_model->updateStatus($data);
		} //if($action=="remove"){

		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 checkRole() {
		$username = $this->input->post('username');
		$password = $this->input->post('password');

		$data = array(

			'username' => $username,
			'password' => $password,
		);

		$code = "";
		// if($this->frontenduser_model->admin_existsbyid($inputadminname,$id)==false){
		//     $code="FAILURE";
		//     $msg="已有管理員姓名".$inputadminname;
		// }
		// else
		// if($this->customer_model->checkRole(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="記錄成功更改";
		//     }

		// }

		$roleId = $this->employee_model->checkRole($username, $password);

		$msg = "";

		header('Content-Type: application/json');
		// $code="SUCCESS";
		// $code="FAILURE";
		// $msg="已有相關記錄";
		// $myArray = ['id'=>12, 'name'=>'Topi'];
		$myArray = ['recordstatus' => $code, 'msg' => $msg, 'username' => $username, 'password' => $password, 'roleId' => $roleId];
		echo json_encode($myArray);
		// echo "posted";
	} //public function checkRole(){
	//end new added employeeend add

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

	public function addold() {

		// $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
		$shortcode = $this->input->post('shortcode');
		$code = $this->input->post('code');
		$name = $this->input->post('name');

		$data = array(
			'shortcode' => $shortcode,
			'code' => $code,
			'name' => $name,

		);

		$code = "";
		// if($this->frontenduser_model->admin_exists($inputadminname)==false){
		//     $code="FAILURE";
		//     $msg="已有管理員姓名".$inputadminname;
		// }
		// else
		if ($this->shop_model->shopcode_exists(strtolower($shortcode)) == false) {
			$code = "FAILURE";
			$msg = "已有門店簡稱" . $shortcode;
		} else {
			$result = $this->shop_model->insertShop($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 getadmindata() {
		$id = $this->input->post('user_id');
		$data = $this->frontenduser_model->getAdmindateByid($id);
		header('Content-Type: application/json');
		echo json_encode($data);
	}

} //class Admins {
