<?php
namespace Smi\Controller;
use Think\Controller;
class WaiyuController extends BaseController 
{	
	protected $business_id;

	public function __construct()
	{
		parent::__construct();

		$this->business_id = session('admin_id');
	}

	/**
	 * 送餐时间列表
	 */
	public function index()
	{
		$list = M('time')->where(array('admin_id' => $this->business_id))->select();
		$this->assign('list', $list);
		$this->display();
	}

	/**
	 * 添加送餐时间
	 */
	public function add()
	{
		$data = I('post.');

		if (!empty($data)) {

			$preg = '/^[\d]{1,2}[:：]{1}[\d]{1,2}$/';
			preg_match($preg, $data['time'], $arr);
			if (empty($arr)) {
				$this->error('Wrong delivery time');die;
			}

			$add = array(
				'admin_id' => $this->business_id,
				'time' => $data['time'],
				'status' => $data['status'],
				'create_time' => time(),
			);

			$result = M('time')->add($add);

			if ($result) {
				$this->success('Successfully Saved');die;
			} else {
				$this->error('Add failure');die;
			}
		}

		$this->assign('op','Add the time');
		$this->display();
	}

	/**
	 * 编辑送餐时间
	 */
	public function edit($id)
	{
		$data = I('post.');

		if (!empty($data)) {
			$preg = '/^[\d]{1,2}[:：]{1}[\d]{1,2}$/';
			preg_match($preg, $data['time'], $arr);
			if (empty($arr)) {
				$this->error('Wrong delivery time');die;
			}

			$save = array(
				'time' => $data['time'],
				'status' => $data['status'],
				'update_time' => time(),
			);

			$result = M('time')->where(array('id' => $data['id']))->save($save);

			if ($result) {
				$this->success('Successfully Edited');die;
			} else {
				$this->error('Update failed');die;
			}
		}

		$info = M('time')->where(array('id' => $id))->find();
		$this->assign('op','Edit time');
		$this->assign('info', $info);
		$this->display('add');
	}

	public function delete($id)
	{
		$info = M('time')->where(array('id' => $id))->find();

		if ($info['admin_id'] != $this->business_id) {
			$this->error('Illegal operation');die;
		}

		$result = M('time')->where(array('id' => $id))->delete();

		if ($result) {
			$this->success('Successfully Deleted');die;
		} else {
			$this->error('Delete failed');die;
		}
	}


	//范围列表 2018-6-12
	public function scope()
	{
		$list = M('scope')->where(array('admin_id' => $this->business_id))->select();
		$this->assign('list', $list);
		$this->display();
	}

	//添加范围 2018-6-12
	public function scope_add()
	{
		$data = I('post.');

		if (!empty($data)) {

			/*$preg = '/^[\d]{1,2}[:：]{1}[\d]{1,2}$/';
			preg_match($preg, $data['time'], $arr);
			if (empty($arr)) {
				$this->error('送餐时间填写错误');die;
			}*/

			$add = array(
				'admin_id' => $this->business_id,
				'scope' => $data['scope'],
				'status' => $data['status'],
				'create_time' => time(),
			);

			$result = M('scope')->add($add);

			if ($result) {
				$this->success('Successfully Saved');die;
			} else {
				$this->error('Add failure');die;
			}
		}

		$this->assign('op','Add delivery scope');
		$this->display();
	}


	//编辑范围 2018-6-12
	public function scope_edit($id)
	{
		$data = I('post.');

		if (!empty($data)) {
			/*$preg = '/^[\d]{1,2}[:：]{1}[\d]{1,2}$/';
			preg_match($preg, $data['time'], $arr);
			if (empty($arr)) {
				$this->error('送餐时间填写错误');die;
			}*/

			$save = array(
				'scope' => $data['scope'],
				'status' => $data['status'],
				//'update_time' => time(),
			);

			$result = M('scope')->where(array('id' => $data['id']))->save($save);

			if ($result) {
				$this->success('Successfully Edited');die;
			} else {
				$this->error('Update failed');die;
			}
		}

		$info = M('scope')->where(array('id' => $id))->find();
		$this->assign('op','Edit distribution scope');
		$this->assign('info', $info);
		$this->display('scope_add');
	}


	//删除范围 2018-6-12
	public function scope_delete($id)
	{
		$info = M('scope')->where(array('id' => $id))->find();

		if ($info['admin_id'] != $this->business_id) {
			$this->error('Illegal operation');die;
		}

		$result = M('scope')->where(array('id' => $id))->delete();

		if ($result) {
			$this->success('Successfully Deleted');die;
		} else {
			$this->error('Delete failed');die;
		}
	}



	public function facutime()
	{
		$info=M('facutime')->where('admin_id='.$this->business_id)->find();
		//print_r($info);die;
	    $data = I('post.');
	   

	    

	    

		if (!empty($data)) {

			$id=$data['id'];
			
			//print_r($data);die;
			/*$preg = '/^[\d]{1,2}[:：]{1}[\d]{1,2}$/';
			preg_match($preg, $data['time'], $arr);
			if (empty($arr)) {
				$this->error('送餐时间填写错误');die;
			}*/

			$addtime1=strtotime($data['addtime1']);
			$endtime1=strtotime($data['endtime1']);
			$addtime2=strtotime($data['addtime2']);
			$endtime2=strtotime($data['endtime2']);



			//$basetime=strtotime(date('Y-m-d'));

			if($addtime1>$endtime1){
				$this->error('Save failed. The start time of the first stage must be less than the end time');die;
			}
			if($addtime2<$endtime1){
				$this->error('Save failed, the second start time must be about the end time of the first');die;
			}

			if($addtime2>$endtime2){
				$this->error('Save failed. The start time of the second stage must be less than the end time');die;
			}

			$add = array(
				'admin_id' => $this->business_id,
				'addtime1' => $data['addtime1'],
				'endtime1' => $data['endtime1'],
				'addtime2' => $data['addtime2'],
				'endtime2' => $data['endtime2'],
				'status' => $data['status'],
				
			);

			if($id){
				$result = M('facutime')->where('id='.$id)->save($add);
			}else{
				$result = M('facutime')->add($add);
			}

			

			if ($result) {
				$this->success('Successfully Saved');die;
			} else {
				$this->error('Save failed');die;
			}
		}



		if(empty($info)){
			$op='Add time range';
		}else{
			$op='Edit time range';
		}

		$this->assign('op',$op);
		$this->assign('info',$info);


		$this->display();
	}
 }