<?php
/**
 * 推荐设置控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
namespace Admin\Controller;
class ReceiptDrawController extends BaseController
{
    /**
     * 读取推荐设置的数据
     */
    public function index()
    {
		$map = array();
		$keyword = I('keyword');
		if(!empty($keyword)){
			$whereor['id']=array('like','%'.$keyword.'%');
            $whereor['draw_name']=array('like','%'.$keyword.'%');
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
            $seotitle=$keyword.'_';
		}
		if(!empty($postdata['reg_from']))$map['reg_time'][]=array('EGT',strtotime($postdata['reg_from']));
		if(!empty($postdata['reg_to']))$map['reg_time'][]=array('elt',strtotime($postdata['reg_to']));
		$map['class'] = 0;
		$list = $this->lists('ReceiptDraw',$map);
        $this->assign('list', $list);
		$this->assign($postdata);
        $this->display();
    }
	public function draw_add(){
		$list = array(
				'0'=>array(
					'prize_name'=>'一等奖',
					'prize_type'=>'一等奖',
					'prize_goods'=>'',
					'goods_num'=>0,
					'prize_probability'=>10,
				),
				'1'=>array(
					'prize_name'=>'二等奖',
					'prize_type'=>'一等奖',
					'prize_goods'=>'',
					'goods_num'=>0,
					'prize_probability'=>10,
				),
				'2'=>array(
					'prize_name'=>'三等奖',
					'prize_type'=>'一等奖',
					'prize_goods'=>'',
					'goods_num'=>0,
					'prize_probability'=>10,
				)
		);
		$this->getGroup(1);
		$this->assign('list',$list);
		$this->display('edit');
	}
	public function draw_insert(){
		$postdata = I('param.');
		if(!$postdata['draw_name'] || !$postdata['start_time'] || !$postdata['end_time']){
			$this->error('必填项不能为空');exit;
		}
		if(strtotime($postdata['end_time'])<strtotime($postdata['start_time'])){
			$this->error('结束时间不能小于开始时间');
		}
		foreach($postdata['goods_num'] as $k=>$v){
			if(is_NaN($v))$this->error('请输入正确的奖品数量');
		}
		foreach($postdata['prize_probability'] as $k=>$v){
			if(is_NaN($v))$this->error('请输入正确的中奖率');
		}
		$receipt_draw['draw_name'] = $postdata['draw_name'];
		$receipt_draw['start_time'] = strtotime($postdata['start_time']);
		$receipt_draw['end_time'] = strtotime($postdata['end_time']);
		$receipt_draw['explain'] = $postdata['explain'];
		$receipt_draw['member'] = $postdata['member'];
		$receipt_draw['status'] = $postdata['status'];
		$ret = M('ReceiptDraw')->add($receipt_draw);
		if($ret){
			$count = count($postdata['prize_name']);
			for($i=0; $i<$count; $i++){
				if($postdata['prize_name'][$i]){
					$prize = array();
					$prize['act_id'] = $ret;
					$prize['prize_type'] = $postdata['prize_type'][$i];
					if($prize['prize_type'] == 4){
						$prize['prize_goods'] = $postdata['c_prize_goods'][$i];
					}else{
						$prize['prize_goods'] = $postdata['prize_goods'][$i];
					}
					$prize['prize_name'] = $postdata['prize_name'][$i];
					$prize['goods_num'] = $postdata['goods_num'][$i];
					$prize['prize_probability'] = $postdata['prize_probability'][$i];
					M('Prize')->add($prize);
				}
			}
			$this->success(L('DO_OK'), '', '', 'index');
		}else{
			$this->error('数据错误');
		}
	}
	
	public function draw_edit(){
		$id = I('param.id');
		$info = M('ReceiptDraw')->where('id='.$id)->field(true)->find();
		$this->assign($info);
		$list = M('Prize')->where('act_id='.$id)->field(true)->select();
		$this->assign('list',$list);
		$this->getGroup(1);
		$this->display('edit');
	}
	public function draw_update(){
		$postdata = I('param.');
		if(!$postdata['draw_name'] || !$postdata['start_time'] || !$postdata['end_time']){
			$this->error('必填项不能为空');exit;
		}
		if(strtotime($postdata['end_time'])<strtotime($postdata['start_time'])){
			$this->error('结束时间不能小于开始时间');
		}
		foreach($postdata['goods_num'] as $k=>$v){
			if(is_NaN($v))$this->error('请输入正确的奖品数量');
		}
		foreach($postdata['prize_probability'] as $k=>$v){
			if(is_NaN($v))$this->error('请输入正确的中奖率');
		}
		$receipt_draw['id'] = $postdata['id'];
		$receipt_draw['draw_name'] = $postdata['draw_name'];
		$receipt_draw['start_time'] = strtotime($postdata['start_time']);
		$receipt_draw['end_time'] = strtotime($postdata['end_time']);
		$receipt_draw['explain'] = $postdata['explain'];
		$receipt_draw['member'] = $postdata['member'];
		$receipt_draw['status'] = $postdata['status'];
		$ret = M('ReceiptDraw')->save($receipt_draw);
		
		$count = count($postdata['p_id']);
		for($i=0; $i<$count; $i++){
			if($postdata['prize_name'][$i]){
				$prize = array();
				$prize['id'] = $postdata['p_id'][$i];
				$prize['act_id'] = $postdata['id'];
				$prize['prize_type'] = $postdata['prize_type'][$i];
				if($prize['prize_type'] == 4){
					$prize['prize_goods'] = $postdata['c_prize_goods'][$i];
				}else{
					$prize['prize_goods'] = $postdata['prize_goods'][$i];
				}
				$prize['prize_name'] = $postdata['prize_name'][$i];
				$prize['goods_num'] = $postdata['goods_num'][$i];
				$prize['prize_probability'] = $postdata['prize_probability'][$i];
				M('Prize')->save($prize);
			}
		}
		$this->success(L('DO_OK'), '', '', 'index');
	}
	/* 得到所属组 */
    private function getGroup($type = ''){
		$c_map = array();$new_time = NOW_TIME;
		$c_map['coupons_type'] = 1;
		$c_map['buy_type'] = 0;
		$c_map['status'] = 1;
		$c_map['quantity'] = array('GT',0);
		$c_map['_string'] = "end_timestamp >".$new_time." OR end_timestamp = '0'";
		//卡券
		$coupons = M('Coupons')->where($c_map)->field(true)->select();
        $this->assign('coupons', $coupons);
    }
}
?>