<?php
namespace Modules\Commande\Controller;

use Skeleton\Controller\Controller;
use Entity\Model\Pays;
use Helpers\Outil\Sql;
use Modules\Commande\Repository\ModificationDetailCommande;
use Modules\Ajax\Repository\Listes;
use Modules\Commande\Repository\CreationCommande;
use Modules\Commande\Service\CreationCommandeService;
use Modules\Commande\Service\OutilCommandeService;

class AjaxModificationCommandeController extends Controller
{
    /* ONGLET FOURNISSEUR */
    public function ajaxValueContactFrsAction() : string
    {
        $post = $this->request->getParsedBody();
        $idCommande = $this->request->getAttribute('idCommande');
        $param = [];

        if ($post['idContact']!=='') {
            $outil = new ModificationDetailCommande($this->pdo);
            $param = OutilCommandeService::parametreModifCde($this->pdo, $idCommande);
            $contact = $outil->detailContactFrs($post['idContact'], $post['idFournisseur'], 'F');
            $fournisseur = $outil->infoFournisseur($post['idFournisseur']);

            $contactCde['IND_FOU'] = $contact['IND_FOU'];
            $contactCde['CTCT_FOU'] = $contact['NOM'];
            
            $contactCde['EMA_CTCT'] = $contact['EMAIL']? $contact['EMAIL']:$fournisseur['EMAIL'];
            $contactCde['TEL_CTCT'] = $contact['TEL']? $contact['TEL']:$fournisseur['TEL'];
            $contactCde['FAX_CTCT'] = $contact['FAX']? $contact['FAX']:$fournisseur['FAX'];
            $contactCde['PORTCFOU'] = $contact['TELPORT']? $contact['TELPORT']:$fournisseur['TELEX'];
        }

        return $this->renderer->render('@Commande/Creation/detailEntete', [
            'modeRefreshDiv'   => true,
            'commande' => $contactCde ?? [],
            'param' => $param,
            'idFournisseur' => $post['idFournisseur'],
            'idCommande' => $idCommande,
        ]);
    }
    
    public function ajaxValueAdresseFrsAction() : string
    {
        $post = $this->request->getParsedBody();
        $idCommande = $this->request->getAttribute('idCommande');

        $param = OutilCommandeService::parametreModifCde($this->pdo, $idCommande);
        
        if ($post['COD_ADR_FOU']!=='') {
            $liste = new Listes($this->pdo);
            $adresse = $liste->adresseFournisseur(['filter_egal_CODE' => $post['CODE_FOU'], 'filter_egal_COD_ADR' => $post['COD_ADR_FOU']])[0];
            
            $post['COD_ADR_FOU']    = $adresse['COD_ADR'];
            $post['ADCDEFOU']   = $adresse['ADRESSE'];
            $post['VILLEFOU']   = $adresse['VILLE1'];
            $post['CPCDEFO']    = $adresse['CPOSTAL'];
            $post['PAYCDEFO']   = $adresse['CODPAYS'];
        }
        $tabPays = (new Pays($this->pdo))->listChoice();
                
        return $this->renderer->render('@Commande/Creation/DetailEntete', [
            'modeRefreshDiv' => true,
            'tabPays'  => $tabPays,
            'commande' => $post,
            'param' => $param,
            'idCommande' => $idCommande,
        ]);
    }
    
    
    
