const origin = document.location.origin;
const siplaburl = document.location.pathname.split('/')[1];
const base_url = origin+"/"+siplaburl;

function isNumberKey(evt) {
  var charCode = (evt.which) ? evt.which: event.keyCode
  if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;
  return true;
}

function filename() {
  var filename = $('#imageinput').val().split("\\");
  var image = filename.slice(-1)[0];
  $('#imagename').text(image);
}

function ubahLogo() {
  var toslice = $('#ubahlogo').val().split('\\')
  var newlogo = toslice.slice(-1)[0];

  $('#newlogoname').text(newlogo);

}

function ubahIcon() {
  var toslice = $('#ubahicon').val().split('\\')
  var newicon = toslice.slice(-1)[0];

  $('#newiconname').text(newicon);

}

function getnamaruangan(evt) {
  var toslice = $('#ubahgambaruangan').val().split('\\');
  var newgambarruangan = toslice.slice(-1)[0];

  $('#gambarruanganbaru').text(newgambarruangan);
}

function cekkoderuangan(event) {
  var kode_ruangan = $('#kode_ruangan').val();
  var id_ruangan = $('#id_ruangan').val();

  $.ajax({
    url: base_url + '/ajax/ajaxcekkoderuangan',
    type: 'POST',
    data: {
      'kode_ruangan': kode_ruangan, 'id_ruangan': id_ruangan
    },
    success: function (data) {
      if (data != "") {
        $('#alertkoderuangan').text(data);
        $('#btnubahruangan').attr('disabled', true);
      } else {
        $('#btnubahruangan').removeAttr('disabled');
      }
    }
  })
}

function ceknip() {
  var nip = $('#nip').val();

  $.ajax({
    url: base_url + '/ajax/ceknip',
    type: 'POST',
    data: 'nip=' + nip,
    success: function (data) {
      if (data != "") {
        $('#alertdaftar').text(data);
        $('#alertdaftar').removeAttr('style');
        $('#daftar').attr('disabled', true);
      } else {
        $('#alertdaftar').text(data);
        $('#alertdaftar').attr('style', 'display: none');
        $('#daftar').removeAttr('disabled');
      }
    }
  })
}

