<?php

namespace AdaPaySdk;
use AdaPay\AdaPay;
use think\facade\Db;

class SettleAccount extends AdaPay
{
    static private $instance;

    public $endpoint = "/v1/settle_accounts";
    public $cash_endpoint = "/v1/cashs";
    public $settle = NULL;

    public function __construct()
    {
        parent::__construct();
        // $this->sdk_tools = SDKTools::getInstance();
    }

    /**
     * 查询账户余额
     * @Author   Kelly
     * @DateTime 2020-10-23
     * @version  V1.1.4
     * @param    array
     * @return   array
     */
    public function balance($params=array()){
        ksort($params);
        if($params['api_key'] == 'api_live_b0a687ec-5450-4ada-a0e4-65bac0f46871'){
            $this->setRsaPrivateKey('MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBANG5b9X+SnEirAtdj1fN/wTzmA5Rm7V8QTIIxIEmADTQ+28slzcCYWpG3FMmIfjwX7Ho0mdUDvDVQ++U92PJRSaMrKYbdf6mz6DWLKveEfjkvrjQqk4l+bLu3H75nS10A8nkBSqYO0tPiHmZSkBb/cHcDar6z2rLuT8IYmZYQtGZAgMBAAECgYEAiiQ7Jhl5r0vHRzU5Vfy6HejWH5hhmAlgaDSeOvNs7104QOJ+OB25TOef+KUFe5PZdLaG4MjdjoTrWsQkr/XDNY+FjrCTPY9LUKQVztS+K6uyhOO5xELX2m6tYvvkRzomJmpxKKlJImralVsn0tgGldH7oGQbpjCa1YH2BwFPygECQQD0VTz54ABvbg6vLQ1CRMwYzInR/QhA/nvS9DLDdpZmkP04WyJ3iUOAot1IQ51NVCz55rHSDZS5nGcqoIWbm3llAkEA270jWrH+xUbQTj4hrV98QHnFWcZGe98lO1zm0bggE3EWlSGp2EmWZZedIu2xq6QvlxbscjjC1M+SDMB/Dn/OJQJAJveBJgjHiP3wcZepyavXMQ1N3aORMWxkqxwvDGVZTzpIaDiAorfNgDJ5QBEogTCvi5tzOKDZ21BKU84OAOcm0QJBANORXATq9B4FfHKZJ9vB3+REWluBqEOEKljpreOmoFuXuuKDswBoecH/JHCI41QblSaoD6g6NnyoDjrw7GSNcXECQEI3aNwxYpvcbVn8l8goyIKckfj5giXkzhGxBN9UI24wmx4CBQmdSsiShbNjVAFs1T7u4ZNEZ1TNnxxlV/+LciU=');
        }else{
            $this->setRsaPrivateKey("MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALFOkwRKx2HKMpAL7LUb755cIS7mleort++Rkl25tJMJCf9Ezeyg4xwLBN6RQsLERTAzRKc/EUtxm4/9rzDzvNZ4BegEI8mXWBUrgpn7e1ar8Ek8IRPdXLrozuld9cv56R3yB/Yta0R+kmgCfn2C5V+xtM6njimkOStvdK5cNwqFAgMBAAECgYA5pn+87IeJr1Hjbk0n0g6GYhRUCJuECXUEC0QY/3HMQSsj+f4uy10s2BRcPcUESQbqFjFjY+k+q5DWL7kP5KBoXGnFyflAyRhWGTkSC3VhGixh6ro3EcUJSrFDjGaThGaPrc8Zfcp5JhL5Thi0GaBXSnRH1Cmn4h8gkAEJxIM7PQJBAOq2+oVPtqkj99YRs0eewkZAM7bKRzSwI7OFU6YWsCYVkqNE7Lr3KKGekehuWwkOvrCBR+NEGQfAb557g2CHmzMCQQDBYtgQ5wvl9fD8BWAVKuPoxoleH+9wUQ65Rnt/Ebkx+g8kP37JRun81duLDULTKr/3ak42goE00TfnfeXl0gNnAkA0wuUVypF4/AjeKiRu2RU6zXvSfy36fg3IcRQYpcj7BqZr5TaB4MVVb1vFL+lu482hx63tPIWT5IIy0fj6XgFZAkBinlKLRBgIPISZMPCPuuCg22ZADs8fGucEStI9ub8Uom3IUW0JndVDb5fW8HtMjrd4p8ic+a6ETdFRhx2PNqsvAkAzDvlve56sZnLYx9t/MPkJTK3ZhgVETS0H2FUyJGVOSfjv68bNsUshyPfzJrdbCeLzjMLq8TpyOmiIUAeQ5YCX");
        }
        $this->__init_params();
        $this->setApiKey($params['api_key']);
        unset($params['api_key']);

        $request_params = $this->do_empty_data($params);
        $req_url =  self::$gateWayUrl . $this->endpoint . "/balance";
        $header = $this->get_request_header($req_url, http_build_query($request_params), self::$headerText);
        $this->result =  $this->ada_request->curl_request($req_url . "?" . http_build_query($request_params), "", $header, false);
        // $this->result = $this->sdk_tools->get($params, $this->endpoint."/balance");
    }

