
<?php echo $with_navbar=false; ?>

<?php $__env->startSection('title'); ?> Transaction <?php $__env->stopSection(); ?>
<?php $__env->startSection('title_card',""); ?>

<?php $__env->startSection('styles'); ?>
    .td_debit {
        text-align:right;
    }
    .td_credit {
        text-align:right;
    }
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>

    <?php echo e(html()->form('POST', url('/grand_livre'))->attributes(["id"=>"form_create"])->open()); ?> 
    <input type="hidden" name="all_data" id="all_data" value="">
    <input type="hidden" name="num_ligne_max" id="num_ligne_max" value="">
    <div class="card">
            <?php if($errors): ?>
                <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <div class='bg-danger alert'><?php echo $error; ?></div>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            <?php endif; ?>
        <div class="card-header bg-info">
            <h3>Nouvelle Transaction</h3>
        </div>
        <div class="card-body">
            <div class="form-group row callout callout-info">
                <div class='col-md-3'>
                    <label for="date_doc" class="control-label">Date Pièce<span style="color:#dc3545">*</span></label>
                    <div class='input-group date' id='datepicker_date_doc'>
                        <?php echo html()->text('date_doc', date('d/m/Y'))->attributes(['id'=>'date_doc','placeholder' => 'JJ/MM/AAAA', 'class' => 'form-control']); ?>

                        <span class='input-group-addon'>
                            <span class='glyphicon glyphicon-calendar'></span>
                        </span>
                    </div>
                </div>

                <div class='col-md-4'>
                    <label for="journal" class="control-label">Journal<span style="color:#dc3545">*</span></label>
                    <?php echo html()->select('journal',$liste_journaux,$journal)->placeholder('A définir')->attributes(['class' => 'form-control','width' => '100%']); ?>

                </div>

                <div class='col-md-5'>
                    <label for="ref_doc" class="control-label">Référence/Pièce<span style="color:#dc3545">*</span></label>
                    <?php echo html()->text('ref_doc', "")->attributes(['class' => 'form-control']); ?>

                </div>
                <span id="Inputref_doc-error" class="error " style="color:#dc3545">* : Obligatoire</span>
            </div> 

            <div class="form-group row ">
            <?php echo $__env->make("comptabilite.grandlivre.content_new", \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?> 
            </div> 

            <div class='form-group row'>
                <div class="col-md-12">
                    <?php echo html()->button('Enregistrer')->value('Enregistrer')->type('submit')->attributes(['id'=>'btn_enregistrer','class' => 'btn btn-primary']); ?>

                    <a class="btn btn-danger"   href="<?php echo url('grand_livre'); ?>">Annuler</a>
                </div>
            </div> 
        </div>
    </div>
    <?php echo e(html()->form()->close()); ?>

<?php $__env->stopSection(); ?>


<?php $__env->startSection('scripts'); ?>
    <script type="text/javascript">
        $(function () {
            $('#datepicker_date_doc').datepicker({
            format: "dd/mm/yyyy",language: "fr",autoclose: true
            });

            var data_comptes=<?php echo $comptes_json; ?>;
            var data_comptes_aux=<?php echo $comptes_aux_json; ?>;
            var chrono_ligne=0;

            CalculNbLignes();
            
            $('#sel_compte').select2({
                width:'100%',
                data: data_comptes,
                placeholder:"Rechercher"   
            });

            $(document).on("select2:open", () => {
              document.querySelector(".select2-container--open .select2-search__field").focus()
            })


            $('#sel_compte_aux').select2({
                width:'100%',
                data: data_comptes_aux,
                placeholder:"Rechercher"
            });

            $('#bt_ajouter').click(function() {
                //Test si compte choisi
                let compte=$('#sel_compte').val();let compte_text=$('#sel_compte option:selected').text();
                let compte_aux=$('#sel_compte_aux').val();let compte_aux_text=$('#sel_compte_aux option:selected').text();
                if(compte_aux=="") compte_aux_text="";
                let date_ref=$('#date_ref').val();
                let libelle=$('#libelle').val();libelle=libelle.replace(';','-');
                if(compte=="") {alert('Vous devez choisir un compte');return false;}
                let debit=$('#debit').val();debit=debit.replace(/\s/g,'');
                let credit=$('#credit').val();credit=credit.replace(/\s/g,'');
                if(!isNumeric(debit)) $('#debit').val(0);
                if(!isNumeric(credit)) $('#credit').val(0);
                $('#debit').val(debit);
                $('#credit').val(credit);
                if((debit==0)&&(credit==0)) {alert('Vous devez Saisir un crédit ou un débit supérieurs à zéro');return false;}
                if((debit!=0)&&(credit!=0)) {alert('Vous devez Saisir un crédit ou un débit, pas les deux');return false;}
                //ajout de la ligne
                let td_val=compte+";"+compte_aux+";"+date_ref+";"+libelle+";"+debit+";"+credit;
                chrono_ligne++;
                markup ="<tr id='tr_"+chrono_ligne+"' class='tr_'>";
                markup +="<td align=''><input id='compte_"+chrono_ligne+"' type='text' name='compte_"+chrono_ligne+"' class='td_compte form-control' disabled value='"+compte_text+"'/></td>";
                markup +="<td align=''><input id='compte_aux_"+chrono_ligne+"' type='text' name='compte_aux_"+chrono_ligne+"' class='td_compte_aux form-control' disabled value='"+compte_aux_text+"'/></td>";
                markup +="<td align=''><input id='date_ref_"+chrono_ligne+"' type='date' name='date_ref_"+chrono_ligne+"' class='td_date_ref form-control' type='date' disabled value='"+date_ref+"'/></td>";
                markup +="<td align=''><input id='libelle_aux_"+chrono_ligne+"' type='text' name='libelle_aux_"+chrono_ligne+"' class='td_libelle form-control' disabled value='"+libelle+"'/></td>";
                markup +="<td align='right'><input id='debit_"+chrono_ligne+"' type='text' name='debit_"+chrono_ligne+"' class='td_debit form-control' disabled value='"+debit+"'/></td>";
                markup +="<td align='right'><input id='credit_"+chrono_ligne+"' type='text' name='credit_"+chrono_ligne+"' class='td_credit form-control' disabled value='"+credit+"'/></td>";
                 markup +="<td nowrap align='center' >";
                //markup +="<a href='#' id='btn_edit_"+chrono_ligne+"' class='btn_edit btn btn-sm bg-info'/><i class='fas fa-pencil'></i></a>&nbsp;&nbsp;";
                markup +="<a href='#' id='btn_delete_"+chrono_ligne+"' class='btn_delete btn btn-sm bg-danger'/><i class='fas fa-trash'></i></a>";
                markup +="</td>";
                markup +="<input id='lg_"+chrono_ligne+"' type='hidden' class='td_data' value='"+td_val+"'/>";
                markup +="</tr>";
                $("#table_e > tbody").append(markup);
                $("#num_ligne_max").val(chrono_ligne);
                $('#sel_compte').val('').trigger('change');
                $('#sel_compte_aux').val('').trigger('change');

                CalculNbLignes();

                return true;

            });
            $('#debit').on('keypress', function() {return isNumberKey(event);});
            $('#credit').on('keypress', function() {return isNumberKey(event);});
            $("form").submit(function(){
                //Test les saisies
                var date_doc=$('#date_doc').val();
                var journal=$('#journal').val();
                var ref_doc=$('#ref_doc').val();
                var msg="";
                if(date_doc=="") msg+=((msg!="")?"\n":"")+"Vous devez saisir une date";
                if(journal=="") msg+=((msg!="")?"\n":"")+"Vous devez choisir un Journal";
                if(ref_doc=="") msg+=((msg!="")?"\n":"")+"Vous devez saisir une référence";
                if(msg!="") {alert(msg);return false;}
                $('#form_create').submit();
            });


            $(document.body).on('click', '.btn_delete' ,function(){                    
                if(!confirm("Confirmez-vous la suppression de la ligne ?")) return false;
                let id=$(this).attr('id');
                id=id.replace('btn_delete_','');
                $("#tr_"+id).remove();
                CalculNbLignes();
            });



            function CalculNbLignes() {
                var total=0;nb=0;
                $('#table_e tbody > tr').each(function() {
                    nb++;
                    var debit= $(this).find(".td_debit").val();
                    var credit= $(this).find(".td_credit").val();
                    if(!isNumeric(debit)) debit=0;
                    if(!isNumeric(credit)) credit=0;
                    total+=credit-debit;
                });
                if(nb==0) {chrono_ligne=0;$("#num_ligne_max").val(chrono_ligne);}
                $("#TOTAL").val(total);
                var total=$("#TOTAL").val();
                $("#TOTAL").css('color','grey');
                if(total<0) $("#TOTAL").css('color','red');
                if(total>0) $("#TOTAL").css('color','darkgreen');

                if((total==0)&&(nb>0)) {
                    $('#sel_compte').val("");
                    $('#sel_compte_aux').val("");
                    $('#libelle').val("");
                    $('#debit').val(0);
                    $('#credit').val(0); 
                    $('#btn_enregistrer').prop('disabled',false);
                } else {
                    if(total<0) {$('#debit').val(0);$('#credit').val(-total);}
                    if(total>0) {$('#debit').val(total);$('#credit').val(0);}
                    $('#btn_enregistrer').prop('disabled',true);
                }
                setLigne2Input();
            }

            function setLigne2Input() {
                var lg_data="";
                $('#table_e tbody > tr').each(function() {
                    let tdata=$(this).find(".td_data").val();
                    if(tdata!=undefined) { 
                        lg_data+=((lg_data!="")?"|":"")+tdata;
                    }
                });
                $('#all_data').val(lg_data);
            }

            function isNumeric(num){
              return !isNaN(num)
            }

            function isNumberKey(evt)
            {
                var charCode = (evt.which) ? evt.which : evt.keyCode;
                if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) return false;
                return true;
            }
            function isNumericKey(evt)
            {
                var charCode = (evt.which) ? evt.which : evt.keyCode;
                if (charCode != 46 && charCode > 31  && (charCode < 48 || charCode > 57)) return true;
                return false;
            } 
        })
    </script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.dash', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH C:\xampp\htdocs\SYSINFO\cpswf\sipre2\resources\views/comptabilite/grandlivre/create.blade.php ENDPATH**/ ?>