 <?php include_once PUBLIC_PATH. '/files_to_include/header_content.php'; ?>
<!-- =============================================== -->

  <!-- Left side column. contains the sidebar -->
  <aside class="main-sidebar" id="aside_menu" >
      <section class="sidebar">
          <ul class="sidebar-menu">
            <?php include_once PUBLIC_PATH. '/files_to_include/menu.php'; ?>
          </ul>
      </section>
  </aside>
   <!-- Content Wrapper. Contains page content -->
   <div class="content-wrapper " style="background-color: white; td{border: solid  2px #000; }">
   

    <!-- Main content -->
    <section class="content">
          
           <div class="row">
                <div class="col-lg-12">
                   
                    <h3 class="page-header"><i class="fa fa-plus" style="color:blue;"></i> Création produit</h3>
                    <a  href='<?php echo $this->url(array('controller'=>'Produit','action'=>'index'),null, true);?>'><span class="glyphicon glyphicon-arrow-left"></span> Retour à la liste</a>
                    <div class="clearfix"></div>
                    <br>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <form id="create" enctype="multipart/form-data">
                       <div class="col-lg-6">
                            <div class="form-group">
                                <label>nom</label><label style="color:blue;">&nbsp; * &nbsp;</label>
                                <input type="text" id="nom" placeholder="nom du  produit" class="form-control" required=""/>
                            </div>
                            
<!--                            <div class="form-group">
                                <label>Prix personne</label><label style="color : blue;">&nbsp; * &nbsp;</label>
                                <input type="number" id="prixpersonne" placeholder="Prix du produit par personne" class="form-control prix" required=""/>
                            </div>
                            <div class="form-group">
                                <label>Prix groupe</label><label style="color : blue;">&nbsp; * &nbsp;</label>
                                <input type="number" id="prixgroupe" placeholder="Prix du  produit pour groupe" class="form-control prix" required=""/>
                            </div>-->
                            <div class="form-group">
                               <label>Categorie du service</label><label style="color:blue;">&nbsp; *&nbsp;</label>
                                    <select class='form-control selectpicker'  data-live-search="true" id="categorie">
                                        <?php
                                          foreach ( $this->liste as $categorie ){
                                              ?>
                                        <option value="<?= $categorie->getIdcategorie();?>"><?php echo $categorie->getNomcategorie();?></option>
                                       <?php   }
                                        ?>
                                    </select>
                            </div>
                    <div class="form-group">
                                <label>Unité</label><label style="color:blue;">&nbsp; * &nbsp;</label>
                                <input categorie="text" id="unite" placeholder="Désignation de l'unité" class="form-control" required=""/>
                            </div>
                            <div class="form-group">
                                <label>Image</label>
                                <input class="imageM" type="file" id="image" name="image" />
                                <div>
                                    <h5>Visualisation : <img src="#" id="preview" style="height: 35px; width: 35px;" />
                                </div>
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn btn-microsoft" id="submitit">Enregistrer</button>
                                <button type="reset" class="btn btn-microsoft" id="submitit">Annuler</button>
                            </div>
                       </div>
                        <div class="col-lg-6">
                            
                            <a href="#" id="ajoutLigneClient" class=" col-lg-12"><i  class="fa fa-plus-circle fa-3x"></i></a>
                            <div id="partieClient">
                            </div>    
                               
                                
                        </div>
                       
                
                    </form>
                      </div>   
                 </div>
                   
                
            </div>
     

    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->
 <?php include_once PUBLIC_PATH. '/files_to_include/footer_content.php'; ?>
  
   	 <script src="<?php echo $this->baseUrl('script/jquery.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl('script/bootstrap.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl('script/app.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl('script/i18next-1.7.1.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl('script/metisMenu.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl('script/sb-admin-2.js');?>"></script>
    <script src="<?php echo $this->baseUrl("script/pdfmake.min.js");?>"></script>
    <script src="<?php echo $this->baseUrl("script/vfs_fonts.js");?>"></script>
    <script src="<?php echo $this->baseUrl("script/jquery.toast.min.js");?>"></script>
    <script src="<?php echo $this->baseUrl('script/loadingoverlay.min.js');?>"></script>
    <script src="<?php echo $this->baseUrl("script/jquery.datetimepicker.full.js");?>"></script>
    <script src="<?php echo $this->baseUrl("script/bootstrap.select.min.js");?>"></script>
    <script src="<?php echo $this->baseUrl("script/moment.js");?>"></script>
    <!-- FastClick -->