    public function create($params,$key){
        $request_params = $params;
        $request_params = $this->do_empty_data($request_params);
        $this->setApiKey($key);
        $req_url = self::$gateWayUrl.$this->endpoint;
        $header =  $this->get_request_header($req_url, $request_params, self::$header);

        Db::name('log')->insert(['data'=>json_encode($header)]);
        $this->result = $this->ada_request->curl_request($req_url, $request_params, $header, $is_json=true);
        // $this->result = $this->sdk_tools->post($params, $this->endpoint);
    }

    public function query($params=array()){
        $request_params = $params;
        $this->setRsaPrivateKey('MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALFOkwRKx2HKMpAL7LUb755cIS7mleort++Rkl25tJMJCf9Ezeyg4xwLBN6RQsLERTAzRKc/EUtxm4/9rzDzvNZ4BegEI8mXWBUrgpn7e1ar8Ek8IRPdXLrozuld9cv56R3yB/Yta0R+kmgCfn2C5V+xtM6njimkOStvdK5cNwqFAgMBAAECgYA5pn+87IeJr1Hjbk0n0g6GYhRUCJuECXUEC0QY/3HMQSsj+f4uy10s2BRcPcUESQbqFjFjY+k+q5DWL7kP5KBoXGnFyflAyRhWGTkSC3VhGixh6ro3EcUJSrFDjGaThGaPrc8Zfcp5JhL5Thi0GaBXSnRH1Cmn4h8gkAEJxIM7PQJBAOq2+oVPtqkj99YRs0eewkZAM7bKRzSwI7OFU6YWsCYVkqNE7Lr3KKGekehuWwkOvrCBR+NEGQfAb557g2CHmzMCQQDBYtgQ5wvl9fD8BWAVKuPoxoleH+9wUQ65Rnt/Ebkx+g8kP37JRun81duLDULTKr/3ak42goE00TfnfeXl0gNnAkA0wuUVypF4/AjeKiRu2RU6zXvSfy36fg3IcRQYpcj7BqZr5TaB4MVVb1vFL+lu482hx63tPIWT5IIy0fj6XgFZAkBinlKLRBgIPISZMPCPuuCg22ZADs8fGucEStI9ub8Uom3IUW0JndVDb5fW8HtMjrd4p8ic+a6ETdFRhx2PNqsvAkAzDvlve56sZnLYx9t/MPkJTK3ZhgVETS0H2FUyJGVOSfjv68bNsUshyPfzJrdbCeLzjMLq8TpyOmiIUAeQ5YCX');
        $this->__init_params();
        $this->setApiKey($params['api_key']);
        unset($request_params['api_key']);
        $settle_account_id = isset($params['settle_account_id']) ? $params['settle_account_id']: '';
        ksort($request_params);
        $request_params = $this->do_empty_data($request_params);
        $req_url = self::$gateWayUrl.$this->endpoint."/" . $settle_account_id;
        $header = $this->get_request_header($req_url, http_build_query($request_params), self::$headerText);
        $this->result = $this->ada_request->curl_request($req_url."?".http_build_query($request_params), "", $header, false);
        // $this->result = $this->sdk_tools->get($params, $this->endpoint."/" . $settle_account_id);
    }

