<?php
// TODO: Gérer l'importation du fichier des clients SAP dans cette page directement
// TODO: Finaliser la gestion du click de ligne du tableau des clients et des boutons de consultation sur lignes dynamiques

global $Route;
global $aModel;
$nbClients = count($aModel["clients"]);
$metacss = "";
$barre = "";
ob_start();
?>
  <div class="row">
  <div class="col-md-12">
    <div class="panel panel-primary">
      <div class="panel-heading ">
        <h3 class="panel-title">
          <span id="pnlClientsTitre"><i class="fa fa-handshake-o"></i> Liste des <?= $nbClients; ?> clients</span>
          <button class="btn btn-info btn-xs pull-right" id="btnImport" title="Importer une mise à jour des clients"><i class="fa fa-sign-in fa-lg"></i> Importer</button>
          <div class="input-group pull-right panel-search">
            <input type="text" class="form-control form-control-xs" id="schClients" name="schClients" placeholder="nom, adresse, ville..." value=""/>
            <span class="input-group-btn">
              <button type="button" class="btn btn-default" id="btnSearch"><i class="fa fa-search fa-xs"></i></button>
            </span>
          </div>
        </h3>
      </div>
      <div class="panel-body">
        <div class="cm-scrollbar cm-table-w-scroll table-800">
          <table id="tblClients" class="table table-bordered table-responsive table-fixed ">
            <thead>
            <tr>
              <th class="header" scope="col" width="30%">Etablissement</th>
              <th class="header text-center" scope="col" width="10%">Type</th>
              <th class="header text-center" scope="col" width="10%">CP</th>
              <th class="header" scope="col" width="20%">Ville</th>
              <th class="header" scope="col" width="20%">Adresse</th>
              <th class="header text-center" scope="col" width="10%">Actions</th>
            </tr>
            </thead>
            <tbody id="tblClientsBody">
            <?php
            foreach ($aModel["clients"] as $client) {
              echo '<tr>';
              echo '<td class="clickable celClient" data-rowid="' . $client["rowid"] . '">' . $client["libelle"] . '</td>';
              echo '<td class="clickable celClient text-center" data-rowid="' . $client["rowid"] . '">' . $client["type_client"] . '</td>';
              echo '<td class="clickable celClient text-center" data-rowid="' . $client["rowid"] . '">' . $client["cp"] . '</td>';
              echo '<td class="clickable celClient" data-rowid="' . $client["rowid"] . '">' . $client["ville"] . '</td>';
              echo '<td class="clickable celClient" data-rowid="' . $client["rowid"] . '">' . $client["adresse1"] . '</td>';
              echo '<td class="text-center">';
              echo '<button class="btn btn-xs btn-primary" data-rowid="' . $client["rowid"] . '" title="Consulter"><i class="fa fa-eye"></i></button>';
              echo '</td>';
              echo '</tr>';
            }
            ?>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
  <?php
  $content = ob_get_contents();
  ob_clean();

  ob_start();
  ?>
  <div class="modal draggable fade" id="modalModClient" tabindex="-1" role="dialog" aria-labelledby="modalModClientTitre" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog modal-xl">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="modalModClientTitre">Fiche Client</h4>
        </div>
        <div class="modal-body">
          <form class="form-horizontal" enctype="multipart/form-data" method="post" id='frmModClient'>
            <input type="hidden" id="rowid" name="rowid">
            <div class="form-group">
              <label for="libelle" class="col-sm-1 control-label">Etablissement :</label>
              <div class="col-sm-4">
                <input type="text" class="form-control" id="libelle" name="libelle">
              </div>
              <label for="groupe" class="col-sm-1 control-label">Type :</label>
              <div class="col-sm-2">
                <input type="text" class="form-control" id="type_client" name="type_client">
              </div>
            </div>
            <div class="form-group">
              <label for="libelle" class="col-sm-1 control-label">Adresse :</label>
              <div class="col-sm-4">
                <input type="text" class="form-control" id="adresse1" name="adresse1">
              </div>
              <label for="groupe" class="col-sm-1 control-label">CP :</label>
              <div class="col-sm-1">
                <input type="text" class="form-control" id="cp" name="cp">
              </div>
              <label for="ville" class="col-sm-1 control-label">Ville :</label>
              <div class="col-sm-2">
                <input type="text" class="form-control" id="ville" name="ville">
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" id="btnQuitModClient" title="Quitter la fiche client">Quitter</button>
        </div>
      </div>
    </div>
  </div>

  <div class="modal draggable fade" id="modalImport" tabindex="-1" role="dialog" aria-labelledby="modalImportTitre" aria-hidden="true" data-backdrop="static">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="modalImportTitre">Importation d'une mise à jour des clients SAP</h4>
        </div>
        <div class="modal-body">
          <form class="form-horizontal" enctype="multipart/form-data" method="post" id='frmImport'>
            <input type="hidden" name="importListe" id="importListe" value="0"/>
            <input type="hidden" name="MAX_FILE_SIZE" id="MAX_FILE_SIZE" value="10297150"/>
            <div class="form-group">
              <label for="importFile" class="col-sm-2 control-label">Fichier :</label>
              <div class="col-sm-10">
                <input type="file" name="importFile" id="importFile" class="btn btn-default"/>
                <p class="help-block"><strong>Le fichier doit être au format CSV avec des séparateurs ;</strong><br/>
                  avec 16 colonnes suivantes par client :<br/>
                  code; libelle; siret; adresse1; adresse2; adresse3; cp; ville; fk_type; contact_civilite; contact_nom; contact_prenom; contact_fonction; telephone; mobile; email
                </p>
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" id="btnCancelImport" title="Annuler l'importation">Annuler</button>
          <button type="button" class="btn btn-success" id="btnSubmitImport" title="Lancer l'importation">Importer</button>
        </div>
      </div>
    </div>
  </div>

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

ob_start();
echo '<script src="/pub/res/js/jclients.js"></script>';
$jscript = ob_get_contents();
ob_end_clean();
require_once $Route->_layout;
