
// =======
// Add Permits
// =======

$(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();

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

    });

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

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

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

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

        formSubmit($sessionStorage);

    });

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

    $("#addPermit").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.addPermit || "";

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

                    formSubmit($sessionStorage);

                });

    });

});