<?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('送餐时间填写错误');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('添加成功');die;
			} else {
				$this->error('添加失败');die;
			}
		}

		$this->assign('op','添加时间');
		$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('送餐时间填写错误');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('更新成功');die;
			} else {
				$this->error('更新失败');die;
			}
		}

		$info = M('time')->where(array('id' => $id))->find();
		$this->assign('op','编辑时间');
		$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('非法操作');die;
		}

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

		if ($result) {
			$this->success('删除成功');die;
		} else {
			$this->error('删除失败');die;
		}
	}
 }