    /* ONGLET LIVRAISON */
    public function ajaxValueContactCentreAction() : string
    {
        $commande = $this->request->getParsedBody();
        $idCommande = $this->request->getAttribute('idCommande');
        $numContact = $this->request->getAttribute('numContact');

        $param = OutilCommandeService::parametreModifCde($this->pdo, $idCommande);
        
        if ($commande['CTC_IND1']!=='' && $numContact=='1') {
            $outil = new ModificationDetailCommande($this->pdo);
            $contact = $outil->detailContactCentre($commande['CTR_LIV'], $commande['CTC_IND1']);
            
            $commande['NMCTC1L']   = $contact['CTC_NOM'];
            $commande['FTCTC1L']   = $contact['CTC_FCT'];
            $commande['MLCTC1L']   = $contact['CTC_MAIL'];
            $commande['TLCTC1L']   = $contact['CTC_TEL'];
            $commande['PRCTC1L']   = $contact['CTC_GSM'];
            $commande['FXCTC1L']   = $contact['CTC_FAX'];
        } else {
            $commande['NMCTC1L']   = '';
            $commande['FTCTC1L']   = '';
            $commande['MLCTC1L']   = '';
            $commande['TLCTC1L']   = '';
            $commande['PRCTC1L']   = '';
            $commande['FXCTC1L']   = '';
        }
        
        if ($commande['CTC_IND2']!=='' && $numContact=='2') {
            $outil = new ModificationDetailCommande($this->pdo);
            $contact = $outil->detailContactCentre($commande['CTR_LIV'], $commande['CTC_IND2']);
            
            $commande['NMCTC2L']   = $contact['CTC_NOM'];
            $commande['FTCTC2L']   = $contact['CTC_FCT'];
            $commande['MLCTC2L']   = $contact['CTC_MAIL'];
            $commande['TLCTC2L']   = $contact['CTC_TEL'];
            $commande['PRCTC2L']   = $contact['CTC_GSM'];
            $commande['FXCTC2L']   = $contact['CTC_FAX'];
        } else {
            $commande['NMCTC2L']   = '';
            $commande['FTCTC2L']   = '';
            $commande['MLCTC2L']   = '';
            $commande['TLCTC2L']   = '';
            $commande['PRCTC2L']   = '';
            $commande['FXCTC2L']   = '';
        }
        
        return $this->renderer->render('@Commande/Creation/DetailEntete', [
            'modeRefreshDiv'   => true,
            'commande' => $commande,
            'param' => $param,
            'idCommande' => $idCommande,
        ]);
    }
    
    public function ajaxValueLivraisonAction() : string
    {
        $commande = $this->request->getParsedBody();
        $idCommande = $this->request->getAttribute('idCommande');

        $param = OutilCommandeService::parametreModifCde($this->pdo, $idCommande);

        $tabPays = (new Pays($this->pdo))->listChoice();
        
        $outil = new ModificationDetailCommande($this->pdo);
        $centre = $outil->adresseCentre($commande['CTR_LIV']);
        $contact = $outil->contactCentre($commande['CTR_LIV']);
        
        $commande['CTR_LIV']    = $centre['CODE_CTR'];
        $commande['ADR_LIV']    = $centre['ADRESSE'];
        $commande['PAY_LIV']    = $centre['PAYS'];
        $commande['CPLIV']      = $centre['CPOSTAL'];
        $commande['VILLELIV']   = $centre['VILLE1'];
        $commande['VIL_LIV']    = $centre['VILLE1'];
        
        if (count($contact)>=1) {
            $commande['NMCTC1L']   = $contact[0]['CTC_NOM'];
            $commande['FTCTC1L']   = $contact[0]['CTC_FCT'];
            $commande['MLCTC1L']   = $contact[0]['CTC_MAIL'];
            $commande['TLCTC1L']   = $contact[0]['CTC_TEL'];
            $commande['PRCTC1L']   = $contact[0]['CTC_GSM'];
            $commande['FXCTC1L']   = $contact[0]['CTC_FAX'];
        } else {
            $commande['NMCTC1L']   = '';
            $commande['FTCTC1L']   = '';
            $commande['MLCTC1L']   = '';
            $commande['TLCTC1L']   = '';
            $commande['PRCTC1L']   = '';
            $commande['FXCTC1L']   = '';
        }
        if (count($contact)>=2) {
            $commande['NMCTC2L']   = $contact[1]['CTC_NOM'];
            $commande['FTCTC2L']   = $contact[1]['CTC_FCT'];
            $commande['MLCTC2L']   = $contact[1]['CTC_MAIL'];
            $commande['TLCTC2L']   = $contact[1]['CTC_TEL'];
            $commande['PRCTC2L']   = $contact[1]['CTC_GSM'];
            $commande['FXCTC2L']   = $contact[1]['CTC_FAX'];
        } else {
            $commande['NMCTC2L']   = '';
            $commande['FTCTC2L']   = '';
            $commande['MLCTC2L']   = '';
            $commande['TLCTC2L']   = '';
            $commande['PRCTC2L']   = '';
            $commande['FXCTC2L']   = '';
        }

        return $this->renderer->render('@Commande/Creation/DetailEntete', [
            'modeRefreshDiv' => true,
            'tabPays'  => $tabPays,
            'commande' => $commande,
            'param' => $param,
            'idCommande' => $idCommande,
        ]);
    }
    
    
    
