var list;
var attachment;
var removeMedia = [];

$(".btn-edit-user").click(function(e){
    e.preventDefault();
    var id = $(e.target.closest('a')).attr('value');
    $('#modal-new-user').attr('user', id);
  $('#modal-new-user').modal('show');
});

$('.deleteForm').submit(function(event){
    if(!confirm("Delete user ini?")){
       event.preventDefault();
     }
});

$('.changeForm').submit(function(event){
    if(!confirm("Ubah status user ini?")){
       event.preventDefault();
     }
});

$("#companySelect").change(async(e)=>{
    if(e.target.value&&e.target.value!=''){
        $("#divisionSelect").removeAttr('disabled');
        var getToken = await fetch(`/session/key?s=jwt`);
        if(getToken.ok){
            var token = await getToken.text();
            var res = await fetch(`/api/divisions?filters[companies][id][$in]=${e.target.value}`,{
                headers:{
                    "Authorization" : "Bearer "+token,
                }
            });
            if(res.ok){
                var obj = await res.json();
                $("#divisionSelect").empty();
                $("#divisionSelect").append('<option  value="">Semua</option>');
                obj.data.forEach((i)=>{
                    $("#divisionSelect").append(`<option  value="${i.id}">${i.attributes.Name}</option>`);
                });
            }
        }
        else{
            showAlert('error','Gagal menarik data divisi. Harap login ulang!');
        }
    }
    else {
        $("#divisionSelect").attr('disabled','disabled');
    }
});

$(".page-link").click((e)=>{
    e.preventDefault();
    var page = $(e.target.closest('a')).attr('value');
    $("input[name=page]").val(page);
    $("#formSearch").submit();
});

$("#btnSearch").click((e)=>{
    e.preventDefault();
    $("input[name=page]").val(1);
    $("#formSearch").submit();
});