

{!! $dataTable->scripts() !!}

<script>
    var getSelectedDatas = function(){
        let data = [];

        $('#table-stock-preview-importation tbody tr.selected').each(function (index) {
            //let $input = $(this).find("td input[type='number']");
            let $comment = $(this).find("td textarea");
            let $type_importation = $(this).find("td select.type-importation");
            let values = {
                'id_import': $comment.attr('data-id-import'),
                "id_type_importation": $type_importation.val(),
                "comment": $comment.val()
            };

            data.push(values);
            // formData.append(index,values);
        });
        return data;
    };

    var $btn =  $("#btn-validate-import-datas");
    var id_table_validate_importation = "table-stock-preview-importation";
    var $table_validate = $("#"+id_table_validate_importation);
  //  $btn.attr('disabled',true);

    $(function () {
        $('[data-toggle="tooltip"]').tooltip();

        $table_validate.find('thead th:first-child')
                       .html('<input class="dt-checkbox-all" type="checkbox" onclick="select_all_td()">');

        $table_validate.on( 'click', 'tbody td',function (e) {
            if ($.fn.dataTable.isDataTable('#'+id_table_validate_importation)){
                let table = $("#"+id_table_validate_importation).DataTable();
                let editor = table.editor();
                let $td = $(this);
                let  index = $td.index();
                let rowData = table.row($td).data();
                let msg =  rowData.produit.fullName;

                //console.log(msg,rowData)

                if(index === 70){ // colonne 'commentaire'
                    {{--editor.s.ajax.url  = "{{route('auth_stocks_management_demand_editMessage')}}";--}}
                    editor.bubble( $td,['comment'], {
                        message: msg,
                        buttons: {
                            label: '{{__("OK")}}',
                            className:"btn-warning btn-sm fs-11",
                            fn: function () {
                                this.submit(
                                    function (data) {
                                        if(typeof data === 'object' && !$.isEmptyObject(data)){
                                            if(data.is_success){
                                                refresh_datatable(id_table_validate_importation);
                                            }
                                        }
                                    },
                                );
                            },
                        },
                    } );
                }
            }
        });

        $btn.click(function (e) {

            if ($.fn.dataTable.isDataTable('#'+id_table_validate_importation)){

                //console.log(table.rows({selected:true}).data())
                let dataSelected = getSelectedDatas();
                let count = dataSelected.length;

                let param = {
                    body:dataSelected,
                    method:'post'
                };
                let url = "{{route("auth_stocks_management_importation_validateData")}}";

                if(count > 0){
                    swal.fire({
                        icon: 'question',
                        title: "{{__('Importations')}}",
                        text: "{{__("Confirmer la validation de ")}} "+count+" {{__('ligne(s)')}}",
                        showCancelButton: true,
                        confirmButtonText: '{{__("Oui")}}',
                        cancelButtonText: '{{__("Non")}}',
                    })
                    .then((result) => {
                            if (result.isConfirmed) {
                                fetch_async(url,param)
                                .then( result => {
                                    if(result.is_success){
                                        hideModal();
                                    }else{}
                                    //
                                    let table = $('#table-stock-preview-importation').DataTable();
                                    table.draw();
                                    showNotification(result.notif.type,{'title':result.notif.title,"msg":result.notif.msg});
                                });
                            }
                        });
                }else{
                    showNotification("error",{'title':"{{__("Importation")}}","msg":"{{__("Veuillez cochez au moins 01 ligne dans le tableau.")}}"});
                }
            }
        });

        $table_validate.on('draw.dt',function (e,context) {
            let api = new $.fn.dataTable.Api(context);
            let datas = api.ajax.json().data;
            let btn_state = (datas.length > 0) ? "block":"none";
            $btn.css("display",btn_state);
        })
    })
</script>

<div class="row">
    <div class="col-lg-12 tabel-responsive">
        {{$dataTable->table()}}
    </div>
    <div class="col-lg-12">
        <span class="pull-right">
           <button class="btn btn-success" id="btn-validate-import-datas" style="display: none">
               <span class="fa fa-check-circle"></span>
               {{__("Valider")}}
           </button>
        </span>
    </div>
</div>
