<?php
/**
 * 推广中心
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author lss <lss@qq.com>
 */
namespace Mobile\Controller;
use Think\Controller;
class ExtensionController extends BaseController
{
    public function _initialize()
    {
        parent::_initialize();
        if (!MID) {
			$memberdata = session('memberdata');
			if($memberdata)$this->autologinupdate($memberdata);
            $this->redirect('Mobile/Login/index');
        }
		if($this->m_info['is_tmp']==1&&!$this->m_info['mobile']){
			session('format_url',U('Mobile/Extension/member_invite'));
			$this->redirect('Mobile/Login/perfect');
		}elseif($this->m_info['is_tmp']==1&&$this->m_info['mobile']){
			$this->error('您已经领取过会员卡了！');exit;
		}
    }
    //微信分享相关配置
    private function signPackage(){ 
		$wx = new \Common\Vendor\Jssdk();
		$signPackage = $wx->getSignPackage();
		$this->assign('signPackage',$signPackage);
    }
    /**
     *推广
     */
    public function member_invite(){
        //推广
		$val = unserialize(C('AFFILIATE'));
        $this->assign('val', $val);
        // $this->assign('item', $val['item']);
        //推广
        $data = D('Common/Member')->extension();
        // 已参与的活动
        $member_info = session('member_info');
        $where['uid'] = $member_info['id'];
        $participation = M('Participation')->where($where)->field(true)->select();
        foreach ($participation as $k => $v) {
            $get_status[$v['act_id']] = $v['get_status'];
        }
        // 全民发券
        $map['status'] = 1;
        $map['class'] = 1;
        $quans = M('ReceiptDraw')->where($map)->field('id,draw_name,start_time,end_time,logo_url')->select();
        foreach ($quans as $k => $v) {
            if($get_status[$v['id']] == 0 && isset($get_status[$v['id']])){//已领取奖励的不在列表显示
                $quan[$k] = $v;
            }elseif(!isset($get_status[$v['id']]) && $v['end_time'] >= time()){
                $quan[$k] = $v;
            }
        }
        $this->assign('quan',$quan);
        $this->assign($data);
        $this->assign('extensionname', '推广');
        $this->assign('tplseo_title', '全民拓客' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '全民拓客');
        $this->display();
    }