$(document).ready(function () {
  // PUBLIK
  // CARI HELP
  $('#carihelp').keyup(function () {
    var keyword = $('#carihelp').val();

    $.ajax({
      url: base_url + '/ajax/carihelp',
      type: 'POST',
      data: 'keyword=' + keyword,
      success: function (data) {
        $('.tabelhelppublik').html(data);
      }
    })
  })



  // ADMIN HELP
  $('#carihelpadmin').keyup(function () {
    var keyword = $('#carihelpadmin').val();

    $.ajax({
      url: base_url + '/ajax/carihelpadmin',
      type: 'POST',
      data: 'keyword=' + keyword,
      success: function (data) {
        $('#tabelhelpadmin').html(data);
      }
    })
  })

  // edit help
  $('#edithelp').on('show.bs.modal', function (e) {
    var id_help = $(e.relatedTarget).data('id_help');
    var judul_help = $(e.relatedTarget).data('judul_help');
    var isi_help = $(e.relatedTarget).data('isi_help');

    $('#id_help').val(id_help);
    $('#judul_help').val(judul_help);
    $('#isi_help').val(isi_help);
  })

  $('#jawabask').on('show.bs.modal', function (e) {
    var id_ask = $(e.relatedTarget).data('id_ask');
    var judul_ask = $(e.relatedTarget).data('judul_ask');
    var isi_ask = $(e.relatedTarget).data('isi_ask');

    $('#id_ask').val(id_ask);
    $('#judul_ask').val(judul_ask);
    $('#isi_ask').val(isi_ask);
  })

  // editprofil
  $('#editprofiladmin').on('show.bs.modal', function (e) {
    var id_user = $(e.relatedTarget).data('id_user');

    var ajaxeditprofil = document.getElementById('ajaxeditprofil');

    $.ajax({
      url: base_url + '/ajax/editprofil',
      type: 'POST',
      data: 'id_user=' + id_user,
      success: function (data) {
        ajaxeditprofil.innerHTML = data;
      }
    })
  })

  // validasi password
  $('#password_baru2').keyup(function () {
    var password_lama = $('#password_lama').val();
    var password_baru = $('#password_baru').val();
    var password_baru2 = $('#password_baru2').val();

    if ($('#password_baru').val() != $('#password_baru2').val()) {
      $('#password_baru_message').text('Password dan konfirmasi tidak sesuai');
      $('#btnubahpassword').attr('disabled', true);
    } else {
      $('#password_baru_message').text('');
      $('#btnubahpassword').removeAttr('disabled');
    }
  })

  // ubah situs
  $('#ubahsitus').on('show.bs.modal',
    function (event) {
      var id_site = $(event.relatedTarget).data('id_site');

      $.ajax({
        url: base_url + '/ajax/ubahsitus',
        method: 'POST',
        data: {
          'id_site': id_site
        },
        success: function (data) {
          $('#ajaxubahsitus').html(data);
        }
      })
    })

  $('#ubahruangan').on('show.bs.modal',
    function (event) {
      var id_ruangan = $(event.relatedTarget).data('id_ruangan');

      $.ajax({
        url: base_url + '/ajax/ubahruangan',
        method: 'POST',
        data: {
          'id_ruangan': id_ruangan
        },
        success: function (data) {
          $('#ajaxubahruangan').html(data);
        }
      })
    })



  // LIVE LOAD KOTAK PERTANYAAN
  $.ajax({
    url: base_url + '/ajax/livekotakpertanyaan',
    dataType: 'html',
    success: function (data) {
      $('#livekotakpertanyaan').html(data);
    }
  })

  // AUTH
  // MENDAFTAR
  $(document).ready(function() {
    // Validasi nama lengkap
    $('#nama_lengkap').on('input', function() {
      var value = $(this).val();
      if (value.length < 3) {
        $('#alertdaftar').text('Nama lengkap minimal terdiri dari 3 karakter!');
        $('#alertdaftar').removeAttr('style');
        $('#daftar').attr('disabled', true);
      } else {
        $('#alertdaftar').attr('style', 'display: none');
        $('#alertdaftar').text('');
        $('#daftar').removeAttr('disabled');
      }
    });
  
    // Validasi username
    $('#usernamedaftar').on('input', function() {
      var value = $(this).val();
      if (value.length < 3) {
        $('#alertdaftar').text('Username minimal terdiri dari 3 karakter!');
        $('#alertdaftar').removeAttr('style');
        $('#daftar').attr('disabled', true);
      } else {
        $('#alertdaftar').attr('style', 'display: none');
        $('#alertdaftar').text('');
        $('#daftar').removeAttr('disabled');
      }
      // Validasi username harus diawali dengan huruf kapital
      var regex = /^[A-Z][a-z0-9_-]{2,}$/;
      if (!regex.test(value)) {
        $('#alertdaftar').text('Username harus diawali dengan huruf kapital!');
        $('#alertdaftar').removeAttr('style');
        $('#daftar').attr('disabled', true);
      } else {
        $('#alertdaftar').attr('style', 'display: none');
        $('#alertdaftar').text('');
        $('#daftar').removeAttr('disabled');
      }
    });
  
    // Validasi password
    $('#password2').keyup(function () {
      var password = $('#password').val();
      var password2 = $(this).val();
  
      if (password != password2) {
        $('#alertdaftar').text('Password dan konfirmasi tidak sama!');
        $('#alertdaftar').removeAttr('style');
        $('#daftar').attr('disabled', true);
      } else {
        $('#alertdaftar').attr('style', 'display: none');
        $('#alertdaftar').text('');
        $('#daftar').removeAttr('disabled');
      }
      // Validasi password harus terdiri dari huruf kapital dan huruf kecil
      var regex = /^(?=.*[a-z])(?=.*[A-Z]).{6,}$/;
      if (!regex.test(password)) {
        $('#alertdaftar').text('Password harus terdiri dari huruf kapital dan huruf kecil!');
        $('#alertdaftar').removeAttr('style');
        $('#daftar').attr('disabled', true);
      } else {
        $('#alertdaftar').attr('style', 'display: none');
        $('#alertdaftar').text('');
        $('#daftar').removeAttr('disabled');
      }
    });
  
    // Validasi NIP/NIS
    $('#nip').on('input', function() {
      var value = $(this).val();
      if (value.length < 10) {
        $('#alertdaftar').text('NIP/NIS minimal terdiri dari 10 karakter!');
        $('#alertdaftar').removeAttr('style');
        $('#daftar').attr('disabled', true);
      } else {
        $('#alertdaftar').attr('style', 'display: none');
        $('#alertdaftar').text('');
        $('#daftar').removeAttr('disabled');
      }
    });
  });
  

  // PEMINJAM
  // MEMINJAM
  $('#pinjam').on('show.bs.modal', function (e) {
    var id_ruangan = $(e.relatedTarget).data('id_ruangan');

    $('#id_ruangan').val(id_ruangan);
  })

  // verify req date
  $('.verifydate').change(function (e) {
    e.preventDefault();
    var tanggal = $('#tanggal').val();
    var jam_mulai = $('#pinjam_jam_mulai').val();
    var jam_selesai = $('#pinjam_jam_selesai').val();

    $.ajax({
      url: base_url + '/ajax/ajaxverifyreqdate',
      type: 'POST',
      data: {
        'tanggal': tanggal,
        'jam_mulai': jam_mulai,
        'jam_selesai': jam_selesai
      },
      success: function (data) {
        $('#alerttanggal').html(data);
        if (data != "") {
          $('.pinjam').attr('disabled', true);
        } else {
          $('.pinjam').removeAttr('disabled');
        }
      }
    })
  });

  // ADMIN
  // DATA TABEL
  $('#tabel').DataTable({
    "paging": true,
    "lengthChange": true,
    "searching": true,
    "ordering": true,
    "info": true,
    "autoWidth": true,
  });

  $('#tabel2').DataTable({
    "paging": true,
    "lengthChange": true,
    "searching": true,
    "ordering": true,
    "info": true,
    "autoWidth": true,
  });

  // filterbulan
  $('#filter-bulan').change(function () {
    var bulan = $('#filter-bulan option:selected').val();

    $.ajax({
      url: base_url + '/ajax/filter_bulan',
      type: 'POST',
      dataType: 'html',
      data: {
        'bulan': bulan
      },
      success: function (data) {
        $('#tabeljadwal').html(data);
      }
    })
  })

  // filter-tanggal
  $('#filter_tanggal').change(function () {
    var tanggal = $('#filter_tanggal').val();

    $.ajax({
      url: '/ajax/filter_tanggal',
      type: 'POST',
      data: {
        'tanggal': tanggal
      },
      success: function (data) {
        $('#tabeljadwal').html(data);
      }
    })
  })

  // filter range tanggal
  $('#filter_tanggal2').change(function () {
    var tanggal1 = $('#filter_tanggal1').val();
    var tanggal2 = $('#filter_tanggal2').val();

    $.ajax({
      url: base_url + '/ajax/filter_range_tanggal',
      type: 'POST',
      dataType: 'html',
      data: {
        'tanggal1': tanggal1,
        'tanggal2': tanggal2
      },
      success: function (data) {
        $('#tabeljadwal').html(data);
      }
    })
  })

  // cetakjadwal
  $('#cetaklapjadwal').click(function () {
    $('#tabel').print();
  })

  // SITE SETTINGS
  // UBAH LOGO
  $('#logo').change(function () {
    var logo = $('#logo').val().split("\\");
    var logoname = logo.slice(-1)[0];
    $('#logoname').text('Ubah ke: ' + logoname);
  });

  // UBAH ICON
  $('#icon').change(function () {
    var icon = $('#icon').val().split("\\");
    var iconname = icon.slice(-1)[0];
    $('#iconname').text('Ubah ke: ' + iconname);
  });
})
// Password validation
addEventListener("DOMContentLoaded", (event) => {
  const password = document.getElementById("password-input");
  const passwordAlert = document.getElementById("password-alert");
  const requirements = document.querySelectorAll(".requirements");
  let lengBoolean, bigLetterBoolean, numBoolean, specialCharBoolean;
  let leng = document.querySelector(".leng");
  let bigLetter = document.querySelector(".big-letter");
  let num = document.querySelector(".num");
  let specialChar = document.querySelector(".special-char");
  const specialChars = "!@#$%^&*()-_=+[{]}\\|;:'\",<.>/?`~";
  const numbers = "0123456789";

  requirements.forEach((element) => element.classList.add("wrong"));

  password.addEventListener("focus", () => {
      passwordAlert.classList.remove("d-none");
      if (!password.classList.contains("is-valid")) {
          password.classList.add("is-invalid");
      }
  });

  password.addEventListener("input", () => {
      let value = password.value;
      if (value.length < 8) {
          lengBoolean = false;
      } else if (value.length > 7) {
          lengBoolean = true;
      }

      if (value.toLowerCase() == value) {
          bigLetterBoolean = false;
      } else {
          bigLetterBoolean = true;
      }

      numBoolean = false;
      for (let i = 0; i < value.length; i++) {
          for (let j = 0; j < numbers.length; j++) {
              if (value[i] == numbers[j]) {
                  numBoolean = true;
              }
          }
      }

      specialCharBoolean = false;
      for (let i = 0; i < value.length; i++) {
          for (let j = 0; j < specialChars.length; j++) {
              if (value[i] == specialChars[j]) {
                  specialCharBoolean = true;
              }
          }
      }

      if (lengBoolean == true && bigLetterBoolean == true && numBoolean == true && specialCharBoolean == true) {
          password.classList.remove("is-invalid");
          password.classList.add("is-valid");

          requirements.forEach((element) => {
              element.classList.remove("wrong");
              element.classList.add("good");
          });
          passwordAlert.classList.remove("alert-warning");
          passwordAlert.classList.add("alert-success");
      } else {
          password.classList.remove("is-valid");
          password.classList.add("is-invalid");

          passwordAlert.classList.add("alert-warning");
          passwordAlert.classList.remove("alert-success");

          if (lengBoolean == false) {
              leng.classList.add("wrong");
              leng.classList.remove("good");
          } else {
              leng.classList.add("good");
              leng.classList.remove("wrong");
          }

          if (bigLetterBoolean == false) {
              bigLetter.classList.add("wrong");
              bigLetter.classList.remove("good");
          } else {
              bigLetter.classList.add("good");
              bigLetter.classList.remove("wrong");
          }

          if (numBoolean == false) {
              num.classList.add("wrong");
              num.classList.remove("good");
          } else {
              num.classList.add("good");
              num.classList.remove("wrong");
          }

          if (specialCharBoolean == false) {
              specialChar.classList.add("wrong");
              specialChar.classList.remove("good");
          } else {
              specialChar.classList.add("good");
              specialChar.classList.remove("wrong");
          }
      }
  });

  password.addEventListener("blur", () => {
      passwordAlert.classList.add("d-none");
  });
});