import $ from "jquery";

import alertify from "alertifyjs";
import "alertifyjs/build/css/alertify.min.css";

$(window).ready(
    () => {
        let startDateWarningDismissed = false;
        let endDateWarningDismissed = false;

        $(".field-publishing_rejection_comment").
        css(
            "display",
            (
                (!$("#id_is_public").is(":checked")) ?
                "block": "none"
            )
        );

        $("#id_is_public").on(
            "change",
            event => {
                const publicIsSelected = $(event.target).is(":checked");

                $(".field-publishing_rejection_comment").
                css(
                    "display",
                    (
                        (!publicIsSelected) ?
                        "block" : "none"
                    )
                );

                if (publicIsSelected) $(".field-publishing_rejection_comment").val("");
            }
        );

        $(".field-cost_unit_other").
        css(
            "display",
            (
                ($("#id_cost_unit").find(":selected").val() === "other") ?
                "block": "none"
            )
        )

        $("#id_cost_unit").on(
            "change",
            event => {
                const otherIsSelected = ($(event.target).find(":selected").val() === "other");

                $(".field-cost_unit_other").
                css(
                    "display",
                    (
                        (otherIsSelected) ?
                        "block" : "none"
                    )
                );

                if (!otherIsSelected) $(".field-cost_unit_other").val("");
            }
        );

        $(
            "#contract_form .submit-row input[name=\"_save\"],"
            + "#contract_form .submit-row input[name=\"_addanother\"],"
            + "#contract_form .submit-row input[name=\"_continue\"]"
        ).on(
            "click",
            event => {
                if (!startDateWarningDismissed && $("#id_valid_start_date").val() === "") {
                    event.preventDefault();

                    alertify.alert(
                        "Začátek platnosti není zadán",
                        `Začátek platnosti nebyl uveden. Pokud ho necháš tak, jak je,
bude automaticky vyplněn podle posledního data
podpisu smluvních stran. Pokud podpisy neexistují,
pole zůstane prázdné.`,
                        () => {
                            startDateWarningDismissed = true;
                        }
                    );
                }

                if (!endDateWarningDismissed && $("#id_valid_end_date").val() === "") {
                    event.preventDefault();

                    alertify.alert(
                        "Konec platnosti není zadán",
                        `Konec platnosti nebyl uveden. Pokud ho necháš tak, jak je,
smlouva bude evidována jako na dobu neurčitou.`,
                        () => {
                            endDateWarningDismissed = true;
                        }
                    );
                }
            }
        )
    }
);