<?php
namespace Smi\Controller;

use \Think\Controller;

class NavController extends BaseController
{
	/**
	 * [denomination 充值面额列表]
	 * @AuthorHTL
	 * @DateTime  2018-02-08T20:06:16+0800
	 * @return    [type]                   [description]
	 */
	public function index()
	{
		$list = M('nav')->where(array('admin_id' => session('admin_id')))->order('sort ASC,id ASC')->select();

		if(empty($list)){

			$title = array('Redeem Mall','Member Card','Get Voucher','About Us','Scan & Order','Distributor','Offer','Reservation','Order Center','Distribution Center','C2C','Food Info','Scratch & Win','Spin Wheel','Golden Egg','Sign In');
			foreach ($title as $value) {
				$add['admin_id']=session('admin_id');
				$add['title']=$value;
				$add['status']=1;
				$add['sort']=0;
				M('nav')->add($add);
			}
			$list = M('nav')->where(array('admin_id' => session('admin_id')))->order('sort ASC,id ASC')->select();
		}
        $this->assign('list', $list);
        //print_r($list);die;
		$this->display();
	}

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

    	if (!empty($data)) {
    		$add = array(
    				'admin_id' => $this->business_id,
    				'title' => $data['title'],
    				'link' => $data['link'],
    				'status' => empty($data['status']) ? 1 : $data['status'],
    				'sort' => $data['sort'],
    				'create_time' => time(),
    			); 
    		if (empty($_FILES['img']['name'])) {
    			$this->error('请上传图片');die;
    		} else {
    			$upload = ToolsController::upload('chart/');
				if ($upload['status'] == -1) {
					$this->error('图片上传失败');die;
				} else {
					$add['img'] = $upload['file_path'];
				}
    		}

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

    		if (!empty($_FILES['img']['name'])) {
				unlink($this->root.'/Uploads/'.$info['img']);
			}

    		if ($result) {
    			$this->success('保存成功');die;
    		} else {
    			if (!empty($_FILES['img']['name']) && $upload['status'] == 1) {
					unlink($this->root.'/Uploads/'.$upload['file_path']);
				}
    			$this->error('保存失败');die;
    		}
    	}

    	$this->assign('op', '新增');
    	$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(
					'sort' => $data['sort'],
					'status' => $data['status']
					
				);

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

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

		$info = M('nav')->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 kaiguan()
	{
		$data = I('post.');

		if (!empty($data)) {
			$save = array(
					
					'status' => $data['status']
					
				);
			if($data['id']){
				$result = M('nav_config')->where(array('id' => $data['id']))->save($save);
			}else{
                    $save['admin_id']=session('admin_id');
                    $result = M('nav_config')->add($save); 
               }

			

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

		$info = M('nav_config')->where('admin_id='.session('admin_id'))->find();
		$this->assign('info', $info);
		$this->display();
		
		
	}

	/**
	 * [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();
	}
}