    public function delete($params=array()){
        $request_params = $params;
        $this->setRsaPrivateKey('MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALFOkwRKx2HKMpAL7LUb755cIS7mleort++Rkl25tJMJCf9Ezeyg4xwLBN6RQsLERTAzRKc/EUtxm4/9rzDzvNZ4BegEI8mXWBUrgpn7e1ar8Ek8IRPdXLrozuld9cv56R3yB/Yta0R+kmgCfn2C5V+xtM6njimkOStvdK5cNwqFAgMBAAECgYA5pn+87IeJr1Hjbk0n0g6GYhRUCJuECXUEC0QY/3HMQSsj+f4uy10s2BRcPcUESQbqFjFjY+k+q5DWL7kP5KBoXGnFyflAyRhWGTkSC3VhGixh6ro3EcUJSrFDjGaThGaPrc8Zfcp5JhL5Thi0GaBXSnRH1Cmn4h8gkAEJxIM7PQJBAOq2+oVPtqkj99YRs0eewkZAM7bKRzSwI7OFU6YWsCYVkqNE7Lr3KKGekehuWwkOvrCBR+NEGQfAb557g2CHmzMCQQDBYtgQ5wvl9fD8BWAVKuPoxoleH+9wUQ65Rnt/Ebkx+g8kP37JRun81duLDULTKr/3ak42goE00TfnfeXl0gNnAkA0wuUVypF4/AjeKiRu2RU6zXvSfy36fg3IcRQYpcj7BqZr5TaB4MVVb1vFL+lu482hx63tPIWT5IIy0fj6XgFZAkBinlKLRBgIPISZMPCPuuCg22ZADs8fGucEStI9ub8Uom3IUW0JndVDb5fW8HtMjrd4p8ic+a6ETdFRhx2PNqsvAkAzDvlve56sZnLYx9t/MPkJTK3ZhgVETS0H2FUyJGVOSfjv68bNsUshyPfzJrdbCeLzjMLq8TpyOmiIUAeQ5YCX');
        $this->__init_params();
        $this->setApiKey($params['api_key']);
        unset($request_params['api_key']);
        $request_params = $this->do_empty_data($request_params);
        $req_url = self::$gateWayUrl.$this->endpoint."/delete";
        $header = $this->get_request_header($req_url, $request_params, self::$header);
        $this->result = $this->ada_request->curl_request($req_url, $request_params, $header, $is_json=true);
        // $this->result = $this->sdk_tools->post($params, $this->endpoint."/delete");
    }

    public function detail($params=array()){
        $request_params = $params;
        ksort($request_params);
        $request_params = $this->do_empty_data($request_params);
        $req_url = self::$gateWayUrl.$this->endpoint."/settle_details";
        $header = $this->get_request_header($req_url, http_build_query($request_params), self::$headerText);
        $this->result = $this->ada_request->curl_request($req_url."?".http_build_query($request_params), "", $header, false);
        // $this->result = $this->sdk_tools->get($params, $this->endpoint."/settle_details");
    }

    public function update($params=array()){
        $request_params = $params;
        $request_params = $this->do_empty_data($request_params);
        $this->setRsaPrivateKey('MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBALFOkwRKx2HKMpAL7LUb755cIS7mleort++Rkl25tJMJCf9Ezeyg4xwLBN6RQsLERTAzRKc/EUtxm4/9rzDzvNZ4BegEI8mXWBUrgpn7e1ar8Ek8IRPdXLrozuld9cv56R3yB/Yta0R+kmgCfn2C5V+xtM6njimkOStvdK5cNwqFAgMBAAECgYA5pn+87IeJr1Hjbk0n0g6GYhRUCJuECXUEC0QY/3HMQSsj+f4uy10s2BRcPcUESQbqFjFjY+k+q5DWL7kP5KBoXGnFyflAyRhWGTkSC3VhGixh6ro3EcUJSrFDjGaThGaPrc8Zfcp5JhL5Thi0GaBXSnRH1Cmn4h8gkAEJxIM7PQJBAOq2+oVPtqkj99YRs0eewkZAM7bKRzSwI7OFU6YWsCYVkqNE7Lr3KKGekehuWwkOvrCBR+NEGQfAb557g2CHmzMCQQDBYtgQ5wvl9fD8BWAVKuPoxoleH+9wUQ65Rnt/Ebkx+g8kP37JRun81duLDULTKr/3ak42goE00TfnfeXl0gNnAkA0wuUVypF4/AjeKiRu2RU6zXvSfy36fg3IcRQYpcj7BqZr5TaB4MVVb1vFL+lu482hx63tPIWT5IIy0fj6XgFZAkBinlKLRBgIPISZMPCPuuCg22ZADs8fGucEStI9ub8Uom3IUW0JndVDb5fW8HtMjrd4p8ic+a6ETdFRhx2PNqsvAkAzDvlve56sZnLYx9t/MPkJTK3ZhgVETS0H2FUyJGVOSfjv68bNsUshyPfzJrdbCeLzjMLq8TpyOmiIUAeQ5YCX');
        $this->__init_params();
        $this->setApiKey($params['api_key']);
        unset($request_params['api_key']);
        $request_params = $this->do_empty_data($request_params);
        $req_url = self::$gateWayUrl.$this->endpoint."/modify";
        $header =  $this->get_request_header($req_url, $request_params, self::$header);
        $this->result = $this->ada_request->curl_request($req_url, $request_params, $header, $is_json=true);
        // $this->result = $this->sdk_tools->post($params, $this->endpoint."/modify");
    }

    
}