<script>
    $("#city").select2();
</script>
<script>
    function clear_fields() {
        $("#add_broker_form")[0].reset();
        $(".error").text('');
    }
    var broker_table = $("#cities_table").DataTable({
        processing: true,
        serverSide: true,
        ajax: "{{ route('broker_index') }}",
        columns: [{
                name: "DT_RowIndex",
                data: "DT_RowIndex"
            },
            {
                name: "name",
                data: "name"
            },
            {
                name: "cnic",
                data: "cnic"
            },
            {
                name: "phone",
                data: "phone"
            },
            {
                name: "city",
                data: "city"
            },
            {
                name: "action",
                data: "action",
                orderable: false,
            },
        ]
    });
    $("#add_broker_form").on('submit', function(e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('broker_store') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function() {
                $("#submit_btn").text('پراسسنگ۔۔۔۔۔');
                $("#submit_btn").prop('disabled', true);
            },
            success: function(data) {
                $("#submit_btn").text('محفوظ');
                $("#submit_btn").prop('disabled', false);
                if (data.success) {
                    broker_table.ajax.reload();
                    clear_fields();
                    alertify.success("Broker 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_broker', function() {
        var edit_id = $(this).data('id');
        var url = "{{ route('broker_edit', ':id') }}";
        url = url.replace(':id', edit_id);
        $.ajax({
            url: url,
            type: "GET",
            success: function(resonpse) {
                // console.log(resonpse);
                $("#editBrokerData").html(resonpse);
                $("#editBrokerModal").modal('toggle');
                setTimeout(() => {
                    $(document).find("#u_city").select2();
                }, 300);
            }
        });
    });
    $(document).on('submit', "#update_broker_form", function(e) {
        e.preventDefault();
        $.ajax({
            url: "{{ route('broker_update') }}",
            type: "POST",
            contentType: false,
            processData: false,
            data: new FormData(this),
            beforeSend: function() {
                $("#update_btn").text('پراسسنگ۔۔۔۔۔');
                $("#update_btn").prop('disabled', true);
            },
            success: function(data) {
                $("#update_btn").text('اپ ڈیٹ');
                $("#update_btn").prop('disabled', false);
                if (data.success) {
                    broker_table.ajax.reload();
                    $("#editBrokerModal").modal('hide');
                    alertify.success("Broker Updated Successfully!");

                } else {
                    alertify.error("Something Went Wrong!");
                }
            },
            error: function(error) {
                $("#update_btn").text('اپ ڈیٹ');
                $("#update_btn").prop('disabled', false);
                if ('errors' in error.responseJSON) {
                    $(".u_error").text('');
                    $.each(error.responseJSON.errors, (index, value) => {
                        $("#u_" + index + "_error").text(value[0]);
                    });
                }
            }
        });
    });
    $(document).on('click', '.delete_broker', function() {
        var del_id = $(this).data('id');
        var url = "{{ route('broker_delete', ':id') }}";
        url = url.replace(':id', del_id);
        if (confirm('کیا آپ کو بروکر کو حذف کرنے کا یقین ہے؟')) {
            $.ajax({
                url: url,
                type: "GET",
                success: function(response) {
                    if (response.success) {
                        broker_table.ajax.reload();
                        alertify.success("Broker Deleted Successfully!");
                    } else {
                        alertify.error("Something Went Wrong!");
                    }
                }
            });
        } else {
            return false;
        }
    });
</script>
