<?php
namespace app\products\controller;

use app\products\validate\Car as CarValidate;
use app\web\controller\BaseController;
use think\Log;
use think\Request;
use util\MCurl;
header("Content-type: text/html; charset=utf-8");
/**
 * 功能描述  华安车险
 * Class Car
 * @package app\products\controller
 * Date: 2020/8/3
 * Time: 17:00
 * @author nyq
 */
class Car extends BaseController
{
    //请求地址
    //private $api_url = "https://api.sosyun.com//serviceapi/car/";
    //private $api_url = "http://demo8.xiaochakeji.com/serviceapi/car/";
    private $api_url = "www.aisigit.com/products/car/";
    //回调查询接口
    private $api_back_url = "http://demo8.xiaochakeji.com/notify/car/";
    //private $api_back_url = "https://api.sosyun.com//notify/car/";

    /**
     * 函数功能描述   获取订单信息
     * Date: 2020/8/07
     * Time: 10:00
     * @author nyq
     */
    public function carOrder(Request $request)
    {
        $data = $request->param();
        $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
        $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
        $offset=($page-1)*$rows;
        $data['id']=2;
        $data['offset']=$offset;
        $data['rows']=$rows;

        $res = MCurl::instance($this->api_url."carOrder")->post($data);
        $json=json_decode($res);

        $result["total"] =count($json);
        $result['rows']=$json;
        echo json_encode($result);
        //return $json;
    }
    /**
     * 函数功能描述   获取订单详情
     * Date: 2020/8/07
     * Time: 10:00
     * @author nyq
     */
    public function carOrderDetail(Request $request)
    {
        $data = $request->param();
        $id['id']=$data['id'];
        $res = MCurl::instance($this->api_url."carOrderDetail")->post($id);
        $res = json_decode($res, true);

        $use_res = MCurl::instance($this->api_url."selMember")->post($id);
        $use_res = json_decode($use_res, true);

        $status_arr = ['转人工核保','','','打回修改','拒保','','','待支付','支付成功','支付失败'];
        foreach($res as  $k => $v){
            if($v['riskCode']=='0302'){
                $res[$k]['type']='商业险';
            }elseif($v['riskCode']=='0301'){
                $res[$k]['type']='交强险';
            }
            $res[$k]['appName']=$use_res['appName'];
            $res[$k]['insuredName']=$use_res['insuredName'];
            $res[$k]['status_arr'] = $status_arr[$v['status']];
            $res[$k]['operDate'] =date("Y-m-d H:i:s",$v['operDate']);
        }
        return $res;
    }
    /**
     * 函数功能描述    搜索
     * Date: 2020/8/07
     * Time: 10:00
     * @author nyq
     */
    public function select(Request $request)
    {
        $data = $request->param();

        $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
        $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
        $offset=($page-1)*$rows;
        $data['id']=2;
        $data['offset']=$offset;
        $data['rows']=$rows;

        $res = MCurl::instance($this->api_url."carOrder")->post($data);
        $json=json_decode($res);

        $result["total"] =count($json);
        $result['rows']=$json;
        echo json_encode($result);
    }

    /**
     * 函数功能描述    支付申请
     * Date: 2020/8/11
     * Time: 10:00
     * @author nyq
     */
    public function pay(Request $request)
    {
        $data = $request->param();
            //查询订单信息及金额
        $order_res = MCurl::instance($this->api_url."pay")->post($data);
        $use_res = MCurl::instance($this->api_url."selMember")->post($data);
        $order_json=json_decode($order_res,true);
        $use_json=json_decode($use_res,true);

        $json['jqAppNo']=$order_json[0]['appNo'];
        $json['jqAmount']=$order_json[0]['premium']+$order_json[0]['currentTax'];
        $json['jqAppName']=$use_json['appName'];
        $json['syAppNo']=$order_json[1]['appNo'];
        $json['syAmount']=$order_json[1]['premium'];
        $json['syAppName']=$use_json['appName'];
        $arr['data']=json_encode($json);
        $res = MCurl::instance($this->api_url."launchPayment")->post($arr);

        $res=json_decode($res, true);

        return $res;

    }


