<?php
api::install(function($dd){
	$x=db::val("SELECT count(*) as x FROM information_schema.columns where TABLE_SCHEMA<>'information_schema' and TABLE_NAME like '¤STATISTICSREQ'",'x');
    if($x!=5){
		db::query("DROP TABLE ¤STATISTICSREQ;");
        db::query("CREATE TABLE ¤STATISTICSREQ ( `date` date NOT NULL, req_vin int(10) NOT NULL DEFAULT 0, req_tec int(10) NOT NULL DEFAULT 0, req_imm int(10) NOT NULL DEFAULT 0, id_user int(10) NOT NULL DEFAULT 0 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;");
        db::query("ALTER TABLE ¤STATISTICSREQ ADD PRIMARY KEY(`date`, `id_user`);");
    }
});
api::xgetdetailforcheck(function($data){
    return db::row("SELECT sum(req_tec) as nbr_req_tecdoc,sum(req_vin) as nbr_vin_search,sum(req_imm) as nbr_imm_search FROM ¤STATISTICSREQ where id_user='".$data['id']."' and id_user!=2");
});
api::xgetdetailforcheckhisto(function($data){
    $tab = db::table("SELECT * FROM ¤STATISTICSREQ WHERE id_user='".$data['id']."' and id_user!=2 and YEAR(date)=YEAR(CURDATE()) and MONTH(date)=MONTH(CURDATE()) order by date ASC");
    if($tab){
        $labels=array_map(function($item){ return $item["date"]; }, $tab); 
        $data_req_tec=array_map(function($item){ return $item["req_tec"]; }, $tab); 
        $data_req_vin=array_map(function($item){ return $item["req_vin"]; }, $tab); 
        $data_req_imm=array_map(function($item){ return $item["req_imm"]; }, $tab); 
        $rep = ["series" => ["Nbr Req VIN","Nbr Req TecDoc","Nbr Req IMM"],"data" => [$data_req_vin,$data_req_tec,$data_req_imm],"labels"=>$labels,"colors"=>['#45b7cd', '#ff6384', '#2ECC71'],"options"=>["legend"=>["display"=>true]]];
    }else{
        $rep = ["series" => ["Nbr Req VIN","Nbr Req TecDoc","Nbr Req IMM"],"data" => [[],[],[]],"labels"=>[],"colors"=>['#45b7cd', '#ff6384', '#2ECC71'],"options"=>["legend"=>["display"=>true]]];
    }
    return $rep;
});


class db_client extends db_table
{
    static $_config = [
        "titre"     => "Clients",
        "titre_page" => "Gestion des Clients",
        "col_titre" => "titre",
        "view"      => "Backoffice",
        "dialog_width" => "fit-content"
    ];
    var $col_int_id;
}
db_client::col_string("titre")
    ->titre("Nom")
    ->col(6)
    ->isTitre(true)
    ->inFilter(true)
    ->inTable(true);
db_client::col_string("tel")
    ->titre("Téléphone")
    ->col(3)
    ->inFilter(true)
    ->inTable(true); 
db_client::col_select("type")
    ->titre("Type")
    ->col(3)
    ->options(["Basic","Pro","Expert"])
    ->inFilter(true)
    ->inTable(true);    
db_client::col_string("login")
    ->titre("Login")
    ->col(4);    
db_client::col_string("password")
    ->titre("Mot de passe")
    ->col(4);
db_client::col_bool("etat")
    ->titre("Active")
    ->inTable(true)
    ->col(2)
    ->inFilter(true);
Route::isJs(function(){
        ob_start();
        ?>	
            <div class="col-md-12" ng-show="item||loading">
                <div ng-show="loading" style="position: absolute;top: 0;right: 0;bottom: 0;left: 0;display: flex;align-items: center;justify-content: center;background: rgba(255,255,255,0.5);z-index: 100">
                    <md-progress-circular md-mode="indeterminate" value="" ng-value="" md-diameter="48"></md-progress-circular>
                </div>
                <div>
                    <h3>Nombre de Req par jour ce mois :</h3>
                    <canvas 
                        id="line" 
                        class="chart chart-line" 
                        chart-data="item.data"
                        chart-labels="item.labels" 
                        chart-series="item.series" 
                        chart-options="item.options"
                        chart-colors="item.colors" 
                    ></canvas>
                </div>
            </div>
        <?php
        $code=ob_get_clean();
        ?>
        <script>
            app.directive('detailclientforcheckhisto', [function () {
                return {
                    restrict: 'EA',
                    transclude : false,
                    replace : false,
                    template : <?= json_encode(preg_replace("/\t|\n|\r/i", "", $code)) ?>,
                    scope : {
                        xid: "@",
                    },
                    controller : function($scope){
                        $scope.item = {};
                        $scope.loading = false;

                        $scope.loadData = function(){
                            $scope.item = {};
                            $scope.loading = true;
                            api("xgetdetailforcheckhisto",{id : $scope.xid},function(res){
                                $scope.item = res;
                                $scope.loading = false;
                                $scope.$apply();
                            });
                        };
                        $scope.$watch("xid",function(){
                            if($scope.xid){
                                $scope.loadData();	
                            }
                        });	
                    },
                    link: function ($scope, iElement, iAttrs) {
    
                    }
                };
            }]);
        </script>
        <?php
    });
   
