<script>
    function clear_fields() {
        $("#add_category_form")[0].reset();
        $(".error").text('');
        $("#head_name").prop('autofocus', true);
    }

    var category_table = $("#cities_table").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('category_account_index') }}",
        columns: [{
            name: "DT_RowIndex",
            data: "DT_RowIndex"
        },
            {
                name: "name",
                data: "name"
            },
            {
                name: "action",
                data: "action",
                orderable: false,
            },
        ]
    });
    $("#add_category_form").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('category_account_store') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $("#submit_btn").text('Processing....');
                $("#submit_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#submit_btn").text('محفوظ');
                $("#submit_btn").prop('disabled', false);
                if (data.success) {
                    category_table.ajax.reload();
                    clear_fields();
                    alertify.success("Category Added Successfully!");
                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $("#submit_btn").text('محفوظ');
                $("#submit_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });
    $(document).on('click', '.edit_head', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('category_account_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                $("#editExpenseHeadData").html(resonpse);
                $("#editExpenseHeadModal").modal('show');
            }
        });
    });
    $(document).on('submit', "#update_category_form", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('category_account_update') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $("#update_btn").text('Processing....');
                $("#update_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#update_btn").text('اپڈیٹ');
                $("#update_btn").prop('disabled', false);
                if (data.success) {
                    category_table.ajax.reload();
                    $("#editExpenseHeadModal").modal('hide');
                    alertify.success("Category Updated Successfully!");

                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $("#update_btn").text('اپڈیٹ');
                $("#update_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#u_" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });
    $(document).on('click', '.delete_head', function () {
        var del_id = $(this).data('id');
        var url = "{{ route('category_account_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('Are You Sure To Delete The Category?')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function (response) {
                    if (response.success) {
                        category_table.ajax.reload();
                        alertify.success("Category Deleted Successfully!");
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });
</script>
