<?php
/**
 * Programme des gestions du dispatch d'article dans des AO depuis le panier envours, environnement temporaire et appel de web services final pour la génération en DB
 */
require_once 'application/class/class_panier.php';

$action = getValue('action');
$OBJECT = getValue('OBJECT');

// Initialisation de la popup pour la saisie des réponses
if ($action == 'initPopup') {
    // retour de l'affichage de la popup de selection fournisseur et donc proposition de création
    
    $aoReponse = AO_Reponse::getIntance();
    $aoReponse->setProperties('NO_AO', getValue('no_ao'));
    
    // $aoReponse->setProperties('modeCreation',getValue('modeCreation'));
    // $aoReponse->setProperties('dateReponse',getValue('dateReponse'));
    // $aoReponse->setProperties('codeFournisseur',getValue('codeFournisseur'));
    // $aoReponse->setProperties('typeFournisseur',getValue('typeFournisseur'));
    // $aoReponse->setProperties('conformeVariante',getValue('conformeVariante'));
    
    AO_Reponse::afficheListeFournisseurAo($aoReponse->buildXmlFour());
} else {
    $aoReponse = AO_Reponse::restoreSession();
    if ($action == 'AO_Select_Four') {
        // Selection du fournisseur et donc lancement du process de creation de la reponse
        if ($aoReponse->setSelectFour(getValue('index'))) {
            
            $indexReponse = getValue('select');
            $aoReponse->setSelectFour(getValue('index'));
            $aoReponse->setSelectIndexReponse(getValue('select'));
            $aoReponse->setProperties('VARIANTE', getValue('VARIANTE'));
            
            echo 'var base = window.parent;';
            echo 'base.closePopup("popup_saisie_rep",true);';
            echo "base.OpenPopup('popup_ao_saisie_rep.php?action=initPopupReponse','popup_saisie_suite',1000,800,false,'agrandir');";
        }
    } else if ($action == 'initPopupReponse') {
        if ($aoReponse->initObjectReponse())
            AO_Reponse::afficheElement($aoReponse->buildXml(), $aoReponse);
    } else if ($action == AO_Reponse::ACTION_SUPPR_LG) {
        if ($aoReponse->deleteSousLigne(getValue('NO_PO_AO_SS'))) {}
        $genXsl = new GenXslt(true);
        $genXsl->XslHtml(AO_Reponse::xslPopup, $aoReponse->buildXml());
        
        $genXsl->echoHtml('ligne_AO');
    } else if ($action == AO_Reponse::ACTION_ONGLET_BTN) {
        if ($aoReponse->actionOnglet(getValue('name'))) {
            $genXsl = new GenXslt(true);
            $genXsl->XslHtml(AO_Reponse::xslPopup, $aoReponse->buildXml());
            
            $genXsl->echoHtml($aoReponse->getIdOnglet());
            $genXsl->echoHtml($aoReponse->getIdPanel());
            $genXsl->echoHtml('ligne_AO_ent');
            $genXsl->echoHtml('ligne_AO');
        }
    } else if ($action == 'changePage') {
        if ($aoReponse->changePage(getValue('onglet'), getValue('page'))) {
            $genXsl = new GenXslt(true);
            $genXsl->XslHtml(AO_Reponse::xslPopup, $aoReponse->buildXml());
            
            $genXsl->echoHtml($aoReponse->getIdOnglet());
            $genXsl->echoHtml($aoReponse->getIdPanel());
            $genXsl->echoHtml('ligne_AO_ent');
            $genXsl->echoHtml('ligne_AO');
        }
    } else if ($action == AO_Reponse::ACTION_PANEL_BTN) {
        if ($aoReponse->actionPanel(getValue('name'))) {
            $genXsl = new GenXslt(true);
            $genXsl->XslHtml(AO_Reponse::xslPopup, $aoReponse->buildXml());
            
            $genXsl->echoHtml($aoReponse->getIdPanel());
            $genXsl->echoHtml('ligne_AO');
        }
    } else if ($action == AO_Reponse::ACTION_SPLIT_LG) {
        if ($aoReponse->splitLigne(getValue('NO_PO_AO'))) {
            $aoReponse->setModeDetail(true);
            
            $genXsl = new GenXslt(true);
            $genXsl->XslHtml(AO_Reponse::xslPopup, $aoReponse->buildXml());
            
            $genXsl->echoHtml($aoReponse->getIdPanel());
            $genXsl->echoHtml('ligne_AO');
        }
    } else if ($action == 'zoomListe') {
        $aoReponse->afficheLoupe(getValue('OBJECT'));
    } else if ($action == AO_Reponse::ACTION_SAVE_OBJ) { // Sauvegarde des elements
        if ($OBJECT != '')
            if (! ($aoReponse->controleSaisie($OBJECT, getValue($OBJECT)) && $aoReponse->saveValue($OBJECT))) {
                if ($aoReponse->getErrorMessage() != '')
                    Affichage::messageJS($aoReponse->getErrorMessage());
                Affichage::setValueForm(AO_Reponse::formSaisie, $OBJECT, StdXmlWsdl::getValueZone($aoReponse->getNodeControl()));
            
            /**
             * En plus il est possible qun refresh soit à réaliser exemple changement du code fabricant ayant un effet sur le zoom de la partie gamme
             * et aussi la valeur du zoom
             */
            }
    } else if (stripos($OBJECT, AO::TAG_SUP_FOUR) === 0) {} else if ($OBJECT == AO_Reponse::ACTION_VALIDE_REP_CNT) {
        $url = $aoReponse->lanceQuestionnaire();
        
        // Retiré demande YA + PP
        if ($aoReponse->valideReponse()) {
            // Affichage::messageJS(trad('La validation a bien été effectuée.'));
            echo 'Oxa_getDevice().closePopup("popup_saisie_suite");';
            echo 'setTimeout(function(){ parent.' . $url . ' }, 0);';
        }
    } else if ($OBJECT == AO_Reponse::ACTION_VALIDE_REP) {
        // Retiré demande YA + PP
        if ($aoReponse->valideReponse()) {
            // Affichage::messageJS(trad('La validation a bien été effectuée.'));
            echo 'Oxa_getDevice().closePopup("popup_saisie_suite");';
        }
    }
}

$connecteur->disconnect();
