<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
/**
 * 汽车
 *
 * @icon fa fa-car
 */
class Car extends Backend
{

    /**
     * Car模型对象
     * @var \app\admin\model\Car
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Car;
        $this->view->assign("chepaicolorList", $this->model->getChepaicolorList());
        $this->view->assign("isEtcList", $this->model->getIsEtcList());
        $this->view->assign("powerList", $this->model->getPowerList());
        $this->view->assign("leasetypeList", $this->model->getLeasetypeList());
    }



    /**
     * 即时信息
     */
    public function gpsinfo(){
        $id = input('id',0);
        $row = Db::name('car')->where('id',$id)->find();

        $data = $this->getgpsinfo($row['chepaihao'],$row['chepaicolor'],$this->model->getChepaicolorList());

        $this->view->assign("gpsinfo", $data);

        return $this->view->fetch();
    }

    private function getgpsinfo($chepaihao = '',$chepaicolor = '',$color_code)
    {
        $url = 'http://140.210.193.143:8088/topgps/services/TopDataService.ashx';
        $url = 'http://ads.cetgps.com/topgps/services/TopDataService.ashx';

        $request = [
            'Action' => 'GetVehicleGpsInfo',
            'UserId' => 'dxqc',
            'Pwd' => strtoupper(md5('134679aA#')),
            'Vehicles' => [
                [
                    'PlateNum' => $chepaihao,
                    'ColorCode' => $chepaicolor,
                ],
            ],
            'NeedAddress' => '1',
        ];

        $curl_data = ['request'=>json_encode($request)];
        $result = curl_post($url,$curl_data);

        //$result = '{"Ret":0,"Msg":"","Data":[{"PlateNum":"川AA80498","ColorCode":"5","GpsTime":"2024/1/17 15:04:13","Mileage":"50805.90","Speed":"5.5","Longitude":"104.045393","Latitude":"30.646638","Address":"四川省成都市武侯区武侯祠大街231-2号 成都武侯祠博物馆南272米","Direction":"94","Altitude":"463","IsGpsValid":"1","AlarmInfo":"","StateInfo":"ACC开;定位;运营;","DriverLicense":"","DriverName":"","OrgName":"成都登欣汽车服务有限公司"}],"Datal":[]}';

        $data = [];

        //字段
        $field_arr = [
            'PlateNum' => '车牌号码',
            'ColorCode' => '车牌颜色编码',
            'Mileage' => '里程',
            'Speed' => '速度',
            'GpsTime' => '时间',
            'Longitude' => '经度',
            'Latitude' => '纬度',
            'Address' => '地理位置',
            'Direction' => '海拔',
            'Altitude' => '方向',
            'IsGpsValid' => '是否定位',
            'AlarmInfo' => '报警信息',
            'StateInfo' => '状态信息',
//            'DriverLicense' => '司机',
//            'DriverName' => '司机',
//            'OrgName' => '公司名',
        ];
        /*//颜色码
        $color_code = [
            5=>'绿色',
            1=>'蓝色',
            2=>'黄色',
            3=>'黑色',
            4=>'白色',
            9=>'其他',
            91=>'农黄色',
            92=>'农绿色',
            94=>'渐变绿',
            224=>'黄绿',
            225=>'白绿'
        ];*/
        $result = json_decode($result,true);
        if(is_array($result) && isset($result['Ret']) && $result['Ret'] == 0){

            if(isset($result['Data'][0])){
                $data0 = $result['Data'][0];
                unset($data0['DriverLicense']);
                unset($data0['DriverName']);
                unset($data0['OrgName']);

                foreach($data0 as $key => $val){
                    if($key == 'IsGpsValid'){
                        $val = ($val == 1) ? '定位' : '未定位';
                    }
                    if($key == 'ColorCode'){
                        $val = isset($color_code[$val]) ? $color_code[$val] : '未知';
                    }

                    //字段转换中文
                    $newkey = isset($field_arr[$key]) ? $field_arr[$key] : $key;
                    $data[$newkey] = $val;
                }
            }
        }

        //dump($data);
        return $data;
    }


}
