<?php
/**
 * 奖品明细
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Admin\Controller;
class PrizeTongController extends BaseController{
	public function index(){
		$postdata = I('param.');
		$map = array();
		if($postdata['win_type']){
			$map['win_type'] = $postdata['win_type'];
		}
		if($postdata['keyword']){
			$u_map = array();
			$u_map['id|username|nickname|real_name|mobile'] = array('like','%'.$postdata['keyword'].'%');
			$uids = M('Member')->where($u_map)->getField('id',true);
			if($uids) $map['user_id'] = array('in',$uids);
			else $map['user_id'] = array('in','');
		}
		if($postdata['check_status']){
			if($postdata['check_status'] == 1) $map['check_status'] = 1;
			else $map['check_status'] = 0;
		}
		if($postdata['date_from'] || $postdata['date_to']){
            $date_from = strtotime($postdata['date_from']);
            if($date_from)$map['add_time'][] = array('egt',$date_from);
            $date_to = strtotime($postdata['date_to'])+86399;
            if($date_to)$map['add_time'][] = array('lt',$date_to);
			$postdata['dates']=0;
        }else{
			$now_time=strtotime(date('Y-m-d',NOW_TIME));
			if($postdata['dates']==1){
				$start_time=$now_time;
				$end_time=$now_time+86399;
				$map['add_time']=array('between',array($start_time,$end_time));
			}else if($postdata['dates']==2){
				$start_time=$now_time-86400;
				$end_time=$now_time-1;
				$map['add_time']=array('between',array($start_time,$end_time));
			}else if($postdata['dates']==7){
				$start_time=$now_time-86400*6;
				$end_time=$now_time+86399;
				$map['add_time']=array('between',array($start_time,$end_time));
			}else if($postdata['dates']==30){
				$start_time=$now_time-86400*29;
				$end_time=$now_time+86399;
				$map['add_time']=array('between',array($start_time,$end_time));
			}else{
				$postdata['dates']=99;
			}
		}
		
        $map['gift_name'] =array('neq','积分');
		$order = 'id desc';
		$list=page_list('WinGift',$map,$order,true,20);
		foreach($list['list'] as $k=>$v){
			if($v['win_type'] != 5){
				$map = array();$map['id'] = $v['gift_id'];
				$prize_info = M('Prize')->where($map)->field('prize_type,prize_goods')->find();
				if($prize_info['prize_type'] == 1){
					$gift_name = $prize_info['prize_goods'];
				}else if($prize_info['prize_type'] == 2){
					$gift_name = '现金*'.$prize_info['prize_goods'];
				}else if($prize_info['prize_type'] == 3){
					$gift_name = '积分*'.$prize_info['prize_goods'];
				}else if($prize_info['prize_type'] == 4){
					$c_map = array();$c_map['id'] = $prize_info['prize_goods'];
					$coupons_name = M('Coupons')->where($c_map)->getField('title');
					$gift_name = $coupons_name.'优惠券*1';
				}
				$v['gift_name'] = $gift_name;
				if($v['win_type'] == 1){
					$v['win_name'] = '积分商品';
				}else if($v['win_type'] == 2){
					$v['win_name'] = '小票抽奖';
				}else if($v['win_type'] == 3){
					$v['win_name'] = '推广活动';
				}else if($v['win_type'] == 4){
					$v['win_name'] = '全民发券';
				}
			}else{
				$v['gift_name'] = $v['gift_name'].'*'.$v['gift_number'];
				$v['win_name'] = '签到';
			}
			$list['list'][$k] = $v;
		}
		$this->win_type_arr();
		$this->assign($list);
		$this->assign($postdata);
	    $this->display();
	}
	private function win_type_arr(){
		$win_type_arr = array('1'=>'积分商城','2'=>'小票抽奖','3'=>'推广活动','4'=>'全民发券','5'=>'签到');
		$check_status_arr = array('1'=>'已领取','2'=>'未领取');
		$this->assign('win_type_arr',$win_type_arr);
		$this->assign('check_status_arr',$check_status_arr);
	}
}