<?php

namespace Admin\Controller;
use User\Api\UserApi as UserApi;
use Think\Controller;

/**
 * 商品控制器
 * @author 鹿文学
 */
class MerchandiseController extends ThinkController {

    const model_name = 'Merchandise';

    /**
     * 商品列表
     */
    public function lists(){

				if(!empty($_REQUEST['account'])) {$extend['user_account']=array('like','%'.$_REQUEST['account'].'%');}
				if(!empty($_REQUEST['small'])) {$extend['small_account']=array('like','%'.$_REQUEST['small'].'%');}
				if(!empty($_REQUEST['timestart']) && !empty($_REQUEST['timeend'])) {$extend['create_time'] = array('between',[strtotime($_REQUEST['timestart']),strtotime($_REQUEST['timeend'])+86399]);}
				elseif (!empty($_REQUEST['timestart']) && empty($_REQUEST['timeend'])) {$extend['create_time'] = array('between',[strtotime($_REQUEST['timestart']),time()]);}
				elseif (empty($_REQUEST['timestart']) && !empty($_REQUEST['timeend'])) {$extend['create_time'] = array('elt',strtotime($_REQUEST['timeend'])+86399);}
				if(is_numeric($_REQUEST['game_id']) && $_REQUEST['game_id']>0) {$extend['game_id']=$_REQUEST['game_id'];}
				if(is_numeric($_REQUEST['status'])) {$extend['status']=$_REQUEST['status'];}
        if(!empty($_REQUEST['keyword'])) {$extend['game_name|title|server_name|small_account']=array('like','%'.$_REQUEST['keyword'].'%');}

				if(!empty($_REQUEST['id'])) {$extend['id'] = $_REQUEST['id'];}


				$this->m_title = '商品列表';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'Merchandise/lists','status'=>1])->find());


        parent::lists(self::model_name,$_GET['p'],$extend);

    }

		/**
     * 查看商品信息
     */
		public function edit() {
			//if(IS_POST) {




			//} else {
				$map['id'] = $_REQUEST['id'];
				$data = D(self::model_name)->where($map)->find();
				$this->assign('data',$data);
				$user = D('User')->field('account,alipay,real_name')->where(['id'=>$data['user_id']])->find();
				$this->assign('user',$user);

				$this->meta_title = '查看商品信息';

				$this->m_title = '商品列表';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'Merchandise/lists','status'=>1])->find());


				$this->display();
			//}
		}

		/**
     * 商品状态
     */
		public function update_status(){
			$ids      = I('request.ids');
			$status   = I('request.status');
			$field = 'status';
			if(empty($ids)){
					$this->error('请选择要操作的数据');
			}

			$map['id'] = array('in',$ids);

			switch ($status){
				case -1 : /* 下架 */
						$data = [$field=>-1];
						$msg = array('success'=>'下架成功','error'=>'下架失败');
						break;
				case 0  : /* 禁用 */
						$data = [$field=>0,'operator_id'=>0,'operator_account'=>'','online_time'=>'','remark'=>''];
						$msg = array('success'=>'禁用成功','error'=>'禁用失败');
						break;
				case 2  : /* 驳回/拒绝 */
						$data = [$field=>2,'operator_id'=>UID,'operator_account'=>get_admin_account(UID),'online_time'=>time(),'remark'=>I('remark')];
						$msg = array('success'=>'驳回成功','error'=>'驳回失败');
						break;
				case 3  : /* 出售中 */
						$data = [$field=>3,'operator_id'=>UID,'operator_account'=>get_admin_account(UID),'online_time'=>time(),'remark'=>''];
						$msg = array('success'=>'审核成功','error'=>'审核失败');
						break;
				default :
                        $data = [];
						$this->error('参数错误');
						break;
			}
			$user = D('User');
			if( D(self::model_name)->where($map)->save($data)!==false ) {
				$pa = $user->field('id')->where(['is_platform'=>1])->find();
				switch($status) {
					case -1:{
						$us = D(self::model_name)->field('user_id,small_id')->where(['id'=>$ids])->find();
						$user->where(['id'=>$us['small_id']])->save(['lock_status'=>1,'puid'=>$us['user_id'],'source_puid'=>0]);
					};break;
					case 2:{
						$us = D(self::model_name)->field('user_id,small_id')->where(['id'=>$ids])->find();
						$user->where(['id'=>$us['small_id']])->save(['lock_status'=>1,'puid'=>$us['user_id'],'source_puid'=>0]);
					};break;
					case 3:{
						$us = D(self::model_name)->field('user_id,small_id')->where($map)->select();
						foreach($us as $v) {

							$user->where(['id'=>$v['small_id']])->save(['lock_status'=>1,'puid'=>$pa['id'],'source_puid'=>$v['user_id']]);

						}

					};break;
				}
				$this->success($msg['success'],'',IS_AJAX);
			}else{
				$this->error($msg['error'],'',IS_AJAX);
			}


    }




}