<?php
namespace Smi\Controller;

class PreController extends BaseController
{	
	/**
	 * [index 优惠券列表]
	 * @AuthorHTL
	 * @DateTime  2018-02-23T11:31:20+0800
	 * @return    [type]                   [description]
	 */
	public function index()
	{
		$list = M('pre')->where(array('admin_id' => session('admin_id')))->select();
		$this->assign('list', $list);
		$this->display();
	}

	/**
	 * [add 添加优惠券]
	 * @AuthorHTL
	 * @DateTime  2018-02-23T11:31:39+0800
	 */
	public function add()
	{
		$data = I('post.');

		if (!empty($data)) {
			$add = array(
					'admin_id' => session('admin_id'),
					'pre_money' => $data['pre_money'],
					'full_money' => $data['full_money'],
					'validity_day' => $data['validity_day'],
					'nums' => $data['nums'],
					'num' => $data['num'],
					'create_time' => time(),
					'update_time' => time(),
				);
			$result = M('pre')->add($add);

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

		$this->assign('op', '添加优惠券');
		$this->display();
	}

	/**
	 * [edit 编辑优惠券]
	 * @AuthorHTL
	 * @DateTime  2018-02-23T11:32:00+0800
	 * @return    [type]                   [description]
	 */
	public function edit($id)
	{	
		$data = I('post.');

		if (!empty($data)) {
			$save = array(
					'pre_money' => $data['pre_money'],
					'full_money' => $data['full_money'],
					'validity_day' => $data['validity_day'],
					'nums' => $data['nums'],
					'num' => $data['num'],
					'update_time' => time(),
				);
			$result = M('pre')->where(array('id' => $data['id']))->save($save);

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

		$info = M('pre')->where(array('id' => $id))->find();

		$this->assign('info', $info);
		$this->assign('op', '编辑优惠券');
		$this->display('add');
	}

	/**
	 * [delete 删除优惠券]
	 * @AuthorHTL
	 * @DateTime  2018-02-23T11:32:18+0800
	 * @return    [type]                   [description]
	 */
	public function delete($id)
	{
		$info = M('pre')->where(array('id' => $id))->find();

		if ($info['admin_id'] != session('admin_id')) {
			$this->error('非法操作');die;
		}	

		$result = M('pre')->where(array('id' => $id))->delete();
		if ($result) {
			$this->success('删除成功');die;
		} else {
			$this->error('删除失败');die;
		}
	}
}