<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\admin\controller\bbfxshop\fenxiao;

use app\admin\controller\bbfxshop\Base;
use think\Db;
use app\common\model\bbfxshop\User;


class Withdrawal extends Base
{
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Fenxiao;
    }
    
    public function check(){
        global $_W;
        $params = $this->param();
        
        $id = intval($params['id']);
        $status = intval($params['status']);
        $is_manual = intval($params['is_manual']);
        $remarks = $params['remarks'];

        $result = \app\common\model\bbfxshop\UserTixian::dakuan($id,$status,$is_manual,$remarks);

        if($result['code'] == 1){
            \app\admin\model\AdminLog::record("","",['id'=>$id,'status'=>$status],"bbfx_withdrawal_log");
        }
        
        __result($result); 
    }
    
    public function list(){
        $params = $this->param();
        
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):10;
        
        $where = array("a.uniacid"=>UNIACID,'a.withdrawal_type'=>2);
        
        $export = intval($params['export']);
        $status = $params['status'];
        $startTime = trim($params['startTime']);
        $endTime = trim($params['endTime']);
        
        $keyword = trim($params['keyword']);
        
        $type = max(1,intval($params['type']));
        if(!empty($keyword)){
            
            if($type == 1){
                $where['b.nickname'] = ['like',"%".$keyword."%"];
            }
            else if($type == 2){
                $where['a.realname'] = ['like',"%".$keyword."%"];
            }
            else if($type == 3){
                $where['b.mobile'] = $keyword;
            }
            else if($type == 4){
                $where['a.user_id'] = intval($keyword);
            }
        }
         
        if($status != ''){
            $where['a.status'] = intval($status);
        }
        
        $whereTime = [];
        if($startTime){
            $startTime = strtotime($startTime." 00:00:00");
            $where['a.createtime'] = ['>=',$startTime];
        }
        
        if($endTime){
            $endTime = strtotime($endTime." 23:59:59");
            $whereTime['a.createtime'] = ['<=',$endTime];
        }
        
        $export = intval($params['export']);
        if($export == 1){
            $psize = 999999;
            $pindex = 1;
        }
        
        $list = Db::name("bbfx_withdrawal_log")->alias("a")->join("bbfx_user b","a.user_id=b.id")->where($where)->field('a.*,b.avatar,b.nickname')->order('a.createtime desc')->limit(($pindex - 1) * $psize,$psize)->select();
    	
    	$provinces = file_get_contents(ROOT_PATH."public/assets/citycode.json");
        $provinces = json_decode($provinces,true);
        $province_list = [];

        foreach($provinces as $key => $val){
            $items = ['val'=>$val['val'],'name'=>$key,'items'=>[]];
            foreach($val['items'] as $k => $v){
                $items['items'][$v['val']] = ['val'=>$v['val'],'name'=>$k];
            }
            $province_list[$val['val']] = $items;
        }

    	foreach($list as &$val){
    	    $val['info'] = iunserializer($val['info']);
            if(!empty($val['info'])){
                $prov_id = $val['info']['prov_id'];
                $area_id = $val['info']['area_id'];
                $val['info']['prov_id'] = $province_list[$prov_id]['name'];
                $val['info']['area_id'] = $province_list[$prov_id]['items'][$area_id]['name'];
            }
    	    $val['avatar'] = cdnurl($val['avatar']);
    	    $val['skqrcode'] = cdnurl($val['skqrcode']);
    	    $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
    	    if($val['paytime']){
    	        $val['paytime'] = date("Y-m-d H:i:s",$val['paytime']);
    	    }
    	    else{
    	        $val['paytime'] = "";
    	    }
    	     
    	    if($export == 1){
    	        $status = "待审核";
    	        if($val['status'] == 1){
    	            $status = "已打款";
    	        }
    	        else if($val['status'] == 2){
    	            $status = "已驳回";
    	        }
    	        
    	        $info = "";
    	        if($val['type'] == 3){
    	            $info .= $val['realname'].PHP_EOL;
    	            $info .= $val['alipay'];
    	        }
    	        else if($val['type'] == 4){
    	            $cardinfo = $val['info'];
    	            if(!empty($cardinfo)){
    	                $info .= "姓名：".$cardinfo['name'].PHP_EOL;
        	            $info .= "银行名称：".$cardinfo['bankname'].PHP_EOL;
        	            $info .= "开户行：".$cardinfo['bankopen'].PHP_EOL;
        	            $info .= "省份：".$cardinfo['prov_id'].PHP_EOL;
        	            $info .= "城市：".$cardinfo['area_id'].PHP_EOL;
        	            $info .= "银行卡号：".$cardinfo['card_no'].PHP_EOL;
        	            $info .= "手机号：".$cardinfo['mobile_no'].PHP_EOL;
        	            $info .= "证件号码：".$cardinfo['cert_no'].PHP_EOL;
        	            $info .= "证件有效期开始日期：".$cardinfo['cert_begin_date'].PHP_EOL;
        	            if($cardinfo['cert_validity_type'] === '0'){
        	                $info .= "证件有效期结束日期：".$cardinfo['cert_end_date'];
        	            }
        	            else{
        	                $info .= "证件有效期结束日期："."长期有效";
        	            }
    	            }
    	            else{
    	                $info .= $val['bankname'].PHP_EOL;
        	            $info .= $val['bankopen'].PHP_EOL;
        	            $info .= $val['realname'].PHP_EOL;
        	            $info .= $val['bankcard'];
    	            }
    	        }
    	        
    	        $export_list[] = array(
    	            $val['id'],
    	            $val['nickname'],
    	            $val['user_id'],
    	            $val['money'],
    	            $val['realmoney'],
    	            $val['service_charge'],
    	            $status,
    	            $info,
    	            $val['paytime'],
    	            $val['createtime']
    	        );
    	    }
    	}
    	unset($val);
    	
    	if($export == 1){
    	    $headArr = ["ID","用户昵称","用户ID","提现金额","实际到账","手续费","状态","提现信息","审核时间","提现时间"];
    	    $result = \app\common\model\PhpOffice::excelExport("tx-".date("Y-m-d"),$headArr,$export_list);
    	}
      
    	$total = Db::name("bbfx_withdrawal_log")->alias("a")->join("bbfx_user b","a.user_id=b.id")->where($where)->where($whereTime)->count();
        
        __success("操作成功",['total'=>$total,'list'=>$list]);
    }
    
    public function delete(){
        $this->commonDelete('bbfx_withdrawal_log');
    }
    
}