<?php
/**
 * 会员账户
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author zhaoh <1070309178@qq.com>
 */

namespace Mobile\Controller;
use Think\Controller;
class CapitalController extends BaseController {
	public function _initialize(){
		parent::_initialize ();
		if(!MID){
			cookie('forward',U('Mobile/'.CONTROLLER_NAME.'/'.ACTION_NAME));
			$this->redirect('Mobile/Login/index');
		}
		if($this->m_info['is_tmp']==1&&!$this->m_info['mobile']){
			session('format_url',U('Mobile/'.CONTROLLER_NAME.'/'.ACTION_NAME));
			$this->redirect('Mobile/Login/perfect');
		}elseif($this->m_info['is_tmp']==1&&$this->m_info['mobile']){
			$this->error('您已经领取过会员卡了！');exit;
		}
		$this->account_type=strtoarray(C('ACCOUNT_TYPE'));$this->assign('account_type',$this->account_type);
		$this->account_status=strtoarray(C('ACCOUNT_STATUS'));$this->assign('account_status',$this->account_status);
		
		$this->order_pay=strtoarray(C('ORDER_PAY'));$this->assign('order_pay',$this->order_pay);
		$this->account_status = array('1'=>'充值','2'=>'提现','3'=>'管理员调节','4'=>'消费','5'=>'系统','8'=>'退款','10'=>'其它');
        $this->assign('account_status',$this->account_status);
		$user_info=M('Member')->where('id='.MID)->field(true)->find();
		$this->assign('user_info',$user_info);
		$grade_info = getRankMoney(MID);//获取等级差额
		// user_ranks(MID);
		$this->assign($grade_info);
   	}
	//余额变更
    public function index($type = 0){
		$page=I('param.page');
		$page=$page?$page:1;
		$map['uid']=MID;
        $map['type']=array('in',array(1,2));
        $map['role']=0;
        $pagesize=empty($pagesize)?C('WEB_DEFAULT_PAGE'):$pagesize;
		$info=page_list('AccountLog',$map,'change_time desc,id desc',true,$pagesize,'',$page);
        if($info['count'] > $pagesize*$page){
            $info['show'] = 1;
        }else{
            $info['show'] = 0;
        }
		if ($type == 1) return $info;
		$this->assign($info);
        $this->assign('tplseo_title', '账户余额' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '账户余额');
        $this->display();
    }
	public function get_index_list(){
        $return['status'] = 0;
        $data = $this->index(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('index_list');
        $return['data'] = $list_html;
        $return['show'] = $data['show'];
        $this->ajaxReturn($return);
    }
	//充值明细
    public function recharge($type = 0){
		$page=I('param.page');
		$page=$page?$page:1;
		$map['uid']=MID;$map['type']=2;
		$info=page_list('OrderInfo',$map,'id desc,addtime desc',true,10,'',$page);
		$where['uid'] = MID;
		$where['status'] = array('IN','1,12');
		$where['type'] = 1;
		$where['role']=0;
		$shop_info = M('AccountLog')->where($where)->select();
		if(!empty($info['list'])){
			$info['list'] = array_merge_recursive($info['list'],$shop_info);
		}else{
			$info['list'] = $shop_info;
		}
		$info['count'] =count($info['list']);
		
		
		if ($type == 1) return $info;
		// echo "<pre>";
		// print_r($info['list']);exit;
		$this->assign($info);
        $this->assign('tplseo_title', '充值明细' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '充值明细');
        $this->display();
    }
	public function get_recharge_list(){
        $return['status'] = 0;
        $data = $this->recharge(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
        $list_html = $this->fetch('recharge_list');
        $return['data'] = $list_html;
        $this->ajaxReturn($return);
    }
	//继续支付
    public function go_pay(){
		$order_id=I('order_id');
		$map['is_delete'] = 0;
        $map['order_id'] = array("like", "%," . $order_id . ",%");
		$log_id = M("PayLog")->where($map)->getField('id');
		if($log_id){
			$return['status']=1;$return['url']=U('Home/Pay/go_buy','',false,true).'/?id='.$log_id;
		}else{
			$return['status']=0;$return['msg']='支付信息错误';
		}
		$this->ajaxReturn($return);
    }
	//提现明细
    public function withdraw($type = 0){
		$page=I('param.page');
		$page=$page?$page:1;
		$map['uid']=MID;
		$info=page_list('MemberWithdrawals',$map,'id desc,update_time asc',true,10,'',$page);
		if ($type == 1) return $info;
		$this->assign($info);
        $this->assign('tplseo_title', '提现明细' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '提现明细');
		$this->get_status();//提现状态
        $this->display();
    }
	public function get_withdraw_list(){
        $return['status'] = 0;
        $data = $this->withdraw(1);
        if (empty($data['list'])) $this->ajaxReturn($return);

        $return['status'] = 1;
        $this->assign($data);
		$this->get_status();//提现状态
        $list_html = $this->fetch('withdraw_list');
        $return['data'] = $list_html;
        $this->ajaxReturn($return);
    }
	private function get_status($status){
		$status_type=array(0=>'处理状态',1=>'未处理',2=>'已处理',3=>'已关闭');
        $status_selected='';
		foreach($status_type as $k=>$v){
			if($status==$k){
				$status_selected.="<option selected='selected' value='$k'>$v</option>";
			}else{
				$status_selected.='<option value='.$k.'>'.$v.'</option>';
			}
		}
		$this->assign('status_type',$status_type);
		$this->assign('status_selected',$status_selected);
	}
    public function money_change_cont(){
        $id = I('param.id');
        if(!$id){
            $this->error('参数错误');
        }
        $map = array();$map['id'] = $id;
        $info = M('AccountLog')->where($map)->find();
        if(!$info){
            $this->error('参数错误');
        }
        $this->assign($info);
        $this->display();
    }

    //907新加礼品卡
    public function gift_card(){
    	$this->assign('tplseo_title', '我的礼品卡' . '_' . C('WEB_SITE_TITLE'));
        $this->assign('page_title', '我的礼品卡');
    	$this->display();
    }
    public function activation(){
    	$return=array('status'=>0,'msg'=>'');
        $data = I('post.');

        $gmodel = M('GiftCard');
        if($data['card_sn'] && $data['card_pass']){
            $arrs=$save=$where=array();
            $final['card_sn'] = $data['card_sn'];
            $final['password'] = $data['card_pass'];
            $final['status'] = 0;

            $arrs['end_time']=array('egt',NOW_TIME);
            $map['_complex'] = $arrs;
            $map['end_time'] = array('eq',0);
            $map['_logic'] = 'or'; 
            $final['_complex'] = $map;
            $is_true = $gmodel->where($final)->field(true)->find();
            if(!$is_true || empty($is_true)){
            	$return['msg'] = '该礼品卡不存在，或卡号激活码错误';
            	$this->ajaxReturn($return);exit();
            }
            $save['bind_uid']=MID;
            $save['bind_time']=NOW_TIME;
            $save['status']=1;
            $where['id'] = $is_true['id'];
            $save_id = $gmodel->where($where)->save($save);
            if($save_id) $res = account_change(MID,$is_true['card_money'],1,10,'礼品卡（'.$is_true['card_sn'].'）激活，余额账户增加'.$is_true['card_money'].'元');
            if($res){
            	$return['status'] = 1;
            	$return['msg'] = '激活成功';
            }
        }else{
            $return['msg'] = '请输入礼品卡号或激活码';
        }
        $this->ajaxReturn($return);

    }
    //end
}
?>