    public function ajaxValueAdresseLivAction() : string
    {
        $post = $this->request->getParsedBody();
        $idCommande = $this->request->getAttribute('idCommande');

        $param = OutilCommandeService::parametreModifCde($this->pdo, $idCommande);

        if ($post['IND_ADR_LIV']!=='') {
            $liste = new Listes($this->pdo);
            $adresse = $liste->adresseCentre(['filter_egal_CODE_CTR' => $post['CTR_LIV'], 'filter_egal_COD_ADR' => $post['IND_ADR_LIV']])[0];
            
            $post['IND_ADR_LIV']    = $adresse['COD_ADR'];
            $post['ADR_LIV']        = $adresse['ADRESSE'];
            $post['VIL_LIV']        = $adresse['VILLE1'];
            $post['CPLIV']          = $adresse['CPOSTAL'];
            $post['PAY_LIV']        = $adresse['CODPAYS'];
            $post['VILLELIV']       = $adresse['VILLE1'];
        }
        
        return $this->renderer->render('@Commande/Creation/DetailEntete', [
            'modeRefreshDiv' => true,
            'tabPays'  => (new Pays($this->pdo))->listChoice(),
            'commande' => $post,
            'param' => $param,
            'idCommande' => $idCommande,
        ]);
    }
    
    
    
    
    
    /* ONGLET TRANSIT */
    public function ajaxValueContactTransitaireAction() : string
    {
        $post = $this->request->getParsedBody();
        $idCommande = $this->request->getAttribute('idCommande');
        
        $param = OutilCommandeService::parametreModifCde($this->pdo, $idCommande);

        if ($post['IND_TRANS']!=='') {
            $outil = new ModificationDetailCommande($this->pdo);
            $contact = $outil->detailContactFrs($post['IND_TRANS'], $post['TRANSIT'], 'F');
            $fournisseur = $outil->infoFournisseur($post['TRANSIT']);
            
            $post['IND_TRANS']        = $contact['IND_FOU'];
            $post['CT_TRANS']         = $contact['NOM'];
            
            $post['MAICTTRS'] = $contact['EMAIL']   ? $contact['EMAIL']:$fournisseur['EMAIL'];
            $post['TELCTTRS'] = $contact['TEL']     ? $contact['TEL']:$fournisseur['TEL'];
            $post['FAXCTTRS'] = $contact['FAX']     ? $contact['FAX']:$fournisseur['FAX'];
            $post['PORCTTRS'] = $contact['TELPORT'] ? $contact['TELPORT']:$fournisseur['TELEX'];
        } else {
            $liste = new Listes($this->pdo);
            $listeContact = $liste->contactsFournisseur(['CODE_FOU' => $post['TRANSIT']]);

            $post['IND_TRANS']        = $listeContact[0]['IND_FOU'];
            $post['CT_TRANS']         = $listeContact[0]['NOM'];
            
            $post['MAICTTRS'] = $listeContact[0]['EMAIL'];
            $post['TELCTTRS'] = $listeContact[0]['TEL'];
            $post['FAXCTTRS'] = $listeContact[0]['FAX'];
            $post['PORCTTRS'] = $listeContact[0]['TELPORT'];
        }
        
        return $this->renderer->render('@Commande/Creation/DetailEntete', [
            'modeRefreshDiv'   => true,
            'commande' => $post,
            'param' => $param,
            'idCommande' => $idCommande,
        ]);
    }