    /**
     *推广分成
     */
    public function member_invite_list($is_ajax = 0){
        $postdata = I('param.');
        //推广分成
        $data = D('Common/Member')->my_extension($postdata);
		if ($is_ajax == 1) return $data;
        // dump($data);exit();
        $this->assign($data);
        $this->assign($postdata);
        $this->assign('tplseo_title', '分佣记录' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '分佣记录');
        $this->display();
    }
	public function invite_list(){
        $return['status'] = 0;
        $data = $this->member_invite_list(1);
        if (empty($data['extension_list']['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('invite_list');
        $return['data'] = $list_html;
		$return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    /**
     *推广分成
     */
    public function member_extension_list($is_ajax = 0){
        $postdata = I('param.');
        //推广分成
        $data = D('Common/Member')->extension_list($postdata);
		if ($is_ajax == 1) return $data;
        $this->assign($data);
        $this->assign($postdata);
        $this->assign('tplseo_title', '推广列表' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '推荐关系');
        $this->display();
    }
	public function extension_list(){
        $return['status'] = 0;
        $data = $this->member_extension_list(1);
        if (empty($data['extension_list']['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('extension_list');
        $return['data'] = $list_html;
		$return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
    public function tuoke_fq(){
		$act_id = I('act_id');
        $map['id'] = $act_id;
        $map['class'] = 1;
        $data = M('ReceiptDraw')->where($map)->field(true)->find();
        if($data['end_time'] <= time()){//判断活动是否结束
            $data['status'] = 1;//未结束
        }else{
            $data['status'] = 0;//结束
        }
        // 推广记录
        $member_info = session('member_info');
        if($data['event_confine'] == 0){//新用户
            $status = 0;
        }else{
            $status = 1;
        }
        $uid = $member_info['id'];
        $map_p['uid'] = $uid;
        $map_p['act_id'] = $act_id;
        $count = M('Participation')->where($map_p)->field(true)->find();
        $where = array();$where['send_id'] = $uid;
        $where['add_time'] = array(array('gt',$count['time']),array('lt',$data['end_time']));
        $records = M('CouponsDonation')->where($where)->group('receive_id')->field(true)->select();
        if($records){
			if($status == 0){
				$receive_id = $receive_ids = array();
				foreach ($records as $k => $v) {
					$receive_id[]=$v['receive_id'];
				}
				$receive_ids = array_unique($receive_id);
				if($receive_ids){
					$aging = $count['time']+$data['aging']*60;//时效转换为秒
					$map_m=array();$map_m['id'] = array('in',$receive_ids);
					if($data['end_time'] >= $aging){//注册时间
						$map_m['reg_time'] = array(array('gt',$count['time']),array('lt',$aging));
					}else{
						$map_m['reg_time'] = array(array('gt',$count['time']),array('lt',$data['end_time']));
					}
					$uids = M('Member')->where($map_m)->getField('id',true);
					if($uids){
						foreach($records as $k=>$v){
							if(!in_array($v['receive_id'],$uids))unset($records[$k]);
						}
					}else{
						$records = array();
					}
				}else{
					$records = array();
				}
			}
		}
        $record = $records;

        // 判断是否参与该活动
        if($count){
            $this->assign('record',$record);
            $data['get_status']=$count['get_status'];
        }else{
            $data['canyu'] = 0;
        }
        $data['start_time'] = date('Y-m-d',$data['start_time']);
        $data['end_time'] = date('Y-m-d',$data['end_time']);
        // 规则
        $prize = M('Prize')->where('act_id = '.$act_id)->field(true)->select();
        $this->assign('data',$data);
		$share_url = U('Extension/tuoke_fq',array('act_id'=>$act_id));
        $this->assign('share_url',$share_url);
        $this->assign('prize',$prize);
        $this->signPackage();
        $this->display();
    }
    // 参与活动
    public function can_award(){
        $act_id = I('get.act_id');
        $member_info = session('member_info');
        $data['uid'] = $map['uid'] = $member_info['id'];
        $data['act_id'] = $map['act_id'] = $act_id;
        $status = M('Participation')->where($map)->count();
        if($status == 0){
            $data['time'] = NOW_TIME;
            M('Participation')->add($data);
            $return['status'] = 0;
        }else{
            $return['status'] = 1;
        }
        $this->ajaxReturn($return);
    }
    // 立即领取
    public function get_award(){
        $map['act_id'] = I('act_id');
        $member_info = session('member_info');
        $map['uid'] = $member_info['id'];
        $status = M('Participation')->where($map)->getfield('get_status');
        if($status == 1){
            $return['status'] = 0;
            $return['msg'] = '已领取！';
        }else{
            $Weixin = new \Common\Vendor\WeixinCoupon();
            $get_award = $Weixin->get_award($map);
            $s_data['get_status'] = 1;
            if($get_award)M('Participation')->where($map)->save($s_data);
            if($get_award['status']	 == 1){
                $return['status'] = 1;
                $return['url'] = U('Mobile/MyMoney/win_gift');
                $return['msg'] = $get_award['data']['prize_name'].'领取成功！'.$get_award['mag'];
            }else{
                $return['status'] = 0;
                $return['msg'] = $get_award['mag'];
            }          
        }
        $this->ajaxReturn($return);
    }
	/**
     *推广海报
     */
    public function member_poster(){
		$member_info = session('member_info');
		$id = $member_info['id'];
        // $shopewm=tgercode($id,2);
        $shopewm=tgercode($id,2);
	
        if($shopewm['status'] && $shopewm['url']){
            $wximg_url=$shopewm['url'].'?'.NOW_TIME;
        }else{
            $url = U('Mobile/Login/perfect',array('eid'=>$id),true,true);
            $wximg_url =U('Home/Qrcode/index',array('data'=>urlencode($url)),true,true);
        }
        $this->assign('wx_img',$wximg_url);
        $title = "分享海报";
        $description = '分享海报' . '_' . C('WEB_SITE_TITLE');
        $share_url=U('Mobile/Extension/member_poster',array('eid'=>$id));
        $this->assign('title',$title);
        $this->assign('description',$description);
        $this->assign('share_url',$share_url);
        //推广
        // $data = D('Common/Member')->extension();
        $this->assign('tplseo_title', '分享海报' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '分享海报');        
        $this->display();
    }
    public function get_member_poster(){
        $return=array('status'=>1);
        $member_info = session('member_info');
        $id = $member_info['id'];
        // $shopewm=tgercode($id,2);
        $shopewm=tgercode($id);
        // unlink(ROOT_PATH.'/uploads/image/qrcode/haibao_5.jpg');
        // die;
        if($shopewm['status'] && $shopewm['url']){
            $wximg_url=$shopewm['url'].'?'.NOW_TIME;
        }else{
            $return['status']=0;
            $return['msg']=$shopewm['msg'];
            $this->ajaxReturn($return);
            $url = U('Mobile/Login/perfect',array('eid'=>$id),true,true);
            $wximg_url =U('Home/Qrcode/index',array('data'=>urlencode($url)),true,true);

            $url = U('Mobile/Login/perfect',array('eid'=>$id),true,true);
            $wximg_url =U('Home/Qrcode/index',array('data'=>urlencode($url)),true,true);
         
        }
        $share_url=U('Mobile/Extension/member_poster',array('eid'=>$id));
        $return['pic_url']=$wximg_url;
        $return['share_url']=$share_url;
        $this->ajaxReturn($return);
    }
    /**
     *判断登陆
     */
    public function status(){
        $method = I('get.method');
        $member_info = session('member_info');
        $mobile = $member_info['mobile'];
        if($mobile){//会员
            $return['status'] = 1;
            $return['url'] = U('Mobile/Extension/'.$method,array('eid'=>MID));
        }else{//粉丝
            $return['status'] = 0;
            $return['info'] = '请先完善资料';
            $return['url'] = U('Mobile/login/perfect');
        }
        $this->ajaxReturn($return);
    }
    /**
    *申请提现
    */
    public function comm_money(){
        $user_info=M('Member')->where('id='.MID)->field(true)->find();
        $this->assign('user_info',$user_info);
        $payfree=C('TIXIAN_FREE');
        if($payfree<1)$num=$payfree*100;
        $this->assign('num',$num);
        $this->assign('tplseo_title', '我要提现' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '我要提现');
        $this->getmemberbank();
        $this->display();
    }
    //提现银行信息
    private function getmemberbank(){
        $map['uid']=MID;$map['status']=1;
        $bank_info=M('MemberBank')->where($map)->field('name,bankname,bankcode')->find();
        
        if($bank_info['bankcode']){
            $length=strlen($bank_info['bankcode']);
            if(str_exists('@',$bank_info['bankcode'])){
                $bankcodearr=explode('@',$bank_info['bankcode']);
                $bank_info['bankcode']=substr($bankcodearr[0],0,(strlen($bankcodearr[0])-4)).'****@'.$bankcodearr[1];
            }else{
                $bank_info['bankcode']=substr($bank_info['bankcode'],0,($length-4)).'****';
            }
        }
        $this->assign('bank_info',$bank_info);
        $banks=array_unique(explode(',',C('MEMBER_BANK')));
        foreach($banks as $v){$banksarr[$v]=$v;}
        $this->assign('banks',$banksarr);

    }
    /**
     *推广
     */
    public function my_invite(){
        //推广
        $bean_unit_arr=array(1=>'积分',2=>'现金');
        $this->assign('bean_unit_arr', $bean_unit_arr);
        $val = unserialize(C('AFFILIATE'));
        $this->assign('val', $val);
        // $this->assign('item', $val['item']);
        //推广
        $data = D('Common/Member')->extension();
        // 已参与的活动
        $member_info = session('member_info');
        $where['uid'] = $member_info['id'];
        $participation = M('Participation')->where($where)->field(true)->select();
        foreach ($participation as $k => $v) {
            $get_status[$v['act_id']] = $v['get_status'];
        }
        // if($member_info['is_littleshop']==1){
        //     $lshop_id=intval(D('Wxsmall/Login')->get_lshop_id($member_info['id']));
        //     $little_info =M('LittleShop')->where('id='.$lshop_id)->find();
        //     $tui_map['parent_id']=$little_info['id'];
        //     // $tui_map['review_status']=2;
        //     $tui_map['status']=1;
        //     $tui_count = M('LittleShop')->where($tui_map)->count();
        //     $member_info['tui_count'] = $tui_count;
        // }else{
            $tui_map['parent_id']=$member_info['id'];
            $tui_map['is_littleshop']=0;
            $tui_count = M('Member')->where($tui_map)->count();
            $rank_name = idtoname($user_info['rank_id'],'MemberRank','name');
            $member_info['tui_count'] = $tui_count;
        // }
        // 全民发券
        $map['status'] = 1;
        $map['class'] = 1;
        $quans = M('ReceiptDraw')->where($map)->field('id,draw_name,start_time,end_time,logo_url')->select();
        foreach ($quans as $k => $v) {
            if($get_status[$v['id']] == 0 && isset($get_status[$v['id']])){//已领取奖励的不在列表显示
                $quan[$k] = $v;
            }elseif(!isset($get_status[$v['id']]) && $v['end_time'] >= time()){
                $quan[$k] = $v;
            }
        }
        $integral_list=D('Common/Member')->integral();
        $this->assign('allpoints',intval($integral_list['allpoints']));
        $this->assign('quan',$quan);
        $this->assign($data);
        $this->assign('user_info',$member_info);

        $this->assign('extensionname', '推广');
        $this->assign('tplseo_title', '全民拓客' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '全民拓客');
        $this->display();
    }
}