<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 客户收益管理
 */
namespace app\allwin\controller;
use app\allwin\model\Bank  as ModelBank;
use app\allwin\model\BankBill;
use app\allwin\model\BankCash;
use app\allwin\model\AllwinStore;
use think\facade\Request;
use think\helper\Time;
use app\common\model\SystemUserBank;
use app\common\model\SystemUser;

class Bank extends Common{


    public function initialize(){
        parent::initialize();
        $this->assign('pathMaps', [['name'=>'账号余额','url'=>url("bank/index")]]);
    }

    /**
     * 客户收益管理
     * @return void
     */
    public function index(int $types = 0){
        $condition = [];
        $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
        $time = Request::param('time/d',0);
        if($time){
            switch ($time) {
                case 2:
                    list($start, $end) = Time::yesterday();
                    break;
                case 30:
                    list($start, $end) = Time::month();
                    break;
                case 60:
                    list($start, $end) = Time::lastMonth();
                    break;
                default:
                    list($start, $end) = Time::today();
                    break;
            }
            $condition[] = ['update_time','>=',$start];
            $condition[] = ['update_time','<=',$end];
        }
        $starttime = Request::param('starttime/s');
        $endtime = Request::param('endtime/s');
        if($starttime){
            $condition[] = ['update_time','>=',strtotime($starttime)];
        }
        if($endtime){
            $condition[] = ['update_time','<=',strtotime($endtime)];
        }
        $view['receipts']   = BankBill::where($condition)->where('money','>',0)->sum('money');  //收入
        $view['expenses']   = BankBill::where($condition)->where('money','<',0)->sum('money');  //支付
        $view['time']       = $time;
        $view['starttime']  = $starttime;
        $view['endtime']    = $endtime;
        //查询帐号
        switch ($types) {
            case 1:
                $order = 'income_money desc';
                break; 
            case 2:
                $order = 'money desc';
                break;
            case 3:
                $order = 'lack_money desc';
                break;
            case 4:
                $order = 'shop_money desc';
                break;  
            default:
                $order = 'update_time desc';
                break;
        }
        $keyword        = trim(input('get.keyword','','htmlspecialchars'));
        $view['list']   = ModelBank::bank($this->member_miniapp_id,$keyword)->order($order)->paginate(20,false,['query'=>['types' => $types]]);
        $view['income'] = ModelBank::bank($this->member_miniapp_id,$keyword)->sum('income_money');
        $view['points'] = ModelBank::bank($this->member_miniapp_id,$keyword)->sum('shop_money'); 
        $view['money']  = ModelBank::bank($this->member_miniapp_id,$keyword)->sum('money'); 
        $view['keyword']= $keyword;
        $view['types']  = $types;
        return view()->assign($view);
    }

    /**
     * 客户收益记录
     * @return void
     */
    public function bill(int $uid){
        $this->path[] = ['name'=>'账单管理','url'=>'javascript:;'];
        $where['user_id'] = $uid;
        $view['list']     = BankBill::bill($where);
        $view['pathMaps'] = $this->path;
        return view()->assign($view);
    } 
    

    /**
     * 客户提现
     */
    public function cash($types = 0){ 
        $keyword = trim(input('get.keyword','','htmlspecialchars'));
        $condition = [];
        $condition['member_miniapp_id'] = $this->member_miniapp_id;
        if(!empty($keyword)){
            //$condition['system_user.phone_uid'] = $keyword;
        }
        switch ($types) {
            case 1:
                $state = -1;
                break;
            case 2:
                $state = 1;
                break;
            default:
                $state = 0;
                break;
        }
        $condition['state'] = $state;
        $view['list']       = BankCash::where($condition)->order('id desc')->paginate(20,false,['query' => ['types' => $types]]);
        $view['money']      = ModelBank::bank($this->member_miniapp_id,$keyword)->sum('money'); 
        $view['lack_money'] = ModelBank::bank($this->member_miniapp_id,$keyword)->sum('lack_money'); 
        $view['now_money']  = BankCash::where(['member_miniapp_id' =>$this->member_miniapp_id,'state' => 0])->sum('money'); 
        $view['realmoney']  = BankCash::where(['member_miniapp_id' =>$this->member_miniapp_id,'state' => 1])->sum('realmoney'); 
        $view['back_money'] = BankCash::where(['member_miniapp_id' =>$this->member_miniapp_id,'state' => -1])->sum('money'); 
        $view['keyword']    = $keyword;
        $view['types']      = $types;
        $view['pathMaps']   = [['name'=>' 提现管理','url' => url("bank/cash")]];
        return view()->assign($view);
    }

    /**
     * 客户审核
     */
    public function cashpass(int $id){
        if(request()->isAjax()){
            $data = [
                'id'         => input('post.id/d'),
                'ispass'     => input('post.ispass/d'),
                'miniapp_id' => $this->member_miniapp_id,
                'realmoney'  => input('post.realmoney/f')
            ];
            $validate = $this->validate($data,'Bank.cash');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = ModelBank::isPass($data,$this->member_miniapp_id);
            return json($result);
        }else{
            $cash = BankCash::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $id])->find();;
            if(empty($cash)){
                $this->error('未找到到申请提现内容');
            }
            $setting   = model('AllwinConfig')->getConfig($this->member_miniapp_id);
            $is_manage = AllwinStore::manageStore($cash->user_id);
            if(empty($is_manage)){
                $realmoney = money($cash->money - $cash->money*($setting->cash_charges/1000));
            }else{
                $realmoney = $cash->money;
            }
            $view['cash']      = $cash;
            $view['realmoney'] = $realmoney;
            $view['bank']      = ModelBank::where(['user_id' => $cash->user_id])->find();
            $view['info']      = SystemUserBank::where(['user_id' => $cash->user_id])->find();
            $view['wechat']    = SystemUser::where(['id' => $cash->user_id])->field('id,miniapp_uid,phone_uid,official_uid')->find();
            $view['config']    = $setting ;
            return view()->assign($view);
        }
    }
}