
$("#btnSave").on('click',function(e){
    e.preventDefault();
    data_id = $('#data-id').val();
    let url = (data_id < 1 ? save_url : update_url);
    $.ajax({
        url: url,
        type:"POST",
        data :{
            "_token": csrf_token,
            uid : data_id,
            name_en: $('#name-en').val(),
            vat_number: $('#vat-number').val(),
            address: $('#address').val(),
            description: $('#description').val(),
            contact_name: $('#contact-name').val(),
            phone_number: $('#phone-number').val(),
            email: $('#email').val(),
            position: $('#position').val(),
        },
        success:function(res){
            handleMessage(res.success, res.message)
            if(res.success) setTimeout(function () {location.reload()}, messageDuration)
        }
    });
});

function delete_data(uid){
    Swal.fire({
        text: confirm_delete_record,
        icon: 'question',
        showCancelButton: true,
        cancelButtonClass:'danger',
        confirmButtonText: confirm_ok_delete,
        cancelButtonText: confirm_cancel
    }).then((result) => {
        if (result.value) {
            $.ajax({
                url: delete_url,
                method:'POST',
                data:{
                    "_token": csrf_token,
                    uid : uid,
                },
                success:function(res){
                    handleMessage(res.success, res.message)
                    if(res.success) setTimeout(function () {location.reload()}, messageDuration)
                }
            })

        }
    });
}

function restore_data(uid){
    Swal.fire({
        text: confirm_restore_record,
        icon: 'question',
        showCancelButton: true,
        cancelButtonClass:'danger',
        confirmButtonText: confirm_ok_restore,
        cancelButtonText: confirm_cancel_restore,
    }).then((result) => {
        if (result.value) {
            $.ajax({
                url: restore_url,
                method:'POST',
                data:{
                    "_token": csrf_token,
                    uid : uid,
                },
                success:function(res){
                    handleMessage(res.success, res.message)
                    if(res.success) setTimeout(function () {location.reload()}, messageDuration)
                }
            })

        }
    });
}


$('#modal-supplier').on('show.bs.modal', function (event) {
    var button = $(event.relatedTarget) // Button that triggered the modal
    var action = button.data('action') // Extract info from data-* attributes
    var modal = $(this);
    if(action === "new"){
        // Clear inputs
        modal.find('.modal-body input[name="uid"]').val(0);
        modal.find('.modal-body input').val("");
        modal.find('.modal-title').html(modal_add_title);
    }else if(action === "edit"){
        uid = button.data('uid');
        modal.find('.modal-body input[name="uid"]').val(uid);
        modal.find('.modal-title').html(modal_edit_title);
        $.ajax({
            url: get_url,
            type:"POST",
            data:{
                "_token": csrf_token,
                uid: uid,
            },
            success:function(res){
                let data = res.data;
                modal.find('.modal-body input[name="name_en"]').val(data.name_en);
                modal.find('.modal-body input[name="vat_number"]').val(data.vat_number);
                modal.find('.modal-body input[name="address"]').val(data.address);
                modal.find('.modal-body input[name="description"]').val(data.description);
                modal.find('.modal-body input[name="contact_name"]').val(data.contact_name);
                modal.find('.modal-body input[name="phone_number"]').val(data.phone_number);
                modal.find('.modal-body input[name="email"]').val(data.email);
                modal.find('.modal-body input[name="position"]').val(data.position);
                if(!res.success) handleMessage(res.success, res.message)
            }
        });
    }
})
