<?php

class vindv
{
    static function query($vin){
        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => "https://vindv.com/dataCore/lvv.php?vin=$vin",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 20,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
        CURLOPT_HTTPHEADER => array(
            'Referer: https://vindecodervehicle.com/'
        ),
        ));
        $response = curl_exec($curl);
        curl_close($curl);
		return json_decode($response);
    }
}

api::getvindv(function($data){
    return ["res" => ["vindv"=>vindv::query($data['vin'])]];
});

db_table::createView("vindv", "VinDV");
Route::isJs(function () {
    ob_start();
?>
    <div style="background: rgba(255,255,255,0.7);flex:1;display: flex;">
        <div style="padding: 15px;width: 220px;display: flex;align-items: center;justify-content: flex-start;background: #fff;flex-direction: column;">
            <div ng-cloak>
                <w-form api="getvindv" on-reset="" on-success="" on-end="" ng-model="ctrl.form" on-init="ctrl.form.data.vin='WDD2130421A012615'">
                    <div class="row" style="width: 100%;margin: 0;padding: 0;">
                        <div class="col-xs-12" style="padding: 0;">
                            <div class="form-group avs">
                                <label style="display:block" class="control-label">VIN</label>
                                <input type="text" class="form-control avs" pattern="[0-9A-Za-z]{17}" required name="vin" ng-model="ctrl.form.data.vin" placeholder="VIN">
                            </div>
                        </div>
                        <div class="col-xs-12" style="padding: 0;">
                            <button class="btn btn-primary" type="submit">Valider</button>
                        </div>
                    </div>
                </w-form>
                <label ng-if="ctrl.form.res.imma" style="display:block;margin-top:10px" class="control-label">Immatricule :<h4><a style="cursor: pointer" ng-click="ijk(ctrl.form.res.imma)">{{ctrl.form.res.imma}}</a></h4></label>
                <label ng-if="ctrl.form.res.dmc" style="display:block;" class="control-label">DMC :<h4>{{ctrl.form.res.dmc}}</h4></label>
            </div>
        </div>
        <div style="padding: 15px;flex:1;display: flex;">
            <div ng-cloak style="display: flex;width: 100%;align-items: flex-start;flex:1;justify-content: flex-start;flex-direction: column;">
                <div ng-show="form.loading" style="display: flex;align-items: center;justify-content: center;flex:1;width: 100%;">
                    <b>Recherche en cours, veuillez patienter ...</b>
                </div>
                <div ng-hide="form.loading" style="width: 100%;">
                    <md-tabs md-dynamic-height md-border-bottom>
                        <md-tab label="V7">
                        <table class="table table-striped table-bordered">
                            <tr ng-repeat="(key, value) in ctrl.form.res.vindv"><th style="width:1px">{{key}}</th><td ng-if="key=='carId'"><a style="cursor: pointer" ng-click="detailDialog.item.carName = ctrl.form.res.vindv.vehicleModelSeriesName;detailDialog.item.carId=value;detailDialog.show();">{{value}}</a></td><td ng-if="key!='carId'">{{value}}</td></tr>
                        </table>
                        </md-tab>
                    </md-tabs>
                </div>
            </div>
        </div>
        <dialog ng-model="detailDialog" titre="Détail : {{detailDialog.item.carName}} " btn-fermer="true" width="fit-content">
            <listcar car-id="{{detailDialog.item.carId}}"></listcar>
        </dialog>
        <dialog ng-model="tabDialog" titre="Véhicule : {{tabDialog.item.fullName}} " btn-fermer="true" width="fit-content">
            <table class="table table-striped table-bordered" style="background: #fff;">
                <tr ng-repeat="(key, value) in tabDialog.item"><th style="width:1px">{{key}}</th><td ng-if="key=='id'"><a style="cursor: pointer" ng-click="detailDialog.item.carName = tabDialog.item.fullName;detailDialog.item.carId=value;detailDialog.show();">{{value}}</a></td><td ng-if="key!='id'">{{value}}</td></tr>
            </table>
        </dialog>
        <dialog ng-model="immDialog" titre="Immatricule : {{immDialog.id}} " btn-fermer="true" width="fit-content">
            <immlist piece-id="{{immDialog.id}}"></immlist>
        </dialog>
    </div>
    <?php
    $code = ob_get_clean();
    ?>
    <script>
        app.directive('vindv', [function() {
            return {
                restrict: 'EA',
                transclude: false,
                replace: false,
                template: <?= json_encode(preg_replace("/\t|\n|\r/i", "", $code)) ?>,
                scope: {},
                controller: function($scope,$sce) {
                    $scope.form = {};
                    $scope.ijk = (i) => {
                        var ll=document.getElementsByTagName('immlist').length;
                        if(ll>0){
                            $scope.immDialog.id = i;
                            $scope.immDialog.show();
                            angular.element(document.getElementsByTagName('immlist')[ll-1]).scope().$$childTail.wwc(i);
                        }
                    };
                },
                link: function(scope, iElement, iAttrs) {}
            };
        }]);
    </script>
<?php
});