<?php


namespace Modules\DemandeAchat\Controller;

use Helpers\ChampsSupplementaire\ChampSupp;
use Helpers\Outil\Roles;
use Modules\GlobalApplication\Controller\AbsExportController;
use Entity\Model\DaEntet;
use Entity\Model\DaLigne;
use Modules\DemandeAchat\Repository\DemandeAchat;
use Modules\Suivi\Repository\SuiviDaRepo;

class ExportDemandeAchatController extends AbsExportController
{

    public function popupExportAction(): string
    {
        $origin = $this->request->getAttribute('origin') ?? 0;

        $post['multiSelectexportCdeOBLI1'] = 'DA_ENTET.NO_DA';
        $post['multiSelectexportCdeOBLI2'] = 'DA_LIGNE.NO_PO';

        $tab = $this->getExportTab();
        $tab = $this->getLabelField($this->getExportTab());

        if ($origin == 'DA' && !Roles::droitsUtilisateur($this->pdo, 'HA_CA_VIPX')) {
            unset($tab['DA_ENTET.TOTAL_DA']);
            unset($tab['DA_ENTET.TOTAL_DA_TTC']);

            unset($tab['DA_LIGNE.PU']);
            unset($tab['DA_LIGNE.MNT_LIG']);
        }

        return $this->renderer->render('@GlobalApplication/Popup/popupExport', [
            'pathExport' => $this->router->generateUri(
                'DemandeAchat.Export.export',
                ['origin' => $origin]
            ),
            'formFilter' => ($origin == 'DA'
                ? 'filtre_suivi'
                : 'filtre_demandeAchat'
            ),
            'coloneExport' => $tab,
            'tabObli' => ['DA_ENTET.NO_DA', 'DA_LIGNE.NO_PO'],
            'post' => $post,
            'modeDebug' => false,
        ]);
    }

    public function exportAction(): string
    {
        $post = $this->request->getParsedBody();
        $origin = $this->request->getAttribute('origin');
        $trad = $this->getTranslationService();
        
        // Column ambigu
        $post['tri_DA_ENTET.NO_DA'] = $post['tri_NO_DA'] ?? null;
        unset($post['tri_NO_DA']);
        $post['tri_DA_ENTET.CODE_STE'] = $post['tri_CODE_STE'] ?? null;
        unset($post['tri_CODE_STE']);
        unset($post['tri_ETAT']);

        $listeSelect = $this->extractFieldList($post);

        if (empty($post['okFilter'])) {
            $post = [];
        }
        
        //___________________
        // Send

        if ($origin == 'DA') {
            $export = (new SuiviDaRepo($this->pdo, $this->router, $trad))->export($post, $listeSelect);
        } else {
            $export = (new DemandeAchat($this->pdo))->export($post, $listeSelect);
        }
        
        $this->getEmailCsvService()->sendCsv(
            'export_DA_' . date('Ymd'),
            $export,
            \Closure::fromCallable([$this,'formatExportRow']),
            $this->getLabelField($listeSelect)
        );

        return 'ok';
    }

