<?php
namespace Smi\Controller;

use \Think\Controller;

class CardsController extends BaseController
{
	/**
	 * [denomination 充值面额列表]
	 * @AuthorHTL
	 * @DateTime  2018-02-08T20:06:16+0800
	 * @return    [type]                   [description]
	 */
	public function denomination()
	{
		$list = M('cards')->where(array('admin_id' => session('admin_id')))->select();
		$this->assign('list', $list);
		$this->display();
	}

	/**
	 * [add 添加面额]
	 * @AuthorHTL
	 * @DateTime  2018-02-08T20:07:22+0800
	 */
	public function add()
	{
		$data = I('post.');

		if (!empty($data)) {
			$add = array(
					'admin_id' => session('admin_id'),
					'money' => $data['money'],
					'preferential_money' => $data['preferential_money'],
					'create_time' => time()
				);

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

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

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

	/**
	 * [edit 编辑面额]
	 * @AuthorHTL
	 * @DateTime  2018-02-08T20:07:36+0800
	 * @return    [type]                   [description]
	 */
	public function edit($id)
	{

		$data = I('post.');

		if (!empty($data)) {
			$save = array(
					'money' => $data['money'],
					'preferential_money' => $data['preferential_money'],
					'update_time' => time()
				);

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

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

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

		$this->assign('info', $info);
		$this->assign('op', 'Edit');
		$this->display('add');
	}

	/**
	 * [delete 删除面额]
	 * @AuthorHTL
	 * @DateTime  2018-02-08T20:07:49+0800
	 * @return    [type]                   [description]
	 */
	public function delete($id)
	{
		$info = M('cards')->where(array('id' => $id))->find();

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

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

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

	/**
	 * [log 充值记录]
	 * @AuthorHTL
	 * @DateTime  2018-02-09T10:33:42+0800
	 * @return    [type]                   [description]
	 */
	public function log()
	{	
		$business_id = session('admin_id');
		$where = array('u.admin_id' => $business_id);
		$count = M('cards_log l')->join('am_users u ON u.id = l.user_id')->where($where)->count();

		$Page = new \Think\Page($count, 20);
		$show = $Page->show();
		$limit = $Page->firstRow.','.$Page->listRows;

		$list = M('cards_log l')->join('am_users u ON u.id = l.user_id')->where($where)->field('l.money, l.preferential_money, l.status, l.create_time, u.nickname')->order('l.create_time, l.status')->limit($limit)->select();

		$this->assign('list', $list);
		$this->assign('page', $show);
		$this->display();
	}
}