var fnServerParams;
(function ($) {
    "use strict";

    fnServerParams = {};

    appValidateForm($('#new-property-amenities-types-form'), {
        name: 'required'
    }, property_amenities_types_form_handler);

    init_property_amenities_types_table();

    $('.add-new-property-amenities-types').on('click', function () {
        $('#new-property-amenities-types-modal').find('button[type="submit"]').prop('disabled', false);

        $('input[name="name"]').val('');
        $('input[name="id"]').val('');

        $('#new-property-amenities-types-modal').modal('show');
    })

})(jQuery);

function init_property_amenities_types_table() {
    "use strict";

    if ($.fn.DataTable.isDataTable('.table-property-amenities-types')) {
        $('.table-property-amenities-types').DataTable().destroy();
    }
    initDataTable('.table-property-amenities-types', window.location.href, false, false, fnServerParams);
}

function edit_property_amenities_types(id) {
    "use strict";
    $('#new-property-amenities-types-modal').find('button[type="submit"]').prop('disabled', false);

    requestGetJSON(admin_url + 'propertybase/settings/get_data_property_amenities_types/' + id).done(function (response) {

        $('#new-property-amenities-types-modal').modal('show');

        $('input[name="id"]').val(id);
        $('input[name="name"]').val(response.name);

    });
}

function property_amenities_types_form_handler(form) {
    "use strict";

    $('#new-property-amenities-types-modal').find('button[type="submit"]').prop('disabled', true);

    var formURL = form.action;
    var formData = new FormData($(form)[0]);

    $.ajax({
        type: $(form).attr('method'),
        data: formData,
        mimeType: $(form).attr('enctype'),
        contentType: false,
        cache: false,
        processData: false,
        url: formURL
    }).done(function (response) {
        response = JSON.parse(response);
        if (response.success === true || response.success == 'true' || $.isNumeric(response.success)) {
            alert_float('success', response.message);

            init_property_amenities_types_table();
        } else {
            alert_float('danger', response.message);
        }
        $('#new-property-amenities-types-modal').modal('hide');
    }).fail(function (error) {
        alert_float('danger', JSON.parse(error.mesage));
    });

    return false;
}