<?php
// TODO: Mettre en place une zone de saisie d'un filtre sur la table des produits
global $aModel;
global $Route;

$metacss = '';
$barre = "";
ob_start();
?>
  <div class="row">
    <div class="col-md-8">
      <div class="panel panel-primary">
        <div class="panel-heading">
          <h3 class="panel-title"><i class="fa fa-cubes"></i> Liste des marchés
            <button class="btn btn-success btn-xs pull-right" id="btnCreateMarche" title="Créer un marché"><i class="fa fa-plus fa-lg"></i> Ajouter</button>
            <button class="btn btn-warning btn-xs pull-right mr-1" id="btnGestionListes" title="Gestion des listes tarifaires par marché"><i class="fa fa-stack-exchange fa-lg"></i> Listes tarifaires</button>
            <button class="btn btn-warning btn-xs pull-right mr-1" id="btnFamilles" title="Gestion des familles de produits"><i class="fa fa-stack-exchange fa-lg"></i> Familles</button>
          </h3>
        </div>
        <div class="border cm-scrollbar cm-table-w-scroll table-responsive table-800">
          <table id="tblMarches" class="table table-bordered table-striped table-fixed">
            <thead>
            <tr>
              <th class="header" scope="col" width="20%">Libellé</th>
              <th class="header text-center" scope="col" width="10%">Actif</th>
              <th class="header text-center" scope="col" width="10%">Type</th>
              <th class="header text-center" scope="col" width="15%">Date début</th>
              <th class="header text-center" scope="col" width="15%">Date fin</th>
              <th class="header text-center" scope="col" width="15%">Date validité Prix</th>
              <th class="header text-center" scope="col" width="15%">Date Import</th>
              <th class="header text-center" scope="col" width="10%">Actions</th>
            </tr>
            </thead>
            <tbody>
            <?php
            foreach ($aModel["lesmarches"] as $leMarche) {
              if ($leMarche["rowid"] == 999) {
                $style = 'style="background-color: #ccccff;font-weight: bold;font-size: 1.2em;"';
              } else {
                $style = '';
              }
              echo '<tr class="ligMarche">';
              echo '<td class="clickable celMarche" data-rowid="' . $leMarche["rowid"] . '" data-libelle="' . $leMarche["libelle"] . '" ' . $style . '>' . $leMarche["libelle"] . '</td>';
              echo '<td class="text-center clickable celMarche" data-rowid="' . $leMarche["rowid"] . '" data-libelle="' . $leMarche["libelle"] . '" ' . $style . '>';
              if ($leMarche["active"] == 1) {
                echo "Oui";
              } else {
                echo "non";
              }
              echo '</td>';
              echo '<td class="text-center clickable celMarche" data-rowid="' . $leMarche["rowid"] . '" data-libelle="' . $leMarche["libelle"] . '" ' . $style . '>';
              if ($leMarche["chk_marche_hybride"] == 1) {
                echo "Hybride";
                if ($leMarche["chk_remise_sur_tg"] == 1) {
                  echo " + TG";
                }
              } else {
                if ($leMarche["chk_remise_sur_tg"] == 1) {
                  echo "TG";
                } else {
                  echo "-";
                }
              }
              echo '</td>';
              echo '<td class="text-center clickable celMarche" data-rowid="' . $leMarche["rowid"] . '" data-libelle="' . $leMarche["libelle"] . '" ' . $style . '>' . affiche_date($leMarche["date_debut"]) . '</td>';
              // on change la couleur de fond de la cellule si la date de fin est dépassée
              if (strtotime($leMarche["date_fin"]) <= strtotime(date("Y-m-d"))) {
                echo '<td class="text-center clickable celMarche" data-rowid="' . $leMarche["rowid"] . '" data-libelle="' . $leMarche["libelle"] . '" style="background-color: #ff0000;color:white;">' . affiche_date($leMarche["date_fin"]) . '</td>';
              } else {
                // on change la couleur de fond de la cellule si la date de fin est dans un délai de 2 mois
                if (strtotime($leMarche["date_fin"]) <= strtotime(date("Y-m-d", strtotime("+3 months")))) {
                  echo '<td class="text-center clickable celMarche" data-rowid="' . $leMarche["rowid"] . '" data-libelle="' . $leMarche["libelle"] . '" style="background-color: #ffcccc;">' . affiche_date($leMarche["date_fin"]) . '</td>';
                } else {
                  echo '<td class="text-center clickable celMarche" data-rowid="' . $leMarche["rowid"] . '" data-libelle="' . $leMarche["libelle"] . '">' . affiche_date($leMarche["date_fin"]) . '</td>';
                }
              }
              echo '<td class="text-center clickable celMarche" data-rowid="' . $leMarche["rowid"] . '" data-libelle="' . $leMarche["libelle"] . '">' . affiche_date($leMarche["date_validite_prix"]) . '</td>';
              if (!empty((int)$leMarche["date_import"])) {
                $laDateImport = affiche_date($leMarche["date_import"]);
              } else {
                $laDateImport = "-";
              }
              echo '<td class="text-center clickable celMarche" data-rowid="' . $leMarche["rowid"] . '" data-libelle="' . $leMarche["libelle"] . '">' . $laDateImport . '</td>';
              echo '<td class="text-center">';
              echo '<a title="Modifier la fiche de ce marché" class="btn btn-xs btn-primary btnModMarche mr-1" data-act="E" data-rowid="' . $leMarche["rowid"] . '" data-libelle="' . $leMarche["libelle"] . '"><i class="fa fa-pencil fa-lg"></i></a>';
              if ($leMarche["rowid"] != 999) {
                //! si ce n'est pas le marché par défaut (hors marché), on autorise la suppression du marché
                echo '<a title="Supprimer ce marché" class="btn btn-xs btn-danger btnSuppMarche" data-act="D" data-rowid="' . $leMarche["rowid"] . '" data-libelle="' . $leMarche["libelle"] . '"><i class="fa fa-trash fa-lg"></i></a>';
              }
              echo '</td>';
              echo '</tr>';
            }

            ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>

    <div class="col-md-4">
      <div class="panel panel-primary">
        <div class="panel-heading">
          <h3 class="panel-title"><span id="pnlProduitsTitre"><i class="fa fa-cubes"></i> Produits du marché</span>
            <input type="text" class="w-25" id="inpSearchProduit" placeholder="Rechercher">
            <button class="btn btn-info btn-xs pull-right" id="btnImportProduits" title="Importer une mise à jour de produits sur un marché ou hors marché"><i class="fa fa-sign-in fa-lg"></i> Importer</button>
          </h3>
        </div>
        <div class="border cm-scrollbar cm-table-w-scroll table-responsive table-800">
          <table id="tblMarchesProduits" class="table table-bordered table-striped table-fixed">
            <thead>
            <tr>
              <th class="header" scope="col" width="15%">Code</th>
              <th class="header" scope="col" width="25%">Désignation</th>
              <th class="header" scope="col" width="15%">Groupe</th>
              <th class="header text-center" scope="col" width="15%">Prix Achat Net</th>
              <th class="header text-center" scope="col" width="15%">Prix Vente</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
