const flashData = $('.flash-data').data('flashdata');

// console.log(flashData);
// if (flashData) {
//   Swal.fire({
//     icon: 'success',
//     title: flashData,
//     text: ''
//   })
// }

if (flashData == 'sukses_login') {
  Swal.fire({
    icon: 'success',
    title: 'Selamat Datang Administrator',
    text: '',
    showConfirmButton: false,
    timer: 1000
  })
} else if (flashData == 'sukses_login_pengguna') {
  Swal.fire({
    icon: 'success',
    title: 'Selamat Datang',
    text: ''
  })
} else if (flashData == 'gagal_login') {
  Swal.fire({
    icon: 'error',
    title: 'Login Error !!!',
    text: 'Username atau Password Salah !'
  })
} else if (flashData == 'logout_sukses') {
  Swal.fire({
    icon: 'info',
    title: 'Login Sukses !!!',
    text: 'Anda Telah Logout Dari Sistem !'
  })
} else if (flashData == '1111') {
  Swal.fire({
    icon: 'success',
    title: 'Simpan Data Sukses',
    text: ''
  })
} else if (flashData == '2222') {
  Swal.fire({
    icon: 'success',
    title: 'Update Data Sukses',
    text: ''
  })
} else if (flashData == '3333') {
  Swal.fire({
    icon: 'success',
    title: 'Hapus Data Sukses',
    text: ''
  })
} else if (flashData == '3131') {
  Swal.fire({
    icon: 'success',
    title: 'Data Backup Telah Dihapus',
    text: ''
  })
} else if (flashData == '4444') {
  Swal.fire({
    icon: 'warning',
    title: 'Data Tidak Ditemukan',
    text: '',
    showConfirmButton: false,
    timer: 1500
  })
} else if (flashData == '5555') {
  Swal.fire({
    icon: 'warning',
    title: 'Login Error !!!',
    text: 'Password dan Ulangi Password yang Anda masukan tidak sama'
  })
} else if (flashData == '6666') {
  Swal.fire({
    icon: 'warning',
    title: 'Upload Data Gagal !!!',
    text: 'File Yang Anda Masukkan Terlalu Besar'
  })
} else if (flashData == '7373') {
  Swal.fire({
    icon: 'warning',
    title: 'Upload Data Gagal !!!',
    text: ''
  })
} else if (flashData == '7575') {
  Swal.fire({
    icon: 'success',
    title: 'Import Sukses',
    text: 'Data Berhasil Diimport'
  })
} else if (flashData == '9393') {
  Swal.fire({
    icon: 'error',
    title: 'Login Error !!!',
    text: 'Username atau Password Salah !'
  })
} else if (flashData == '9191') {
  Swal.fire({
    icon: 'success',
    title: 'Logout Sukses',
    text: 'Anda Telah Logout Dari Sistem !'
  })
} else if (flashData) {
  Swal.fire({
    icon: 'info',
    title: flashData,
    text: '',
    showConfirmButton: false,
    timer: 1000
  })
}

$('.tombol-hapus').on('click', function (e) {
  e.preventDefault();
  const href = $(this).attr('href');

  Swal.fire({
    title: 'Apakah Anda Yakin ?',
    text: "Data Akan Terhapus Secara Permanen",
    icon: 'question',
    showCancelButton: true,
    confirmButtonColor: '#d33',
    cancelButtonColor: '#3085d6',
    cancelButtonText: 'Batal',
    confirmButtonText: 'Hapus!'
  }).then((result) => {
    if (result.value) {
      document.location.href = href;
      // window.history.back();
    } else if (result.dismiss === Swal.DismissReason.cancel) {
      Swal.fire({
        icon: 'error',
        title: 'Data Batal Dihapus',
        showConfirmButton: false,
        timer: 1000
      })
    }
  })
});


$('.tombol-logout').on('click', function (e) {
  e.preventDefault();
  const href = $(this).attr('href');

  Swal.fire({
    title: 'Apakah Anda Ingin Logout ?',
    icon: 'question',
    showCancelButton: true,
    confirmButtonColor: '#d33',
    cancelButtonColor: '#28A745',
    cancelButtonText: 'Batal',
    confirmButtonText: 'Logout!'
  }).then((result) => {
    if (result.value) {
      let timerInterval
      Swal.fire({
        title: 'Logout Proses!',
        html: 'Memproses Logout Dalam <b></b> milidetik.',
        timer: 1000,
        timerProgressBar: true,
        onBeforeOpen: () => {
          Swal.showLoading()
          timerInterval = setInterval(() => {
            const content = Swal.getContent()
            if (content) {
              const b = content.querySelector('b')
              if (b) {
                b.textContent = Swal.getTimerLeft()
              }
            }
          }, 100)
        },
        onClose: () => {
          clearInterval(timerInterval)
        }
      }).then((result) => {
        /* Read more about handling dismissals below */
        if (result.dismiss === Swal.DismissReason.timer) {
          // console.log('I was closed by the timer')
        }
      })
      document.location.href = href;

    } else if (result.dismiss === Swal.DismissReason.cancel) {
      Swal.fire({
        icon: 'error',
        title: 'Logout Dibatalkan',
        showConfirmButton: false,
        timer: 1000
      })
    }
  })
});