    /**
     * Summary of getLabelField
     * @param array<mixed> $tabTmp
     * @return array<mixed>
     */
    private function getLabelField($tabTmp)
    {
        $listeChpsEntete = (new DaEntet($this->pdo))->getArchitecture($this->getTranslationService());
        $listeChpsLigne = (new DaLigne($this->pdo))->getArchitecture($this->getTranslationService());
        $listeCSEntete = (new ChampSupp($this->pdo))->getInfoCS('DA_ENTET');
        $listeCSLigne = (new ChampSupp($this->pdo))->getInfoCS('DA_LIGNE');

        $libEntete = $this->getTranslationService()->t('Entête');
        $libLigne = $this->getTranslationService()->t('Ligne');

        $tab = [];
        foreach ($tabTmp as $tmp) {
            if (str_starts_with($tmp, 'DA_ENTET.MEMOS.',)) {
                $tab[$tmp] = $libEntete.' '.$listeChpsEntete[(string)str_replace('DA_ENTET.MEMOS.', '', $tmp)]['nomChamp'];
            } elseif (str_starts_with($tmp, 'DA_LIGNE.MEMOS.',)) {
                $tab[$tmp] = $libLigne.' '.$listeChpsLigne[(string)str_replace('DA_LIGNE.MEMOS.', '', $tmp)]['nomChamp'];
            } elseif (str_starts_with($tmp, 'DA_ENTET.CS_DON.',)) {
                $tab[$tmp] = $libEntete.' '.$listeCSEntete[(string)str_replace('DA_ENTET.CS_DON.', '', $tmp)]['NOM'];
            } elseif (str_starts_with($tmp, 'DA_LIGNE.CS_DON.',)) {
                $tab[$tmp] = $libLigne.' '.$listeCSLigne[(string)str_replace('DA_LIGNE.CS_DON.', '', $tmp)]['NOM'];
            } elseif (str_starts_with($tmp, 'DA_ENTET.',)) {
                $tab[$tmp] = $libEntete.' '.$listeChpsEntete[(string)str_replace('DA_ENTET.', '', $tmp)]['nomChamp'];
            } elseif (str_starts_with($tmp, 'DA_LIGNE.',)) {
                $tab[$tmp] = $libLigne.' '.$listeChpsLigne[(string)str_replace('DA_LIGNE.', '', $tmp)]['nomChamp'];
            } elseif (str_starts_with($tmp, 'oxaExportFOU.RS')) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé fournisseur');
            } elseif (str_starts_with($tmp, 'oxaExportCODE_STE.RAIS_SOC',)) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé société');
            } elseif (str_starts_with($tmp, 'oxaExportNO_DEM.NOM',)) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé demandeur');
            } elseif (str_starts_with($tmp, 'oxaExportSECTIMP.LIBELLE',)) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé section d\'imputation');
            } elseif (str_starts_with($tmp, 'oxaExportCODECOMP.LIBELLE',)) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé code comptable');
            } elseif (str_starts_with($tmp, 'oxaExportSSCPTEANA.LIBELLE',)) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé sous compte d\'analyse');
            } elseif (str_starts_with($tmp, 'oxaExportACD.LIBELLE',)) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé accord-cadre');
            } elseif (str_starts_with($tmp, 'oxaExportDAENT_CENTRE_EMETTEUR.RAIS_SOC',)) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé centre emetteur');
            } elseif (str_starts_with($tmp, 'oxaExportCTR_LIV.RAIS_SOC',)) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé centre de livraison');
            } elseif (str_starts_with($tmp, 'oxaExportCOND_PAIE.LIBELLE',)) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé condition de paiement');
            } elseif (str_starts_with($tmp, 'oxaExportMODE_PAIE.LIBELLE',)) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé mode de paiement');
            } elseif (str_starts_with($tmp, 'oxaExportMODE_LIV.LIBELLE',)) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé mode de livraison');
            } elseif (str_starts_with($tmp, 'oxaExport_LIGNE_SSFAM.DESIGNAT',)) {
                $tab[$tmp] = $libLigne.' '.$this->getTranslationService()->t('Libellé sous-famille');
            } elseif (str_starts_with($tmp, 'oxaExport_LIGNE_COD_FAB.RS',)) {
                $tab[$tmp] = $libLigne.' '.$this->getTranslationService()->t('Libellé code fabricant');
            } elseif (str_starts_with($tmp, 'oxaExport_LIGNE_COD_GAM.DESIGN',)) {
                $tab[$tmp] = $libLigne.' '.$this->getTranslationService()->t('Libellé code gamme');
            } elseif (str_starts_with($tmp, 'oxaExport_LIGNE_SITE_STK.DESIGN',)) {
                $tab[$tmp] = $libLigne.' '.$this->getTranslationService()->t('Libellé site de stock');
            } elseif (str_starts_with($tmp, 'oxaExport_LIGNE_SECTIMP.LIBELLE',)) {
                $tab[$tmp] = $libLigne.' '.$this->getTranslationService()->t('Libellé section d\'imputation');
            } elseif (str_starts_with($tmp, 'oxaExport_LIGNE_CODE_COMP.LIBELLE',)) {
                $tab[$tmp] = $libLigne.' '.$this->getTranslationService()->t('Libellé code complémentaire');
            } elseif (str_starts_with($tmp, 'oxaExport_LIGNE_SSCPTEANA.LIBELLE',)) {
                $tab[$tmp] = $libLigne.' '.$this->getTranslationService()->t('Libellé sous-compte d\'analyse');
            } elseif (str_starts_with($tmp, 'oxaExport_LIGNE_CODE_STE.RAIS_SOC',)) {
                $tab[$tmp] = $libLigne.' '.$this->getTranslationService()->t('Libellé société');
            } elseif (str_starts_with($tmp, 'oxaExport_LIGNE_ACD.LIBELLE',)) {
                $tab[$tmp] = $libLigne.' '.$this->getTranslationService()->t('Libellé accord-cadre');
            } elseif (str_starts_with($tmp, 'oxaExport_LIGNE_CODE_FOU.RS',)) {
                $tab[$tmp] = $libLigne.' '.$this->getTranslationService()->t('Libellé fournisseur');
            } elseif (str_starts_with($tmp, 'oxaExport_LIGNE_ACHETEUR.NOM',)) {
                $tab[$tmp] = $libLigne.' '.$this->getTranslationService()->t('Libellé acheteur');
            } elseif (str_starts_with($tmp, 'oxaExportCTRLIVR.RAIS_SOC',)) {
                $tab[$tmp] = $libEntete.' '.$this->getTranslationService()->t('Libellé centre de livraison');
            }
        }

        return $tab;
    }

