import { simpleNotification } from '../../z_comps/notify.js';


function delete_file(e) {
    let btn = $(this);
    let postId = btn.data('id');
    let url = `${window.location.origin}/user/carousel/delete-slide/${postId}`;

    fetch(url, {
      method: 'DELETE',
      headers: {
        'Authorization': 'Bearer ' + localStorage.getItem('token_ai') // o tu token JWT
      }
    })
      .then(res => res.json())
      .then(data => {
        if (data.msg) {
          console.log(data.msg);
          let btn = document.querySelector(`[data-id="${postId}"]`);
          let row = $(btn).closest('tr'); // usa jQuery para compatibilidad con DataTables
          let table = $('#tblCarousel').DataTable(); // obtiene instancia

          table.row(row).remove().draw(); // elimina la fila y redibuja la tabla

          simpleNotification("Eliminación exitosa", `${data.msg}`, "success");

          // eliminar el tr del slide si quieres: btn.closest('tr').remove();
        } else {
          console.error('Error:', data.error);
        }
      })
      .catch(err => console.error('Error en fetch:', err));
  }




      // https://htmlguyllc.github.io/jConfirm/
      $(function(){
        $('.delete-btn').jConfirm({
            //false|array: if provided, this will override the default confirm/deny buttons (see below for an example)
            btns: false,
            //string: question displayed to the user
            question: '¿Deseas continuar con la eliminación?',
            //string: confirm button text
            confirm_text: 'Sí',
            //string: deny button text
            deny_text: 'No',
            //boolean: if true, when the confirm button is clicked the user will be redirected to the button's href location
            // follow_href: true,
            //boolean: if true and follow_href is true, the href will be opened in a new window
            open_new_tab: false,
            //boolean: if true, the tooltip will be hidden if you click outside of it
            hide_on_click: true,
            //string ('auto','top','bottom','left','right'): preferred location of the tooltip (defaults to auto if no space)
            position: 'auto',
            //boolean: if true, the deny button will be shown
            show_deny_btn: true,
            //string ('black', 'white', 'bootstrap-4', 'bootstrap-4-white')
            theme: 'bootstrap-4',
            //string ('tiny', 'small', 'medium', 'large')
            size: 'medium',
            //boolean: show the tooltip immediately on instantiation
            show_now: false,
            //string: class(es) to add to the tooltip
            'class': ''
        }).on('confirm', delete_file)
        .on('deny', function(e){
            var btn = $(this);
            //do something on deny
          }).on('jc-show', function(e, tooltip){
            // console.log("el tooltip es visible");
            //do something when tooltip is shown
            //tooltip dom element is passed as the second parameter

        }).on('jc-hide', function(e){
            //do something when tooltip is hidden
        });

    });