<?php
/**
 * @author xiaogg QQ:756663992
 * @copyright 2017
 */
namespace Admin\Controller;
class TokerController extends BaseController {
	public function index(){
		$postdata = I('post.');
    	$map=array();	
    	if($postdata['shops_keyword'])$map['draw_name'] = array('like','%'.$postdata['shops_keyword'].'%');
    	if($postdata['reg_from'] && !$postdata['reg_to'])$map['add_time'] = array(array('gt',strtotime($postdata['reg_from'])));
    	if($postdata['reg_to'] && !$postdata['reg_from'])$map['add_time'] = array(array('lt',strtotime($postdata['reg_to'])));
    	if($postdata['reg_from'] && $postdata['reg_to'])$map['add_time'] = array(array('gt',strtotime($postdata['reg_from'])),array('lt',strtotime($postdata['reg_to'])));
		$map['class'] = 1;
		$info=$this->lists('ReceiptDraw',$map);
    	$this->assign('info', $info);
    	$this->assign('postdata', $postdata);
		$this->display('coupon_send');
	}
	public function coupon_send_add(){
        $list = array(
                '0'=>array(
                    'prize_name'=>'一等奖',
                    'prize_goods'=>'',
                    'goods_num'=>0,
                    'prize_probability'=>10,
                ),
                '1'=>array(
                    'prize_name'=>'二等奖',
                    'prize_goods'=>'',
                    'goods_num'=>0,
                    'prize_probability'=>10,
                ),
                '2'=>array(
                    'prize_name'=>'三等奖',
                    'prize_goods'=>'',
                    'goods_num'=>0,
                    'prize_probability'=>10,
                )
        );
        $this->assign('list',$list);
        $this->get_coupons();
		$this->display();
	}
	public function coupon_send_edit(){
        $postdata = I('post.');
        if($postdata){
            $save_data['draw_name'] = $postdata['draw_name'];
            $save_data['start_time'] = strtotime($postdata['start_time']);
            $save_data['end_time'] = strtotime($postdata['end_time']);
            // $save_data['logo_url'] = C('WEB_SMALL_LOGO');
            $save_data['logo_url'] = $postdata['logo_url'];
            $save_data['explain'] = $postdata['explain'];
            $save_data['event_confine'] = $postdata['event_confine'];
            $save_data['status'] = $postdata['status'];
            $save_data['aging'] = $postdata['aging'];
            $save_w['id'] = $postdata['id'];
            M('ReceiptDraw')->where($save_w)->save($save_data);
            if($postdata["p_id"]){
                $del_w['id'] = array('in',$postdata["p_id"]);
                M('Prize')->where($del_w)->delete();
            }            
            $level_point_prize['act_id'] = $postdata['id'];
            foreach ($postdata['key'] as $key => $v) {
                if (empty($v) && empty($postdata['key'][$key])) continue;
                $level_point_prize['prize_name'] = $postdata['key'][$key];
                $level_point_prize['goods_num'] = $postdata['goods_num'][$key];
                if($postdata['c_prize_goods'][$key]){
                    $level_point_prize['prize_goods'] = $postdata['c_prize_goods'][$key];
                }else{
                    $level_point_prize['prize_goods'] = $postdata['prize_goods'][$key];
                }
                $level_point_prize['prize_type'] = $postdata['prize_type'][$key];
                $level_point_prize['class'] = 1;
                $add = M('Prize')->add($level_point_prize);
            }
            $url = U('Admin/Toker/index');
            if (!empty($add)) {
                $this->success('修改成功',$url);
            } else {
                $this->error('修改失败',$url);
            }
        }else{
            $id = I('get.id');
            $map_p['act_id'] = $map_d['id'] = $id;
            $map_p['class'] = $map_d['class'] = 1;
            // 获取活动
            $receipt_draw = M('ReceiptDraw')->where($map_d)->field(true)->find();
            // 获取奖励列表
            $prize = M('Prize')->where($map_p)->field(true)->select();
            $this->get_coupons();
            $this->assign('list',$prize);
            $this->assign($receipt_draw);
    		$this->display('coupon_send_add');
        }
	}
	public function coupon_send_insert(){
		$url = U('Admin/Toker/index');
		$postdata = I('post.');
        $data['draw_name'] = $postdata['draw_name'];
        $data['start_time'] = strtotime($postdata['start_time']);
        $data['end_time'] = strtotime($postdata['end_time']);
        // $data['logo_url'] = C('WEB_SMALL_LOGO');
        $data['logo_url'] = $postdata['logo_url'];
        $data['explain'] = $postdata['explain'];
        $data['event_confine'] = $postdata['event_confine'];
        $data['aging'] = $postdata['aging'];
        $data['status'] = $postdata['status'];
        $data['class'] = 1;
        $data['add_time'] = time();
        $save = M('ReceiptDraw')->data($data)->add();
        $level_point_prize['act_id'] = $save;
        foreach ($postdata['key'] as $key => $v) {
            if (empty($v) && empty($postdata['key'][$key])) continue;
            $level_point_prize['prize_name'] = $postdata['key'][$key];
            $level_point_prize['goods_num'] = $postdata['goods_num'][$key];
            if(!$postdata['prize_goods'][$key]){
                $level_point_prize['prize_goods'] = $postdata['c_prize_goods'][$key];
            }else{
                $level_point_prize['prize_goods'] = $postdata['prize_goods'][$key];
            }
            $level_point_prize['prize_type'] = $postdata['prize_type'][$key];
            $level_point_prize['class'] = 1;
            M('Prize')->add($level_point_prize);
        }
        if (!empty($save)) {
            $this->success('添加成功',$url);
        } else {
            $this->error('添加失败',$url);
        }
    }
    // 获取优惠券
    public function get_coupons(){
		$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);
    }
    // 删除活动
    public function delete(){
        $id = I('get.id');
        $map['id'] = $id;
        $map['class'] = 1;
        // 删除活动
        $delete_r = M('ReceiptDraw')->where($map)->delete();
        unset($map['id']);
        $map['act_id'] = $id;
        // 删除活动规则
        $delete_p = M('Prize')->where($map)->delete();
        $url = U('Admin/Toker/coupon_send');
        if($delete_r && $delete_p){
            $this->success('删除成功',$url);
        }else{
            $this->error('删除失败',$url);
        }
    }
	/**
     * 拓客海报
     */
	public function poster(){
        $is_auth=check_use_auth("NationalCustomers");
        if($is_auth['status']==0){
            $this->error('该版本没有此权限',$is_auth['url']);return;
        }
        $this->display();
    }
	/**
     * 拓客海报背景
     */
	public function poster_bj(){
        $is_auth=check_use_auth("NationalCustomers");
        if($is_auth['status']==0){
            $this->error('该版本没有此权限',$is_auth['url']);return;
        }
       // var_dump(C('POSTER_LIST_ID'));die;
        $poster_list=poster_list();//调取可用海报列表
		// echo "<pre>";print_r($poster_list);exit;
		$this->assign('poster_list',$poster_list);//保存id到一个配置项中,判断有没有 没有就创建 POSTER_LIST_ID
		$this->assign('dq_id',C('POSTER_LIST_ID'));
        $this->display();
    }
    	/**
     * 修改海报设置
     */
    public function poster_update(){
        $config = I('post.');
        $postdata[]=str_replace('px','',$config['headerxy']);
        $postdata[]=str_replace('px','',$config['nicknamexy']);
        $postdata[]=str_replace('px','',$config['codexy']);
        $postdata[]=str_replace('px','',$config['recomcode']);
        $param = implode("|",$postdata);
          // print_r($config);exit;
        $attr_data = require_once ROOT_PATH."/Public/data/poster_data.php";
        // print_r($attr_data);
        if($config['poster_id']==1){
            if($config['codewidth'])$codewidth = $config['codewidth'];
            if($config['headerwidth'])$headerwidth = $config['headerwidth'];
            $param1['background'] = $attr_data[1]['background'];
            $param1['param']=$param;
            $param1['type']=$attr_data[1]['type'];
            $param1['font_size']=$attr_data[1]['font_size'];//字体大小
            $param1['erweima']=$codewidth;//二维码大小
            $param1['touxiang']=$headerwidth;//头像大小

            $param2['background'] = $attr_data[2]['background'];
            $param2['param']=$attr_data[2]['param'];
            $param2['type']=$attr_data[2]['type'];
            $param2['font_size']=$attr_data[2]['font_size'];//字体大小
            $param2['erweima']=$attr_data[2]['erweima'];//二维码大小
            $param2['touxiang']=$attr_data[2]['touxiang'];//头像大小
        }
        if($config['poster_id']==2){
            if($config['codewidth'])$codewidth = $config['codewidth'];
            if($config['headerwidth'])$headerwidth = $config['headerwidth'];

            $param1['background'] = $attr_data[1]['background'];
            $param1['param']=$attr_data[1]['param'];
            $param1['type']=$attr_data[1]['type'];
            $param1['font_size']=$attr_data[1]['font_size'];//字体大小
            $param1['erweima']=$attr_data[1]['erweima'];//二维码大小
            $param1['touxiang']=$attr_data[1]['touxiang'];//头像大小

            $param2['background'] = $attr_data[2]['background'];
            $param2['param']=$param;
            $param2['type']=$attr_data[2]['type'];
            $param2['font_size']=$attr_data[2]['font_size'];//字体大小
            $param2['erweima']=$codewidth;//二维码大小
            $param2['touxiang']=$headerwidth;//头像大小
        }
        // print_r($param1);exit;
         $arr = array(
            1   =>  array(
            "background"     => $param1['background'],
            "param"          => $param1['param'],//海报坐标表示海报头像、昵称和二维码的坐标，用|隔开。昵称的横坐标可用center
            "type"           => $param1['type'],//类型非必填 默认0 1商品推广图片 
            "font_size"    =>   $param1['font_size'],//字号非必填默认16
            "erweima"      => $param1['erweima'],//二维码大小 非必填默认200
            "touxiang"   => $param1['touxiang'],//头像大小 非必填默认300
            //"permanent"    => 0,//二维码时效 非必填默认0 0永久 1临时
            ),
            2   =>  array(
            "background"     => $param2['background'],
            "param"          => $param2['param'],//海报坐标表示海报头像、昵称和二维码的坐标，用|隔开。昵称的横坐标可用center
            "type"           => $param2['type'],//类型非必填 默认0 1商品推广图片 
            "font_size"    =>   $param2['font_size'],//字号非必填默认16
            "erweima"      => $param2['erweima'],//二维码大小 非必填默认200
            "touxiang"   => $param2['touxiang'],//头像大小 非必填默认300
            //"permanent"    => 0,//二维码时效 非必填默认0 0永久 1临时
            ),
        );
        $arrr = array(
            1	=>	array(
                    "background"     => 'http://shop7.thinkp2p.cn/uploads/image/20190131/5c52b21b600a7.jpg',
                    "param"	         =>	"252,110|397,368|218,743|225,745",//海报坐标表示海报头像、昵称和二维码的坐标，用|隔开。昵称的横坐标可用center
                    "name"	         =>	"默认1",//非必填
                    "type"	         =>	0,//类型非必填 默认0 1商品推广图片 
                    "auth_siteid"    => "",//允许使用的站点ID 多个,分隔 空为不限制
                    "auth_package"   => "",//允许使用的套餐ID 多个,分隔 空为不限制
                    "font_size"    =>	20,//字号非必填默认16
                    "erweima"	     =>	210,//二维码大小 非必填默认200
                    "touxiang"	 =>	150,//头像大小 非必填默认300
                    //"permanent"	 =>	0,//二维码时效 非必填默认0 0永久 1临时
            ),
            2	=>	array(
                    "background"     => 'http://mendian.oss-cn-qingdao.aliyuncs.com/uploads/image/20181208/5c0b86a8b044e.jpg',
                    "param"	         =>	"308,491|center,666,750|264,706",
                    "auth_siteid"   => "1061",
                    "touxiang"	     =>	132,
                    "erweima"	     =>	230,
                    "type"	         =>	0,
            ),
        );
       // print_r($arrr);exit;
        // file_put_contents('Public/data/poster_datas.php',var_export($arr,true));exit;
        arr2file('Public/data/poster_data.php',var_export($arr,true));

        $url = U('Admin/Toker/poster');
        $this->success('保存成功！',$url);
        // print_r($config);exit;
        // if ($config && is_array($config)) {
        //     $vi=array();
        //     $this->model = D('Config');
        //     foreach ($config as $name => $value) {
        //         $map = array('name' => $name);
        //         if(is_array($value)){
        //             foreach($value as $key=>$val){
        //                 $vi[]=implode(":",$val);
        //             }$value = implode(",",$vi);
        //         }
        //         print_r($map);exit;
        //         $this->model->where($map)->setField('value', $value);
        //     }
        //     S('DB_CONFIG_DATA', null);
        // }
        
    }

  
	/**
     * 修改海报设置
     */
    public function poster_update_old(){
		$config = I('post.');
        if ($config && is_array($config)) {
            $vi=array();
            $this->model = D('Config');
            foreach ($config as $name => $value) {
                $map = array('name' => $name);
                if(is_array($value)){
                    foreach($value as $key=>$val){
                        $vi[]=implode(":",$val);
                    }$value = implode(",",$vi);
                }
                $this->model->where($map)->setField('value', $value);
            }
            S('DB_CONFIG_DATA', null);
        }
        $this->success('保存成功！');
    }
    // 7-1  海报编辑
    public function poster_tz(){
        $postdata = I('param.');
        $attr_data = require_once ROOT_PATH."/Public/data/poster_data.php";

        $this->assign($postdata);

        $poster_conf=$attr_data[$postdata['id']];
        $url = format_url($poster_conf['background']);
        $res = getimagesize($url);
        $back_info['width']=$res[0];
        $back_info['height'] = $res[1];
        $this->assign('back_info',$back_info);

        // print_r($res);exit;
        // print_r($postdata);
        $ewm_xy =!empty($poster_conf['param'])?$poster_conf['param']:C('COORDINATE');

        $size=!empty($poster_conf['font_size'])?$poster_conf['font_size']:20;
        $xyy = array();
        $xy = explode('|',$ewm_xy);

        // print_r($xy);
        $this->assign('xy',$xy);
        foreach($xy as $it){$xyy[] = explode(',',$it);}
        // print_r($xyy);exit();
        $this->assign('xyy',$xyy);

        foreach ($poster_conf as $key => $value) {
            $poster_conf['background'] = $url;
        }
// print_r($poster_conf);exit;
        $member_info = $poster_conf;
        // print_r($member_info);
        $this->assign($member_info);
        $this->display();
    }


	/**
     * 修改海报背景设置
     */
	public function poster_bj_update(){
        $id = I('id');
        var_dump($id);die;
		$map = array();$map['name'] = 'POSTER_LIST_ID';
		$num = M('Config')->where($map)->count();
		if($id){
			if($num){
				$ret = M('Config')->where($map)->setField('value', $id);
			}else{
				$add_data = array();$add_data['name'] = 'POSTER_LIST_ID';$add_data['value'] = $id;
				$add_data['type'] = 0;$add_data['title'] = '拓客海报背景';
				$add_data['status'] = 1;
				$ret = M('Config')->add($add_data);
			}
			S('DB_CONFIG_DATA', null);
			if($ret) $this->success('保存成功！',U('poster_bj'));
		}else{
			$this->error('参数错误！',U('poster_bj'));
		}
	}
	//参与统计
	public function cy_count(){
		$id = I('id');
		$url = U('Admin/Toker/index');
		if(empty($id)){$this->error('参数错误！',$url);}
		$postdata = I('post.');
		$map = array();
		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['uid'] = array('in',$uids);
			else $map['uid'] = array('in','');
		}
		if($postdata['get_status']){
			if($postdata['get_status'] == 1) $map['get_status'] = 1;
			else $map['get_status'] = 0;
		}
		if($postdata['add_from'] || $postdata['add_to']){
			$date_from = strtotime($postdata['add_from']);
			if($date_from)$map['time'][] = array('egt',$date_from);
			$date_to = strtotime($postdata['add_to'])+86399;
			if($date_to)$map['time'][] = array('lt',$date_to);
		}
		
        $map['act_id'] = $id;
		$order = 'id desc';
        $list = page_list('participation',$map,$order);
		
		$map_s = array();$map_s['id'] = $id;$map_s['class'] = 1;
		$coupon_send = M('ReceiptDraw')->where($map_s)->field('event_confine,end_time,aging')->find();
		$all_count = 0;
		foreach($list['list'] as $key => $val){
			$val['gift_name'] = '';
			$map_d = array();
			if($coupon_send['event_confine'] == 0){
				$aging = $val['time']+$coupon_send['aging']*60;//时效转换为秒
				$map_m=array();$map_m['id'] = array('gt',0);
				if($val['end_time'] >= $aging){//注册时间
					$map_m['reg_time'] = array(array('gt',$val['time']),array('lt',$aging));
				}else{
					$map_m['reg_time'] = array(array('gt',$val['time']),array('lt',$coupon_send['end_time']));
				}
				$uids = M('Member')->where($map_m)->getField('id',true);
				if($uids){
					$uidss = array_unique($uids);
					$map_d['receive_id']=array('in',$uidss);
				}else{
					$map_d['receive_id']=array('in','');
				}
			}
			$map_d['add_time'] = array(array('gt',$val['time']),array('lt',$coupon_send['end_time']));
			$map_d['send_id'] = $val['uid'];
			$val['count'] = M('CouponsDonation')->where($map_d)->group('receive_id')->field('id')->select();
			$val['count'] = count($val['count']);
			$where_p = array();$where_p['act_id'] = $id;$where_p['class'] = 1;
			$prize = M('Prize')->where($where_p)->field(true)->select();
			foreach ($prize as $k => $v) {
				if($val['count'] >= $v['goods_num']){
					if($v['prize_type'] == 1){
						$val['gift_name'] = $v['prize_goods'].'一个';
					}else if($v['prize_type'] == 2){
						$val['gift_name'] = '现金'.$v['prize_goods'];
					}else if($v['prize_type'] == 3){
						$val['gift_name'] = '积分'.$v['prize_goods'];
					}else{
						$map = array();$map['id'] = $v['prize_goods'];
						$coupons_name = M('Coupons')->where($map)->getField('title');
						$val['gift_name'] = $coupons_name.'优惠券一个';
					}
					break;
				}
			}
			$list['list'][$key] = $val;
			$all_count += $val['count'];
		}
		
		// echo "<pre>";print_r($list['list']);exit;
		$this->assign($list);
		$this->assign('id',$id);
		$this->assign('all_count',$all_count);
		$this->assign($postdata);
		$this->data_arr();
		$this->display();
	}
	private function data_arr(){
		$check_status_arr = array('1'=>'已领取','2'=>'未领取');
		$this->assign('check_status_arr',$check_status_arr);
	}
    public function poster_up(){
        $postdata = I('param.');
        // echo "<pre/>";print_r($postdata);exit();
        $map=array();
        $map['name']="SHARE_POSTER";
        $infos = M('Config')->where($map)->getField('value');
        $attr_data=unserialize($infos);
        if(!$attr_data){
            $attr_data = require_once ROOT_PATH."/Public/data/poster_data.php";
        }
        // print_r($postdata);die;
        if($postdata['member_img']){//会员推广海报
            $attr_data[1]['background']=$postdata['member_img'];
        }
        // 存库
        $new_arr = serialize($attr_data);
        $map=array();
        $map['name']="SHARE_POSTER";
        $infos = M('Config')->where($map)->count('id');
        $data['value']=$new_arr;
        if($infos){
            M('Config')->data($data)->where($map)->save();
        }else{
            $data['name']="SHARE_POSTER";
            $data['status']=1;
            $data['title']=$data['remark']='分享海报';
            M('Config')->add($data);
        }S('DB_CONFIG_DATA',null);
        // echo "<pre/>";print_r($arr);exit;
        // file_put_contents('Public/data/poster_datas.php',var_export($arr,true));exit;
        // arr2file('Public/data/poster_data.php',var_export($arr,true));
        $url = U('Admin/Toker/poster');
        $this->success('保存成功',$url);


    }
}
?>