<script>
    $('body').on('click', '.btn-create', function()
    {
        event.preventDefault();
        $('#form_save select[name="iddistrito"]').select2({
            placeholder: "[SELECCIONE]",
            dropdownParent: $('#modalAddOrigin')
        });
        $('#modalAddOrigin').modal('show');
    });

    $('body').on('click', '.btn-save', function()
    {
        event.preventDefault();
        let form            = $('#form_save').serialize(),
            direccion       = $('#form_save input[name="direccion"]');

        if(direccion.val() == '')
            direccion.addClass('is-invalid');
        else
            direccion.removeClass('is-invalid');

        if(direccion.val().trim() != '')
        {
            $.ajax({
                url         :  "<?php echo e(route('admin.save_origin')); ?>",
                method      : 'POST',
                data        : form,
                beforeSend  : function(){
                    $('.btn-save').prop('disabled', true);
                    $('.text-saving').removeClass('d-none');
                    $('.text-save').addClass('d-none');
                },
                success     : function(r)
                {
                    if(!r.status)
                    {
                        $('.btn-save').prop('disabled', false);
                        $('.text-saving').addClass('d-none');
                        $('.text-save').removeClass('d-none');
						toast_msg(r.msg, r.type);
                        return;
                    }

                    $('#modalAddOrigin').modal('hide');
                    $('#form_save').trigger('reset');
                    $('.btn-save').prop('disabled', false);
                    $('.text-save').removeClass('d-none');
                    $('.text-saving').addClass('d-none');
                    toast_msg(r.msg, r.type);
                    reload_table();
                },
                dataType    : 'json'
            });
            return;
        }
    });

    $('body').on('click', '.btn-detail', function()
    {
        event.preventDefault();
        let id  = $(this).data('id');
        $.ajax({
            url         : "<?php echo e(route('admin.detail_origin')); ?>",
            method      : 'POST',
            data        : {'_token' : "<?php echo e(csrf_token()); ?>",id: id},
            beforeSend  : function(){
                block_content('#layout-content');
            },
            success     : function(r){
                if(!r.status)
                {
                    close_block('#layout-content');
                    toast_msg(r.msg, r.type);
                    return;
                }
                close_block('#layout-content');
                $('#form_edit input[name="id"]').val(r.origin.id);
                $('#form_edit input[name="direccion"]').val(r.origin.direccion);
                $('#modalEditOrigin select[name="iddistrito"]').select2({
                    placeholder: "[SELECCIONE]",
                    dropdownParent: $('#modalEditOrigin')
                });
                $('#modalEditOrigin select[name="iddistrito"]').val(r.origin.iddistrito).trigger('change');
                $('#modalEditOrigin').modal('show');
            },
            dataType    : 'json'
        });
        return;
    });

    $('body').on('click', '.btn-store', function()
    {
        event.preventDefault();
        let form            = $('#form_edit').serialize(),
            direccion     = $('#form_edit input[name="direccion"]');

        if(direccion.val() == '')
            direccion.addClass('is-invalid');
        else
            direccion.removeClass('is-invalid');

        if(direccion.val().trim() != '')
        {
            $.ajax({
                url         :  "<?php echo e(route('admin.store_origin')); ?>",
                method      : 'POST',
                data        : form,
                beforeSend  : function(){
                    $('.btn-store').prop('disabled', true);
                    $('.text-store').addClass('d-none');
                    $('.text-storing').removeClass('d-none');
                },
                success     : function(r)
                {
                    if(!r.status)
                    {
                        $('.btn-store').prop('disabled', false);
                        $('.text-store').removeClass('d-none');
                        $('.text-storing').addClass('d-none');
                        toast_msg(r.msg, r.type);
                        return;
                    }

                    $('#modalEditOrigin').modal('hide');
                    $('.btn-store').prop('disabled', false);
                    $('.text-store').removeClass('d-none');
                    $('.text-storing').addClass('d-none');
                    toast_msg(r.msg, r.type);
                    reload_table();
                },
                dataType    : 'json'
            });
            return;
        }
    });

    $('body').on('click', '.btn-confirm', function()
    {
        event.preventDefault();
        let id      = $(this).data('id');
        Swal.fire({
            title: 'Eliminar',
            text: "¿Desea eliminar el registro?",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonText: 'Si, eliminar',
            cancelButtonText: 'Cancelar',
            customClass: {
                confirmButton: 'btn btn-primary',
                cancelButton: 'btn btn-outline-danger ml-1'
            },
            buttonsStyling: false
        }).then(function (result) {
            if (result.value) 
            {
                $.ajax({
                    url         : "<?php echo e(route('admin.delete_origin')); ?>",
                    method      : 'POST',
                    data        : {
                        '_token': "<?php echo e(csrf_token()); ?>",
                        id      : id
                    },
                    success     : function(r){
                        if(!r.status)
                        {
                            toast_msg(r.msg, r.type);
                            return;
                        }

                        toast_msg(r.msg, r.type);
                        reload_table();
                    },
                    dataType    : 'json'
                });
            }
        });
    });
</script><?php /**PATH C:\xampp\htdocs\courier\resources\views/admin/origins/js-store.blade.php ENDPATH**/ ?>