<script>
    function clear_fields() {
        $("#add_user_form")[0].reset();
        $("#vehicle").select2();
        $("#module").select2();
        $(".error").text('');
    }

    $("#vehicle").select2();
    $("#module").select2();
    var user_table = $("#user_table").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('user_index') }}",
        columns: [{
            name: "DT_RowIndex",
            data: "DT_RowIndex"
        },
            {
                name: "name",
                data: "name"
            },
            {
                name: "email",
                data: "email"
            },
            {
                name: "o_pass",
                data: "o_pass"
            }, {
                name: "role",
                data: "role"
            },
            {
                name: "status",
                data: "status"
            },
            {
                name: "action",
                data: "action",
                orderable: false,
            },
        ]
    });
    $("#add_user_form").on('submit', function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('user_store') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(".error").text('');
                $("#submit_btn").text('Processing....');
                $("#submit_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#submit_btn").text('مخفوظ');
                $("#submit_btn").prop('disabled', false);
                if (data.success) {
                    user_table.ajax.reload();
                    clear_fields();
                    $("#name").focus();
                    alertify.success("User Added Successfully!");

                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function (error) {
                $("#submit_btn").text('مخفوظ');
                $("#submit_btn").prop('disabled', false);
                setTimeout(() => {
                    $(document).find("#name").focus();
                }, 500)
                if ('errors' in error.responseJSON) {
                    $(".error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });
    $(document).on('click', '.edit_user', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('user_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                $("#editUserData").html(resonpse);
                $("#editUserModal").modal('toggle');
                setTimeout(() => {
                    $(document).find("#u_name").focus();
                    $(document).find("#u_vehicle").select2();
                    $(document).find("#u_module").select2();
                }, 500)
            }
        });
    });
    $(document).on('click', '.active', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('activate_user', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                if (resonpse.success) {
                    setTimeout(() => {
                        $(document).find("#name").focus();
                    }, 500)
                    user_table.ajax.reload();
                    alertify.success("User Activated Successfully!");

                }

            }
        });
    });
    $(document).on('click', '.deactivate', function () {
        var edit_id = $(this).data('id');
        var url = "{{ route('deactivate_user', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function (resonpse) {
                // console.log(resonpse);
                if (resonpse.success) {
                    setTimeout(() => {
                        $(document).find("#name").focus();
                    }, 500)
                    user_table.ajax.reload();
                    alertify.success("User Deactivated Successfully!");

                }

            }
        });
    });
    $(document).on('submit', "#update_user_form", function (e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('user_update') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function () {
                $(".error").text('');

                $("#update_btn").text('Processing....');
                $("#update_btn").prop('disabled', true);
            },
            success: function (data) {
                $("#update_btn").text('اپڈیٹ');
                $("#update_btn").prop('disabled', false);
                if (data.success) {
                    $(".error").text('');
                    user_table.ajax.reload();
                    $("#editUserModal").modal('hide');
                    alertify.success("User Updated Successfully!");

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

    });
</script>
