
// ===========
// Edit Permit
// ===========

$(document).ready(function () {

    var $application = $("#APPLICATION_MNEMONIC").val(), $sessionStorage = JSON.parse(sessionStorage.getItem($application) || "{}");

    // -----------
    // Input Masks
    // -----------

    $(".phone-mask").inputmask("(999) 999-9999", {showMaskOnHover: false});
    $(".zip-mask").inputmask("9{5}", {showMaskOnHover: false, removeMaskOnSubmit: true});

    $(".date-mask").inputmask({alias: "datetime",
        inputFormat: "mm/dd/yyyy",
        placeholder: "MM/DD/YYYY",
        showMaskOnHover: false});

    // --------------
    // Select Picker.
    // --------------

    $(".search-dropdown").selectpicker({noneSelectedText: "",
        style: "",
        styleBase: "form-select form-select-sm",
        iconBase: "bi",
        virtualScroll: true,
        size: 10,
        selectOnTab: true,
        containter: "body",
        liveSearch: true});

    // ------------
    // Date Pickers
    // ------------

    $("#permit_entry_date").datepicker({format: "mm-dd-yyyy",
        container: "div.permit_entry_date",
        keyboardNavigation: false,
        showOnFocus: false,
        autoclose: true,
        endDate: "0d",
        orientation: "left bottom",
        todayBtn: "linked",
        todayHighlight: true});

    $("#permit_permit_date").datepicker({format: "mm-dd-yyyy",
        container: "div.permit_permit_date",
        keyboardNavigation: false,
        showOnFocus: false,
        autoclose: true,
        endDate: "0d",
        orientation: "left bottom",
        todayBtn: "linked",
        todayHighlight: true});

    // ---------------------
    // Show Calendar Buttons
    // ---------------------

    $(".btnEntryDateCalendar").on("click", function (e) {

        e.preventDefault();

        $("#permit_entry_date").focus().datepicker("show");

    });

    $(".btnPermitDateCalendar").on("click", function (e) {

        e.preventDefault();

        $("#permit_permit_date").focus().datepicker("show");

    });

    // -----------
    // Save Button
    // -----------

    $(".btnSave").on("click", function (e) {

        e.preventDefault();

        $("#editPermit").submit();

    });

    // --------------
    // Cancel Button.
    // --------------

    $(".btnCancel").on("click", function () {

        $sessionStorage.action = $sessionStorage.editPermit || "";

        sessionStorage.setItem($application, JSON.stringify($sessionStorage));

        formSubmit($sessionStorage);

    });

    // -------------
    // Delete Button
    // -------------

    $(".btnDelete").on("click", function () {

        $("#confirm-dialog").removeClass().addClass("modal confirm-delete-permit")
                .find(".modal-title").html("Confirm Delete Permit").end()
                .find(".modal-body").html("Are you sure you wish to delete this Permit?").end()
                .modal("show");

    });

    $("body").on("click", ".confirm-delete-permit .btnConfirmDialogConfirm", function () {

        $.post("./", {action: "Permits.deletePermit", permit_serial: $sessionStorage.permit_serial})
                .done(function () {
                    formSubmit({action: $sessionStorage.deletePermit});
                });

    });

    // ---------------
    // Validate Inputs
    // ---------------

    $("#editPermit").submit(function (e) {

        var permit_entry_date = $("#permit_entry_date"), permit_county = $("#permit_county");

        clearInputErrors();


        if (isBlank(permit_entry_date)) {
            createInputError(permit_entry_date, "Entry Date is required");
            return false;
        }

        if ($("option:selected", permit_county).val() === "") {
            createInputError(permit_county, "County is required");
            return false;
        }

        $.post("./", $(this).serialize())

                .done(function () {

                    $sessionStorage.action = $sessionStorage.editPermit || "";

                    sessionStorage.setItem($application, JSON.stringify($sessionStorage));

                    formSubmit($sessionStorage);

                });

    });

});