<?php

namespace app\api\controller\school\newactivity;

use app\api\controller\school\Base;

use app\common\model\school\activity\order\SettleLog as SettleLogModel;
use app\common\model\user\withdrawal\Userwithdrawal;
use app\common\model\user\withdrawal\UserwithdrawalLog;

/**
 * 用户端：结算记录相关接口
 */
class SettleLog  extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    /**
     * 初始化操作
     * @access protected
     */
    protected function _initialize()
    {
        $this->transactionCheck();

        $this->model = new SettleLogModel;
        parent::_initialize();


        $this->setUrlLock();
    }




    /**
     * @ApiTitle( 结算记录接口（收益明细）)
     * @ApiSummary(我的结算记录接口（收益明细）)
     * @ApiMethod(GET)
     * @ApiParams(name = "keywords", type = "string",required=false,description = "搜索关键字")
     * @ApiParams(name = "page", type = "string",required=true,description = "页数")
     * @ApiParams(name = "limit", type = "string",required=true,description = "条数")
     * @ApiParams(name = "status", type = "string",required=false,description = "结算状态:1=冻结中,2=待结算,3=已结算")
     * @ApiParams(name = "activity_id", type = "int",required=false,description = "活动id")
     * @ApiReturn({
     *
     *})
     */
    public function settle_log()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $page  =  $this->request->get('page/d', 0); //页数
        $limit =  $this->request->get('limit/d', 0); //条数
        $keywords =  $this->request->get('keywords/s', ''); //搜索关键字
        $status =  $this->request->get('status/s', ''); //搜索关键字
        $server_status =  $this->request->get('server_status/s', ''); //搜索关键字
        $params = [];
//        $params["auth_status"] =  $this->request->get('auth_status/s', ''); //搜索关键字

        $activity_id =  $this->request->get('activity_id/s', ''); //搜索关键字

        $has_evaluate =  $this->request->get('has_evaluate/d', 0); //搜索关键字
//        $type =  $this->request->get('type/s', ''); //筛选学员和教练单

        try{
            //当前申请状态
            $res = $this->model::allList($user_id,$page, $limit,$keywords,$status,$activity_id,$params);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }





    /**
     * @ApiTitle( 我的提现记录)
     * @ApiSummary(我的提现记录列表接口)
     * @ApiMethod(GET)
     * @ApiParams(name = "keywords", type = "string",required=false,description = "搜索关键字")
     * @ApiParams(name = "page", type = "string",required=true,description = "页数")
     * @ApiParams(name = "limit", type = "string",required=true,description = "条数")
     * @ApiParams(name = "status", type = "string",required=false,description = "提现申请状态:1=待审核,2=审核通过,3=审核不通过")
     * @ApiParams(name = "withdrawal_status", type = "string",required=false,description = "打款状态:1=未打款,2=打款中,3=已打款")
     * @ApiReturn({
     *
     *})
     */
    public function withdrawal_log()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $page  =  $this->request->get('page/d', 0); //页数
        $limit =  $this->request->get('limit/d', 0); //条数
        $keywords =  $this->request->get('keywords/s', ''); //搜索关键字
        $status =  $this->request->get('status/s', ''); //搜索关键字
        $withdrawal_status =  $this->request->get('withdrawal_status/s', ''); //搜索关键字
        $params = [];
//        $params["auth_status"] =  $this->request->get('auth_status/s', ''); //搜索关键字

//        $activity_id =  $this->request->get('activity_id/s', ''); //搜索关键字
//
//        $has_evaluate =  $this->request->get('has_evaluate/d', 0); //搜索关键字
//        $type =  $this->request->get('type/s', ''); //筛选学员和教练单

        try{
            //当前申请状态
            $res = UserwithdrawalLog::allList($user_id,$page, $limit,$keywords,$status,$withdrawal_status,$params);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }




    /**
     * @ApiTitle( 我的提现银行卡信息（未绑数据为null）)
     * @ApiSummary(我的提现银行卡信息)
     * @ApiMethod(GET)
     * @ApiReturn({
     *
     *})
     */
    public function detail(){
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];

//        if(empty($id)){
//            $this->error(__('缺少必要参数'));
//        }

        try {
            $res =  (new Userwithdrawal)->detail($user_id);
        } catch (\Exception $e){
//            Log::log($e->getMessage());
            $this->error($e->getMessage(),['errcode'=>$e->getCode()]);
        }
        $this->success('获取成功', $res);
    }






    /**
     * @ApiTitle( 更新或保存提现银行卡)
     * @ApiSummary(更新或保存提现银行卡)
     * @ApiMethod(POST)
     * @ApiParams(name = "name", type = "string",required=true,description = "转账账户名")
     * @ApiParams(name = "bank_name", type = "string",required=true,description = "开户行名称（具体到支行）")
     * @ApiParams(name = "bank_user_name", type = "string",required=true,description = "银行账户号")
     * @ApiParams(name = "id_number", type = "string",required=true,description = "身份证号")
     * @ApiReturn({
     *
     *})
     */
    public function save()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];
        $params =[];
        $params["name"] = $this->request->post('name/s', ''); //页数
        $params["bank_name"] =  $this->request->post('bank_name/s', ''); //条数
        $params["bank_user_name"] =  $this->request->post('bank_user_name/s', ''); //搜索关键字
        $params["id_number"] =  $this->request->post('id_number/s', ''); //搜索关键字

        $params = [];
//        $params["auth_status"] =  $this->request->get('auth_status/s', ''); //搜索关键字

//        $activity_id =  $this->request->get('activity_id/s', ''); //搜索关键字
//
//        $has_evaluate =  $this->request->get('has_evaluate/d', 0); //搜索关键字
//        $type =  $this->request->get('type/s', ''); //筛选学员和教练单

        try{
            //当前申请状态
            $res = (new Userwithdrawal)->addOrupdateWithdrawal($user_id,$params,true);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){

            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }








    /**
     * @ApiTitle(申请银行卡提现)
     * @ApiSummary(申请银行卡提现)
     * @ApiMethod(POST)
     * @ApiReturn({
     *
     *})
     */
    public function withdrawal()
    {
        $user_id = 0;
        $user = $this->auth->getUser();//登录用户
        if($user)$user_id = $user['id'];

        $this->model = new UserwithdrawalLog;
        $this->setUrlLock();
        try{
            //当前申请状态
            $res = $this->model->initiateWithdrawal($user_id,"bank",true);
//            if($user_id =='670153'){
//               file_put_contents("ceshi66.txt",(new AppointmentOrder())->getLastSql());
//            }
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('查询成功', $res);
    }




}