<?php
namespace Modules\Facture\Controller;

use Skeleton\Controller\Controller;
use Modules\Fournisseur\Repository\Indicateur;
use Helpers\Highcharts\Highcharts;
use Modules\Facture\Repository\IndicateurFacture;

class IndicateurFactureController extends Controller
{

    
    public function indexAction() : string
    {
        $post = $this->request->getParsedBody();
        
        
        $trad = $this->getTranslationService();

        $indicateur = new Indicateur($this->pdo);
        $periodes = $indicateur->periodedispo();
        $indicateurFacture = new IndicateurFacture($this->pdo);
        
        $origineFacture = [
            ['libelle' => 'test1', 'value' => '50'],
            ['libelle' => 'test2', 'value' => '25'],
            ['libelle' => 'test3', 'value' => '12.5'],
            ['libelle' => 'test4', 'value' => '12.5'],
        ];
        
        $typeFacture = $delaiPaiement = $rapprochement = [
            ['libelle' => 'test1', 'value' => '75'],
            ['libelle' => 'test2', 'value' => '15'],
            ['libelle' => 'test3', 'value' => '10'],
        ];

        /* DONUT 1 origine des factures */
        $array1['value'] = $origineFacture;
        $array1['detailDonut'] = ['nom' => $trad->t('Origine'),'innerSize' => '160', 'tooltip' => ' %'];
        $array1['titre'] = $trad->t('Origine des factures');
        $array1['subTitle'] = ['titre' => $origineFacture[0]['value'].'%'];
        $array1['legend'] = true;
        $array1['type'] = 'pie';
        
        $highcharts1 = Highcharts::generate($array1, $this->pdo, $trad);
        
        /* DONUT 2 type de factures */
        $array2['value'] = $typeFacture;
        $array2['detailDonut'] = ['nom' => $trad->t('Type'),'innerSize' => '160', 'tooltip' => ' %'];
        $array2['titre'] = $trad->t('Type de factures');
        $array2['subTitle'] = ['titre' => $typeFacture[0]['value'].'%'];
        $array2['legend'] = true;
        $array2['type'] = 'pie';
        
        $highcharts2 = Highcharts::generate($array2, $this->pdo, $trad);
        
        /* DONUT 3 Rapprochement */
        $array3['value'] = $rapprochement;
        $array3['detailDonut'] = ['nom' => $trad->t('Rapprochement'),'innerSize' => '160', 'tooltip' => ' %'];
        $array3['titre'] = $trad->t('Rapprochement');
        $array3['subTitle'] = ['titre' => $rapprochement[0]['value'].'%'];
        $array3['legend'] = true;
        $array3['type'] = 'pie';
        
        $highcharts3 = Highcharts::generate($array3, $this->pdo, $trad);
        
        /* DONUT 4 Délai de paiement */
        $array4['value'] = $delaiPaiement;
        $array4['detailDonut'] = ['nom' => $trad->t('Délai'),'innerSize' => '160', 'tooltip' => ' %'];
        $array4['titre'] = $trad->t('Délai de paiement');
        $array4['subTitle'] = ['titre' => $delaiPaiement[0]['value'].'%'];
        $array4['legend'] = true;
        $array4['type'] = 'pie';
        
        $highcharts4 = Highcharts::generate($array4, $this->pdo, $trad);
        

        /* GRPAH1 nombre incident */
        $nbIncident = $indicateurFacture->nbIncidentByMois($post['annee'] ?? date('Y'));

        $array5['value'] = $nbIncident;
        $array5['categorie'] = 'mois';
        $array5['detailColone']['NOMBRE_INCIDENT'] = ['nom' => $trad->t('Nombre d\'incidents')];
        $array5['titre'] = $trad->t("Nombre d\'incidents").' - '.($post['annee'] ?? date('Y'));
        $array5['legendGraph'][] = ['texte' => $trad->t("Nombre d\'incidents"), 'format' => '', 'axe' => 'gauche'];
        $array5['type'] = 'area';

        $highcharts5 = Highcharts::generate($array5, $this->pdo, $trad);
        
        $listeNbFactureMois = $indicateurFacture->nbFactureByMois($post['annee'] ?? date('Y'));
        /* GRAPH 2 nombrefacture */
        $array6['value']                  = $listeNbFactureMois;
        $array6['categorie']              = 'mois';
        $array6['titre']                  = $trad->t('Montant et nombre de factures').' - '.($post['annee'] ?? date('Y'));
        $array6['type']                   = 'column';
        $array6['detailColone']['NBRE_FACTURE'] = ['nom' => $trad->t('Nombre de facture'), 'type' => 'column', 'axeLier' => 'gauche'];
        $array6['detailColone']['MONTANT']     = ['nom' => $trad->t('Montant de facture'), 'type' => 'spline', 'axeLier' => 'droit', 'tooltip' => $_SESSION['parametrage']['DEVISE_BASE']];
        $array6['legendGraph'][]          = ['texte' => $trad->t('Nbr de facture'), 'format' => '', 'axe' => 'gauche'];
        $array6['legendGraph'][]          = ['texte' => $trad->t('Montant'), 'format' => ' '.$_SESSION['parametrage']['DEVISE_BASE'], 'axe' => 'droit'];
        
        $highcharts6 = Highcharts::generate($array6, $this->pdo, $trad);
        
        \Helpers\Ariane\Ariane::generate(0, $trad->t('Analyse'));
        
        return $this->renderer->render('@gestion-facture/indicateur', [
            'menuActive' => 'Facture',
            'periodes' => $periodes,
            'annee' => ($post['annee'] ?? date('Y')),
            'dataHighchart1' => $highcharts1,
            'dataHighchart2' => $highcharts2,
            'dataHighchart3' => $highcharts3,
            'dataHighchart4' => $highcharts4,
            'dataHighchart5' => $highcharts5,
            'dataHighchart6' => $highcharts6,
        ]);
    }
}