    public function ajaxValueAdresseFrsLivAction() : string
    {
        $post = $this->request->getParsedBody();
        $idCommande = $this->request->getAttribute('idCommande');

        $param = OutilCommandeService::parametreModifCde($this->pdo, $idCommande);

        if ($post['IND_ADRLIVR']!=='') {
            $liste = new Listes($this->pdo);
            $adresse = $liste->adresseFournisseur(['filter_egal_CODE' => $post['FOURLIVR'], 'filter_egal_COD_ADR' => $post['IND_ADRLIVR']])[0];
            
            $post['IND_ADRLIVR']    = $adresse['COD_ADR'];
            $post['ADRLIVR']        = $adresse['ADRESSE'];
            $post['PAYLIVR']        = $adresse['CODPAYS'];
            $post['CPLIVR']         = $adresse['CPOSTAL'];
            $post['VIL_LIVR']       = $adresse['VILLE1'];
            $post['TLADLVEN']       = $adresse['TELEPHONE'];
            $post['FXADLVEN']       = $adresse['FAX'];
        } else {
            $liste = new Listes($this->pdo);
            $adresse = $liste->adresseFournisseur(['filter_egal_CODE' => $post['FOURLIVR']])[0];
            
            $post['IND_ADRLIVR']    = $adresse['COD_ADR'];
            $post['ADRLIVR']        = $adresse['ADRESSE'];
            $post['PAYLIVR']        = $adresse['CODPAYS'];
            $post['CPLIVR']         = $adresse['CPOSTAL'];
            $post['VIL_LIVR']       = $adresse['VILLE1'];
            $post['TLADLVEN']       = $adresse['TELEPHONE'];
            $post['FXADLVEN']       = $adresse['FAX'];
        }
        
        return $this->renderer->render('@Commande/Creation/DetailEntete', [
            'modeRefreshDiv' => true,
            'tabPays'  => (new Pays($this->pdo))->listChoice(),
            'commande' => $post,
            'param' => $param,
            'idCommande' => $idCommande,
        ]);
    }
    
    
    
    /* ONGLET EXPORT */
    public function ajaxValueContactExportAction() : string
    {
        $post = $this->request->getParsedBody();
        $idCommande = $this->request->getAttribute('idCommande');

        $param = OutilCommandeService::parametreModifCde($this->pdo, $idCommande);

        if ($post['IND_CTORGCT']!=='') {
            $outil = new ModificationDetailCommande($this->pdo);
            $contact = $outil->detailContactFrs($post['IND_CTORGCT'], $post['ORGCTRL'], 'F');
            $fournisseur = $outil->infoFournisseur($post['ORGCTRL']);
            
            $post['IND_CTORGCT']    = $contact['IND_FOU'];
            $post['CTORGCT']  = $contact['NOM'];
            
            $post['MAICTORG'] = $contact['EMAIL']   ? $contact['EMAIL']     :$fournisseur['EMAIL'];
            $post['TELCTORG'] = $contact['TEL']     ? $contact['TEL']       :$fournisseur['TEL'];
            $post['FAXCTORG'] = $contact['FAX']     ? $contact['FAX']       :$fournisseur['FAX'];
            $post['PORCTORG'] = $contact['TELPORT'] ? $contact['TELPORT']   :$fournisseur['TELEX'];
        }
        
        return $this->renderer->render('@Commande/Creation/DetailEntete', [
            'modeRefreshDiv'   => true,
            'commande' => $post,
            'param' => $param,
            'idCommande' => $idCommande,
        ]);
    }
}