Route::isJs(function(){
        ob_start();
        ?>	
            <style>
                .modal-footer {
                    background: #ffffff !important;
                }
                .modal-body {
                    padding: 0px;
                }
                .modal-title {
                    line-height: 1 !important;
                }
            </style>
            <div class="col-md-4">
                <div ng-show="loading" style="position: absolute;top: 0;right: 0;bottom: 0;left: 0;display: flex;align-items: center;justify-content: center;background: rgba(255,255,255,0.5);z-index: 100">
                    <md-progress-circular md-mode="indeterminate" value="" ng-value="" md-diameter="48"></md-progress-circular>
                </div>
                <div ng-hide="loading" class="box box-widget widget-user">
                    <div class="widget-user-header bg-aqua">
                        <div class="row">
                            <div class="col-sm-8">
                                <h3 class="widget-user-username">{{xname}}</h3>
                            </div>
                            <div class="col-sm-4" style=" padding-top: 10px; ">
                                <button ng-click="lsthistoDialog.xname = xname;lsthistoDialog.xid = xid;lsthistoDialog.show()" class="btn btn-primary" type="button" style="color: #337ab7;background: white;border-radius: 15px !important;font-weight: bold;float: right;text-align: center;">Voir Historique &nbsp; <span class="glyphicon glyphicon-list" style="color: #337ab7 !important;"></span></button>
                                <dialog ng-model="lsthistoDialog" titre="Historique User : {{lsthistoDialog.xname}} " btn-fermer="true" width="60%">
                                    <detailclientforcheckhisto xid='{{lsthistoDialog.xid}}'></detailclientforcheckhisto>
                                </dialog>
                            </div> 
                        </div>
                    </div>
                    <div class="widget-user-image">
                    <img class="img-circle" src="/public/images/apiclient.png">
                    </div>
                    <div class="box-footer">
                        <div class="row">
                            <div class="col-sm-4">
                                <div class="description-block">
                                    <h5 class="description-header">{{(item['nbr_vin_search']?item['nbr_vin_search']:"0")}}</h5>
                                    <span >Nbr Req VIN</span>
                                </div>
                            </div>
                            <div class="col-sm-4">
                                <div class="description-block">
                                    <h5 class="description-header">{{(item['nbr_req_tecdoc']?item['nbr_req_tecdoc']:"0")}}</h5>
                                    <span >Nbr Req TecDoc</span>
                                </div>
                            </div>
                            <div class="col-sm-4">
                                <div class="description-block">
                                    <h5 class="description-header">{{(item['nbr_imm_search']?item['nbr_imm_search']:"0")}}</h5>
                                    <span >Nbr Req IMM</span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        <?php
        $code=ob_get_clean();
        ?>
        <script>
            app.directive('detailclientforcheck', [function () {
                return {
                    restrict: 'EA',
                    transclude : false,
                    replace : false,
                    template : <?= json_encode(preg_replace("/\t|\n|\r/i", "", $code)) ?>,
                    scope : {
                        xname: "@",
                        xid: "@",
                    },
                    controller : function($scope){
                        $scope.item = {};
                        $scope.loading = false;
                        $scope.loadData = function(){
                            $scope.item = {};
                            $scope.loading = true;
                            api("xgetdetailforcheck",{id : $scope.xid},function(res){
                                $scope.item = res;
                                $scope.loading = false;   
                                $scope.$apply();
                            });
                        };
                        $scope.loadData();
                    },
                    link: function ($scope, iElement, iAttrs) {
    
                    }
                };
            }]);
        </script>
        <?php
    });