<script src="<?php echo $this->baseUrl('script/fastclick.js');?>"></script>
<!-- AdminLTE App -->
<script src="<?php echo $this->baseUrl('script/adminlte.min.js');?>"></script>
<script src="<?php echo $this->baseUrl('script/demo.js');?>"></script>
   <script type="text/javascript">
        
        $(document).ajaxSend(function(event, jqxhr, settings){
            $.LoadingOverlay("show");
        });
        
        $(document).ajaxComplete(function(event, jqxhr, settings){
            $.LoadingOverlay("hide");
        });
     
      
      function readURL(input) {

               if (input.files && input.files[0]) {
                   var reader = new FileReader();

                   reader.onload = function(e) {
                       $('#preview').attr('src', e.target.result);
                   };

                   reader.readAsDataURL(input.files[0]);
               }
        }

        $("#image").change(function(){
           readURL(this);
        });
      
      $("#ajoutLigneClient").click(function(){
        var html = "<div class='col-lg-12'>"+
                "<div id='client-minus-purpose' class='col-lg-1 col-md-2 col-xs-2'>"+
                        "<a href='#' ><i  class='supprimerLigne fa fa-minus-circle fa-3x'></i></a>"+
               " </div>"+
                         " <div class='col-lg-11'>"+
                              "<input type='text' placeholder='Nouveau prix' class='form-control prix' />"+
                           "</div>"+      
                             "</div>";
                                          
                                      
        $("#partieClient").append(html);
    });
    $("body").on("click", ".supprimerLigne", function (e) {
        e.preventDefault();
       $(this).parent().parent().parent().remove();
    });
    
      
     
      
      $("#create").on("submit",function(e){
         e.preventDefault();
         
         var $allNumber = $("input[type='number']");
         for( var k = 0; k < $allNumber.length ; k++){
             if ( parseInt($allNumber.eq(k).val()) <=  0  && !$allNumber.eq(k).hasClass("prix") ){
                 $.toast({
                     heading : "Incohérence",
                     text : "Tous les nombres sur cette page doivent être positifs",
                     hideAfter : false,
                     stack : false,
                     icon : "error",
                     position : 'mid-center'
                 });
                 return false;
             }else{
                 if ( $allNumber.eq(k).hasClass("prix") && parseInt($allNumber.eq(k).val()) <  0 ){
                     $.toast({
                        heading : "Incohérence",
                        text : "Le prix d'une entrée ne peut être négatif",
                        hideAfter : false,
                        stack : false,
                        icon : "error",
                        position : 'mid-center'
                     });
                     return false;
                 }
                 
             
             }
         }
         
          var $listeclient = [];
          var object = {value:""};
                     $('.prix').each(function(){
                         object = {value:""};
                         object.value = $(this).val();
                         
                            $listeclient.push(object);
                    });
          
         var form = $('#create')[0];
         var formData = new FormData(form);
         formData.append("nom" , $("#nom").val());
         formData.append("categorie" , parseInt($("#categorie").val()));
         formData.append("unite" , $("#unite").val());
         formData.append("listePrix",JSON.stringify($listeclient));

         
 
         
         $.ajax({
             url : "<?php echo $this->url(array('controller' => 'Produit', 'action' => 'enregistrer')); ?>",
             data : formData , 
             dataType : "json",
             type : "post",
             contentType: false,
             processData: false,
             error : function(){
                $.toast({
                     heading : "serveur inactif",
                     text : "MVOLYEMANAGER n'arrive pas à accéder au serveur , si cela persiste veuilez contacter le groupe MELLCO",
                     icon : "error",
                     hideAfter : false,
                     stack : false,
                     position : 'mid-center'
                });
             },
             success : function(data){
                 var top = $("#submitit").offset().top;
                 var left = $("#submitit").offset().left;
                 if ( data.type === "success"){
                     $.toast({
                        heading : "succès",
                        text : "Entree a été enrégistré avec succès",
                        icon : "success",
                        hideAfter : false,
                        stack : false,
                        position : 'mid-center'
                     });
                     
                     $("#create").trigger("reset");
                    
                 }else{
                     $.toast({
                         heading : "erreur",
                         text : data.message,
                         icon : "error",
                         hideAfter : false,
                         stack : false,
                         position : 'mid-center'
                     });
                 }
             }
         });
         
      });
      
    </script>

</body>

</html>