<?php
$content = ob_get_contents();
ob_clean();

ob_start();
//! Affichage des modales
?>
  <div class="modal draggable fade" id="modalEditMarche" tabindex="-1" role="dialog" aria-labelledby="modMarcheTitre" data-backdrop="static">
    <div class="modal-dialog modal-xl">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="modMarcheTitre"></h4>
        </div>
        <div class="modal-body">
          <div class="row">
            <div class="col-md-12">
              <form class="form-horizontal" id="frmMarche" role="form" enctype="multipart/form-data">
                <input type="hidden" name="act" id="marcheAct" value=""/>
                <input type="hidden" name="rowid" id="marcheRowid" value=""/>

                <div class="form-group">
                  <label for="marcheLibelle" class="control-label col-md-2">Libellé :</label>
                  <div class="col-md-3"><input type="text" class="form-control" name="libelle" id="marcheLibelle" data-default="" required="required"/></div>
                  <label for="marcheNumero" class="control-label col-md-1">N° :</label>
                  <div class="col-md-2"><input type="text" class="form-control" name="numero" id="marcheNumero" data-default=""/></div>
                  <label for="marcheNom" class="control-label col-md-1">Nom :</label>
                  <div class="col-md-3"><input type="text" class="form-control" name="nom" id="marcheNom" data-default=""/></div>
                </div>
                <div class="form-group">
                  <label for="marcheChk_remise_sur_tg" class="control-label col-md-2">Remise sur TG :</label>
                  <div class="col-md-2">
                    <div class="checkbox"><input type="checkbox" name="chk_remise_sur_tg" id="marcheChk_remise_sur_tg"/></div>
                  </div>
                  <label for="marcheChk_prix_nets" class="control-label col-md-2">Prix nets :</label>
                  <div class="col-md-2">
                    <div class="checkbox"><input type="checkbox" name="chk_prix_nets" id="marcheChk_prix_nets"/></div>
                  </div>
                  <label for="marcheChk_marche_public" class="control-label col-md-2">Marché public :</label>
                  <div class="col-md-2">
                    <div class="checkbox"><input type="checkbox" name="chk_marche_public" id="marcheChk_marche_public"/></div>
                  </div>
                </div>

                <div class="form-group has-warning has-feedback">
                  <label for="marcheTaux_remise_trimestrielle" class="control-label col-md-2">RFA trimestrielle :</label>
                  <div class="col-md-2">
                    <div class="input-group">
                      <input type="number" min="0" step="0.01" max="9999" class="form-control" name="taux_remise_trimestrielle" id="marcheTaux_remise_trimestrielle" data-default=""/>
                      <span class="input-group-addon">&percnt;</span>
                    </div>
                  </div>
                  <label for="marcheTaux_remise_semestrielle" class="control-label col-md-2">RFA semestrielle :</label>
                  <div class="col-md-2">
                    <div class="input-group">
                      <input type="number" min="0" step="0.01" max="9999" class="form-control" name="taux_remise_semestrielle" id="marcheTaux_remise_semestrielle" data-default=""/>
                      <span class="input-group-addon">&percnt;</span>
                    </div>
                  </div>
                  <label for="marcheTaux_remise_annuelle" class="control-label col-md-2">RFA annuelle :</label>
                  <div class="col-md-2">
                    <div class="input-group">
                      <input type="number" min="0" step="0.01" max="9999" class="form-control" name="taux_remise_annuelle" id="marcheTaux_remise_annuelle" data-default=""/>
                      <span class="input-group-addon">&percnt;</span>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <label for="marcheDate_debut" class="control-label col-md-2">Date debut :</label>
                  <div class="col-md-2"><input type="text" class="form-control datepicker" name="date_debut" id="marcheDate_debut" data-default=""/></div>
                  <label for="marcheDate_fin" class="control-label col-md-2">Date fin :</label>
                  <div class="col-md-2"><input type="text" class="form-control datepicker" name="date_fin" id="marcheDate_fin" data-default=""/></div>
                  <label for="marcheDate_validite_prix" class="control-label col-md-2">Date validite prix :</label>
                  <div class="col-md-2"><input type="text" class="form-control datepicker" name="date_validite_prix" id="marcheDate_validite_prix" data-default=""/></div>
                </div>
                <div class="form-group">
                  <label for="marcheFranco_de_port" class="control-label col-md-2">Franco de port :</label>
                  <div class="col-md-2">
                    <div class="input-group">
                      <input type="number" min="0" step="1" max="9999" class="form-control" name="franco_de_port" id="marcheFranco_de_port" data-default=""/>
                      <span class="input-group-addon">&euro;</span>
                    </div>
                  </div>
                  <label for="marcheGarantie" class="control-label col-md-2">Garantie :</label>
                  <div class="col-md-2"><input type="text" class="form-control" name="garantie" id="marcheGarantie" data-default=""/></div>
                  <label for="marcheDelai_de_livraison" class="control-label col-md-2">Délai de livraison :</label>
                  <div class="col-md-2"><input type="text" class="form-control" name="delai_de_livraison" id="marcheDelai_de_livraison" data-default=""/></div>
                </div>
                <div class="form-group">
                  <label for="marcheRemises_commerciales" class="control-label col-md-2">Remises commerciales :</label>
                  <div class="col-md-8"><input type="text" class="form-control" name="remises_commerciales" id="marcheRemises_commerciales" data-default=""/></div>
                </div>
                <div class="form-group">
                  <label for="marcheRemise_palier_1" class="control-label col-md-2">Remise palier 1 :</label>
                  <div class="col-md-2">
                    <div class="input-group">
                      <input type="number" lang="fr" min="0" step="1" max="9999" class="form-control" name="remise_palier_1" id="marcheRemise_palier_1" data-default=""/>
                      <span class="input-group-addon">k&euro;</span>
                    </div>
                  </div>
                  <label for="marcheRemise_taux_1" class="control-label col-md-2">Remise taux 1 :</label>
                  <div class="col-md-2">
                    <div class="input-group">
                      <input type="number" lang="fr" min="0" step="0.01" max="99" class="form-control" name="remise_taux_1" id="marcheRemise_taux_1" data-default=""/>
                      <span class="input-group-addon">&percnt;</span>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <label for="marcheRemise_palier_2" class="control-label col-md-2">Remise palier 2 :</label>
                  <div class="col-md-2">
                    <div class="input-group">
                      <input type="number" lang="fr" min="0" step="1" max="9999" class="form-control" name="remise_palier_2" id="marcheRemise_palier_2" data-default=""/>
                      <span class="input-group-addon">k&euro;</span>
                    </div>
                  </div>
                  <label for="marcheRemise_taux_2" class="control-label col-md-2">Remise taux 2 :</label>
                  <div class="col-md-2">
                    <div class="input-group">
                      <input type="number" lang="fr" min="0" step="0.01" max="99" class="form-control" name="remise_taux_2" id="marcheRemise_taux_2" data-default=""/>
                      <span class="input-group-addon">&percnt;</span>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <label for="marcheRemise_palier_3" class="control-label col-md-2">Remise palier 3 :</label>
                  <div class="col-md-2">
                    <div class="input-group">
                      <input type="number" lang="fr" min="0" step="1" max="9999" class="form-control" name="remise_palier_3" id="marcheRemise_palier_3" data-default=""/>
                      <span class="input-group-addon">k&euro;</span>
                    </div>
                  </div>
                  <label for="marcheRemise_taux_3" class="control-label col-md-2">Remise taux 3 :</label>
                  <div class="col-md-2">
                    <div class="input-group">
                      <input type="number" lang="fr" min="0" step="0.01" max="99" class="form-control" name="remise_taux_3" id="marcheRemise_taux_3" data-default=""/>
                      <span class="input-group-addon">&percnt;</span>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <label for="marcheRemise_palier_4" class="control-label col-md-2">Remise palier 4 :</label>
                  <div class="col-md-2">
                    <div class="input-group">
                      <input type="number" lang="fr" min="0" step="1" max="9999" class="form-control" name="remise_palier_4" id="marcheRemise_palier_4" data-default=""/>
                      <span class="input-group-addon">k&euro;</span>
                    </div>
                  </div>
                  <label for="marcheRemise_taux_4" class="control-label col-md-2">Remise taux 4 :</label>
                  <div class="col-md-2">
                    <div class="input-group">
                      <input type="number" lang="fr" min="0" step="0.01" max="99" class="form-control" name="remise_taux_4" id="marcheRemise_taux_4" data-default=""/>
                      <span class="input-group-addon">&percnt;</span>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <label for="marcheCommentaire" class="control-label col-md-2">Commentaire :</label>
                  <div class="col-md-8">
                    <textarea class="form-control" rows="3" name="commentaire" id="marcheCommentaire"></textarea>
                  </div>
                </div>
                <div class="form-group">
                  <label for="marcheActive" class="control-label col-md-2">Marché activé :</label>
                  <div class="col-md-2">
                    <div class="checkbox"><input type="checkbox" name="active" id="marcheActive"/></div>
                  </div>
                  <label for="marcheChk_cache_commerciaux" class="control-label col-md-2">Marché caché aux commerciaux :</label>
                  <div class="col-md-2">
                    <div class="checkbox"><input type="checkbox" name="chk_cache_commerciaux" id="marcheChk_cache_commerciaux"/></div>
                  </div>
                  <label for="marcheChk_marche_hybride" class="control-label col-md-2">Marché hybride :</label>
                  <div class="col-md-2">
                    <div class="checkbox"><input type="checkbox" class="checkbox" name="chk_marche_hybride" id="marcheChk_marche_hybride"/></div>
                  </div>
                </div>
                <div class="form-group">
                  <label for="marcheChk_regle_seuils_marge" class="control-label col-md-2">Règle des seuils de marge :</label>
                  <div class="col-md-2">
                    <div class="checkbox"><input type="checkbox" class="checkbox" name="chk_regle_seuils_marge" id="marcheChk_regle_seuils_marge"/></div>
                    <p class="help-block">Prise en compte des seuils de marge RR et DV enregistrés dans les familles de produits</p>
                  </div>
                </div>
              </form>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <div class="alert col-md-4" id="frmMsgErrorModMarche"></div>
          <button type="button" class="btn btn-default" id="btnCancelModMarche" title="Annuler la modification">Annuler</button>
          <button type="button" class="btn btn-success" id="btnSubmitModMarche">Enregistrer</button>
        </div>
      </div>
    </div>
  </div>

  <div class="modal draggable fade" id="modalImportMarche" tabindex="-1" role="dialog" aria-labelledby="modImportTitre" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="modImportTitre"></h4>
        </div>
        <div class="modal-body">
          <div class="alert-danger">Le fichier doit contenir tous les produits d'un marché avec les toutes ses lignes de vente et d'achat.<br/>Un fichier complet, pas partiel.</div>
          <form class="form-horizontal" enctype="multipart/form-data" method="post" id='frmImportMarche'>
            <input type="hidden" id="importIdMarche" name="importIdMarche" value="0"/>
            <input type="hidden" name="MAX_FILE_SIZE" id="MAX_FILE_SIZE" value="10297150"/>
            <div class="form-group">
              <label for="importFileMarche" class="col-sm-2 control-label">Fichier :</label>
              <div class="col-sm-10">
                <input type="file" id="importFileMarche" name="importFileMarche" class="btn btn-default"/>
                <p class="help-block">Le fichier doit être au format CSV avec des séparateurs ;<br/>
                  avec 7 colonnes suivantes par produit :<br/>
                  Item No.;ItemName;Item Group;ListName;List Price;Discount in %;Quantity
                </p>
              </div>
            </div>
            <progress id="progressBar" value="0" max="100" style="width:300px;display:none"></progress>
            <p id="loaded_n_total"></p>
          </form>
        </div>
        <div class="modal-footer">
          <span id="importMarcheBarProgress"></span>
          <button type="button" class="btn btn-default" id="btnCancelImportMarche" title="Annuler l'importation">Annuler</button>
          <button type="button" class="btn btn-success" id="btnSubmitImportMarche" title="Lancer l'importation">Importer</button>
        </div>
      </div>
    </div>
  </div>

  <div class="modal draggable fade" id="modalEditListes" tabindex="-1" role="dialog" aria-labelledby="modListesTitre" data-backdrop="static">
    <div class="modal-dialog modal-xl">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="modListesTitre"><i class="fa fa-stack-exchange fa-lg"></i> Gestion des listes tarifaires par marché</h4>
        </div>
        <div class="modal-body p-5">
          <p>Définition des mots clés et termes à rechercher dans les listes tarifaires importées pour enregistrer les produits dans les bons marchés en vente ou en achat</p>
          <form class="form-horizontal" id="frmListes" role="form" enctype="multipart/form-data">
            <input type="hidden" name="idListe" value="1"/>

            <div class="border cm-scrollbar cm-table-w-scroll table-responsive">
              <table class="table table-bordered table-striped table-400" id="tblListes">
                <thead>
                <tr>
                  <th class="header" scope="col" width="10%">#</th>
                  <th class="header" scope="col" width="15%">Marché</th>
                  <th class="header" scope="col" width="20%">Mot clé</th>
                  <th class="header" scope="col" width="20%">Terme Achat</th>
                  <th class="header" scope="col" width="20%">Terme Vente</th>
                </tr>
                </thead>
                <tbody>

                </tbody>
              </table>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" id="btnCancelListes" title="Annuler la modification">Annuler</button>
          <button type="button" class="btn btn-success" id="btnSubmitListes" title="Enregistrer la modification">Enregistrer</button>
        </div>
      </div>
    </div>
  </div>

  <div class="modal draggable fade" id="modalFamilles" tabindex="-1" role="dialog" aria-labelledby="modFamillesTitre" data-backdrop="static">
    <div class="modal-dialog modal-xl">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="modFamillesTitre"><i class="fa fa-stack-exchange fa-lg"></i> Gestion des familles de produits</h4>
        </div>
        <div class="modal-body p-0">
          <div class="panel panel-primary mb-0">
            <div class="panel-heading ">
              <h3 class="panel-title">
                <span id="pnlFamillesTitre"><i class="fa fa-cubes"></i> Correspondances des groupes et des familles de produits par mots clés</span>
                <button class="btn btn-success btn-xs pull-right" id="btnAddFamille" title="Ajouter une famille"><i class="fa fa-plus fa-lg"></i> Ajouter</button>
              </h3>
            </div>
            <div class="panel-body">
              <form class="form-horizontal" id="frmListes" role="form" enctype="multipart/form-data">
                <input type="hidden" name="idFamilles" value="1"/>
                <div class="cm-scrollbar cm-table-w-scroll">
                  <table id="tblFamilles" class="table-bordered table-400">
                    <thead>
                    <tr>
                      <th class="header" scope="col" width="10%">#</th>
                      <th class="header" scope="col" width="20%">Groupe</th>
                      <th class="header" scope="col" width="20%">Famille</th>
                      <th class="header" scope="col" width="20%">Code Maintenance</th>
                      <th class="header" scope="col" width="10%">Marge RR</th>
                      <th class="header" scope="col" width="10%">Marge DV</th>
                      <th class="header" scope="col" width="10%">Actions</th>
                    </tr>
                    </thead>
                    <tbody>

                    </tbody>
                  </table>
                </div>
              </form>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" id="btnCancelFamilles" title="Annuler la modification">Annuler</button>
          <button type="button" class="btn btn-success" id="btnSubmitFamilles" title="Enregistrer la modification">Enregistrer</button>
        </div>
      </div>
    </div>
  </div>

<?php
$modal = ob_get_contents();
ob_clean();

ob_start();
?>
  <script src="/pub/res/js/jmarches.js"></script>
<?php
$jscript = ob_get_contents();
ob_end_clean();

require_once $Route->_layout;