    /**
     * 函数功能描述  获取车型
     * Date: 2020/8/03
     * Time: 17:10
     * @author nyq
     */
    public function carmodel(Request $request)
    {
        $data = $request->param();
        $validtaor = new CarValidate();
        $res = $validtaor->scene("carmodel")->check($data);
        if ($res === false) {
            return ['errcode' => 100, 'errmsg' => $validtaor->getError()];
        }
        if (in_array("undefined", $data)) {
            return ["errcode" => 100, "errmsg" => "提交数据存在问题"];
        }

        $res = MCurl::instance($this->api_url."carmodel")->post($data);
        $res = json_decode($res, true);

        return $res;
    }


    /**
     * 函数功能描述 车险保费计算
     * Date: 2020/8/4
     * Time: 09:12
     * @author nyq
     */
    public function premium(Request $request)
    {

        $json = $request->param();   // 报错 Array to string conversion
        $data = $request->param('data');

        //数据特殊符号转义
        $data = htmlspecialchars_decode($data);
        $data = json_decode($data, true);
        //处理校验数据
        $arr=$this->premiumArray($data);
        $validtaor = new CarValidate();
        $res = $validtaor->scene("premium")->check($arr);
        if ($res === false) {
            return ['errcode' => 100, 'errmsg' => $validtaor->getError()];
        }
        if (in_array("undefined", $arr)) {
            return ["errcode" => 100, "errmsg" => "提交数据存在问题"];
        }

        $res = MCurl::instance($this->api_url."premium")->post($json);
        $res = json_decode($res, true);

        return $res;
    }
    /**
     * 函数功能描述 车险提交核保
     * Date: 2020/8/03
     * Time: 15:10
     * @author nyq
     */

    public function underwrite()
    {
        $json = $this->request->post();
        $base = $this->request->post('data');

        $base = htmlspecialchars_decode($base);
        $base = json_decode($base, true);

        //进行数据校验
        $validtaor = new CarValidate();
        $res = $validtaor->scene("underwrite")->check($base);
        if ($res === false) {
            return ['errcode' => 100, 'errmsg' => $validtaor->getError()];
        }

        $res = MCurl::instance($this->api_url."underwrite")->post($json);
        $res=json_decode($res, true);
        return $res;
    }


    /**
     * 函数功能描述 支付/核保回调接口
     * Date: 2020/8/4
     * Time: 17:00
     * @author nyq
     */
    public function get_status()
    {
        $base = $this->request->post();
        $res = MCurl::instance($this->api_back_url."get_status")->post($base);
        $res=json_decode($res, true);
        return $res;
    }

    /**
     * 函数功能描述 支付申请接口
     * Date: 2020/8/4
     * Time: 16:30
     * @author nyq
     */
    public function payment()
    {
        $json = $this->request->post();
        $base = $this->request->post('data');
        $base = htmlspecialchars_decode($base);
        $base=json_decode($base, true);

        //进行数据校验
        $validator = new CarValidate();
        $res = $validator->scene("pay")->check($base);
        if ($res === false) {
            return ['errcode' => 100, 'errmsg' => $validator->getError()];
        }

        $res = MCurl::instance($this->api_url."payment")->post($json);
        $res=json_decode($res, true);
        return $res;
    }


    /**
     * 函数功能描述 整理车险检验信息
     * Date: 2020/7/21
     * Time: 11:12
     * @author nyq
     */
    public  function  premiumArray($arr){

        //车主信息
        $owner=$arr['owner'];
        //投保人
        $applacation=$arr['applacation'];
        //被保人
        $insured=$arr['insured'];
        //车辆信息
        $vhl=$arr['vhl'];
        //base
        //$base=$arr['base'];
        $data=array_merge($owner,$applacation,$insured,$vhl);
        //险种信息
        if(count($arr['riskList'])>1){
            if($arr['riskList'][0]['riskCode'] != '' && $arr['riskList'][1]['riskCode'] != ''){
                $data['xz']='true';
            }else{
                $data['xz']='';
            }
        }else{
            $data['xz']='';
        }
        $data['cityCode']=$arr['cityCode'];
        $data['modelName']=$arr['modelName'];
        $data['email']=$arr['email'];
        return  $data;
    }





}