<?php
/** App登录控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <xiaoq@570035208.com>
 */

namespace Wxsmall\Controller;
class ExtensionController extends BaseController {
	public function _initialize(){
        	parent::_initialize ();
    	}
	/**
	 * 全名拓客
	 */
	public function index(){
         // unlink(ROOT_PATH.'/uploads/image/qrcode/small_haibao_5.jpg');
         // die;
        $return=array('status'=>1);
        $param = I('param.');
        // print_r($param);
        if($param['is_littleshop']==1){
            $user_info=$this->checkuserauth();
            $param_data['id']= $param['lshop_id'];
            $param_data['is_littleshop'] = $param['is_littleshop'];
        }else{
            $user_info=$this->checkuserauth();
            $param_data['id']= $user_info['id'];
        }
        
        // print_r($user_info);exit;
        
        $uid=$user_info['id'];
        $val = unserialize(C('AFFILIATE'));
        $return['data']['affconfig']=$val;
        $return['data']['poster_img']=C('POSTER_IMG');
        $aff = unserialize(getconfig('AFFILIATE'));
        $share_img = $aff['config']['review_img'];
        $return['data']['share_img']= format_url($share_img);
        //推广
        
        $data = D('Common/Member')->extension($param_data);
        // print_r($data);
        $tui_map = array();
        $tui_map['parent_id']=$user_info['id'];
        $tui_map['is_littleshop']=0;
        $tui_count = M('Member')->where($tui_map)->count();
        $rank_name = idtoname($user_info['rank_id'],'MemberRank','name');
        // $return['data']['rank_name']=$rank_name;
        $user_info['tui_count'] = $tui_count;
        $user_info['rank_name'] = $rank_name;
        $integral_list=D('Common/Member')->integral();
        $user_info['allpoints']=intval($integral_list['allpoints']);
        $return['data']['user_info']=$user_info;
        
        
        $return['data']['info']=$data;
        // 已参与的活动
        $where=array();
        $where['uid'] = $uid;
        $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;
            }
        }
        if($quan){
            foreach($quan as $key=>$val){
                $quan[$key]['start_time']=time_format($val['start_time']);
                $quan[$key]['end_time']=time_format($val['end_time']);
            }
        }
        $return['data']['quan']=$quan;
        $return['web_name']=getconfig('WEB_SITE_NAME');
        $this->ajaxReturn($return);
	}

    /**
     * 推广记录
     */
	public function extension_list(){
        $return=array('status'=>1);
		$param = I('param.');
		$user_info=$this->checkuserauth();
		$param['uid']=$user_info['id'];
            //推广分成
            $data = D('Common/Member')->my_extension($param);
            $return['data'] = $data['extension_list'];
            $return['comm_money'] = $data['comm_money'];
            if(empty($data['extension_list']['list'])){
                $return['status']=2;
                $return['msg']="没有更多数据了";
                $this->ajaxReturn($return);
            }
		$this->ajaxReturn($return);
	}

    /**
     * 推荐关系
     */
    public function extension(){
        $return=array('status'=>1);
        $param = I('param.');
        $user_info=$this->checkuserauth();
        $param['uid']=$user_info['id'];
        $data = D('Common/Member')->extension_list($param);
        foreach($data['extension_list']['list'] as $key=>$val){
            $data['extension_list']['list'][$key]['user_childname']=show_user_name($val['id']);
            $data['extension_list']['list'][$key]['user_parentname']=show_user_name($val['parent_id']);
            $data['extension_list']['list'][$key]['reg_time']=time_format($val['reg_time']);
            $data['extension_list']['list'][$key]['count_tui']=count_tui($val['id']);
        }
        $return['data'] = $data['extension_list'];
        if(empty($data['extension_list']['list'])){
            $return['status']=2;
            $return['msg']="没有更多数据了";
            $this->ajaxReturn($return);
        }
        $this->ajaxReturn($return);
    }

    /**
     * 我的推广海报
     */
    public function member_poster(){
        $return=array('status'=>1);
        $param = I('param.');
        $user_info=$this->checkuserauth();
        $id=$user_info['id'];
        $shopewm=tgercode($id);

        if($shopewm['status'] && $shopewm['url']){
            $wximg_url=$shopewm['url'];
        }else{
            $url = U('Mobile/Login/perfect',array('eid'=>$id),true,true);
            $wximg_url =U('Home/Qrcode/index',array('data'=>urlencode($url)),true,true);
        }
        $return['data']=$wximg_url;
        $this->ajaxReturn($return);
    }

    /**
     * 充值
     */
    public function rechange(){
        $return=array('status'=>1);
        $param = I('param.');
        $user_info=$this->checkuserauth();
        $uid=$user_info['id'];
        $map=array();
        $map['id']=$uid;
        $member_info=M("Member")->where($map)->field(true)->find();
        $return['data']['member_info']=$member_info;
        $recharge_info = C('FIXED_RECHARGE');
        if($recharge_info){
            $recharge_arr = explode(',',$recharge_info);
            $return['data']['recharge_arr']=$recharge_arr;
        }
        $map=array();
        $map['consumption_type'] = array('in','1,3');
        $list = $this->lists('ManypointsRule',$map);
        $return['data']['list']=$list;
        $this->ajaxReturn($return);
    }

    /**
     * 提交充值
     */
    public function sub_recharge(){
        $return=array('status'=>1);
        $postdata=I('param.');
        $user_info=$this->checkuserauth();
        $postdata['uid']=$user_info['id'];
        $result=D('Common/Withdrawals')->recharge($postdata);
        $result['msg']=$result['info'];
        $this->ajaxReturn($result);
    }

    /**
     * 提现
     */
    public function withdrawals(){
        $return=array('status'=>1);
        $postdata=I('param.');
        $user_info=$this->checkuserauth();
        $postdata['uid']=$user_info['id'];
        $map=array();
        $map['id']=$postdata['uid'];
        $member_info=M("Member")->where($map)->field(true)->find();
        $payfree=C('TIXIAN_FREE');	//提现手续费
        if($payfree<1)$num=$payfree*100;
        $return['data']['num']=$num;
        $tx_min_money=C('USER_MIN_GET_MONEY');	//最低提现金额
        $MEMBER_BANK=idtoname('MEMBER_BANK','Config','value,extra','name');
		$banks_arr=array_values(strtoarray($MEMBER_BANK['extra']));
		$banks_value=array_unique(explode(',',$MEMBER_BANK['value']));
		foreach($banks_arr as $key=>$v){if(in_array($v,$banks_value))$banksarr[$key+1]=$v;}
        $return['data']['tx_min_money']=$tx_min_money;
        $return['data']['banks']=$banks;
        $return['data']['member_info']=$member_info;
        $return['data']['user_account_date']=C("USER_ACCOUNT_DATE");//到账时间
        $return['data']['tixian_free']=C("TIXIAN_FREE");
        $return['data']['sms_tx_verify']=C("SMS_TX_VERIFY");
        $return['data']['mobile_true_time']=C("MOBILE_TURE_TIME");
        $this->ajaxReturn($return);
    }

    /**
     * 提交提现申请
     */
    public function sub_ti(){
        $return=array();
        $postdata=I('param.');
        $user_info=$this->checkuserauth();
        $postdata['uid']=$user_info['id'];
        $result=D('Common/Withdrawals')->get_money($postdata);
        $result['msg']=$result['info'];
        $this->ajaxReturn($result);
    }
}