<?php

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

class Brshopend extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('Brshopmodel', 'shop_model');

	}

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

		$data = array(
			'shortcode' => $shortcode,
			'code' => $code,
			'name' => $name,
			'sales_base' => $sales_base,
		);

		$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() {

		$shortcode = $this->input->post('shortcode');
		$code = $this->input->post('code');
		$name = $this->input->post('name');
		$id = $this->input->post('id');
		$sales_base = $this->input->post('sales_base');

		$data = array(

			'shortcode' => $shortcode,
			'code' => $code,
			'name' => $name,
			'id' => $id,
			'sales_base' => $sales_base,

		);

		$code = "";
		// if($this->frontenduser_model->admin_existsbyid($inputadminname,$id)==false){
		//     $code="FAILURE";
		//     $msg="已有管理員姓名".$inputadminname;
		// }
		// else
		if ($this->shop_model->shopCodeExistByID(strtolower($shortcode), $id) == false) {
			$code = "FAILURE";
			$msg = "已有門店簡稱" . $shortcode;
		} else {
			$result = $this->shop_model->updateShop($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->shop_model->deleteShop($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 {