//______________________________________________________________________________
// Sub-routine

    /**
     * @return string[]
     */
    protected function getExportTab(): array
    {
        $tab = array_merge(
            $this->getFieldListFullPath(new DaEntet($this->pdo)),
            $this->getFieldListFullPath(new DaLigne($this->pdo))
        );

        $tab[] = 'oxaExportFOU.RS';

        $tab[] = 'oxaExportCODE_STE.RAIS_SOC';
        $tab[] = 'oxaExportNO_DEM.NOM';
        $tab[] = 'oxaExportSECTIMP.LIBELLE';
        $tab[] = 'oxaExportCODECOMP.LIBELLE';
        $tab[] = 'oxaExportSSCPTEANA.LIBELLE';
        $tab[] = 'oxaExportACD.LIBELLE';
        $tab[] = 'oxaExportDAENT_CENTRE_EMETTEUR.RAIS_SOC';
        $tab[] = 'oxaExportCTR_LIV.RAIS_SOC';
        $tab[] = 'oxaExportCOND_PAIE.LIBELLE';
        $tab[] = 'oxaExportMODE_PAIE.LIBELLE';
        $tab[] = 'oxaExportMODE_LIV.LIBELLE';


        $tab[] = 'oxaExport_LIGNE_SSFAM.DESIGNAT';
        $tab[] = 'oxaExport_LIGNE_COD_FAB.RS';
        $tab[] = 'oxaExport_LIGNE_COD_GAM.DESIGN';
        $tab[] = 'oxaExport_LIGNE_SITE_STK.DESIGN';
        $tab[] = 'oxaExport_LIGNE_SECTIMP.LIBELLE';
        $tab[] = 'oxaExport_LIGNE_CODE_COMP.LIBELLE';
        $tab[] = 'oxaExport_LIGNE_SSCPTEANA.LIBELLE';
        $tab[] = 'oxaExport_LIGNE_CODE_STE.RAIS_SOC';
        $tab[] = 'oxaExport_LIGNE_ACD.LIBELLE';
        $tab[] = 'oxaExport_LIGNE_CODE_FOU.RS';
        $tab[] = 'oxaExport_LIGNE_ACHETEUR.NOM';
        $tab[] = 'oxaExportCTRLIVR.RAIS_SOC';

        return $tab;
    }
}
