<template>
    <!-- Checkbox groups MUST be wrapped in a <fieldset> and <legend> tag -->
    <div
        class="form-check ui-checkbox d-flex align-items-center"
        :class="[hasError ? 'has-error' : '']">
        <label class="d-flex align-items-start" :for="buttonID">
            <input
                v-model="value"
                class="form-check-input me-2"
                type="checkbox"
                aria-checked="false"
                :name="checkboxName"
                :validationRules="validationRules"
                :id="inputId"
                :tabindex="tabIndex"
                :aria-required="isRequired" />
            <textBlock
                class="m-0"
                :customText="checkboxLabelCopy"
                marginTopSizeOverride="0"
                typeStyle="small"
                @text-link-clicked="bubbleTextBlockClick" />
            <span v-if="screenReaderOnlyText" class="sr-only">{{ screenReaderOnlyText }}</span>
        </label>
    </div>
</template>

<script>
import { useField, validate } from "vee-validate";
import { v4 as uuidv4 } from "uuid";
import textBlock from "@/digital-components/text-block/text-block";

export default {
    name: "checkboxQuestion",
    computed: {
        checkboxLabelCopy() {
            return this.getCmsContent(this.cmsWidgetName, "QuestionText");
        },
    },
    setup(props) {
        const uuid = uuidv4();
        const inputId = !props.customInputId ? `input-${uuid}` : props.customInputId;

        const fieldOptions = {
            initialValue: props.modelValue,
        };

        const {
            value,
            errorMessage,
            handleBlur,
            handleChange,
            meta,
            validate,
            errors,
            resetField,
        } = useField(inputId, props.validationRules, fieldOptions);

        return {
            value,
            errorMessage,
            handleBlur,
            handleChange,
            validate,
            meta,
            errors,
            resetField,
            inputId,
        };
    },
    props: {
        customInputId: String,
        validationRules: String,
        cmsWidgetName: String,
        checkboxName: String,
        buttonID: String,
        tabIndex: Number,
        screenReaderOnlyText: String,
        isRequired: Boolean,
        hasError: Boolean,
        modelValue: {
            type: Boolean,
            default: false,
        },
    },
    methods: {
        bubbleTextBlockClick(event) {
            this.$emit("textLinkClicked", event);
        },
    },
    watch: {
        modelValue(newValue, oldValue) {
            if (this.value !== newValue) {
                this.value = newValue;
            }
        },
        value(newValue, oldValue) {
            this.$emit("update:modelValue", newValue);
        },
    },
    components: {
        textBlock,
    },
};
</script>

<style lang="scss" scoped>
.form-check {
    .form-check-input {
        border: 1px solid $gray-500;
        border-radius: 2px;
        min-width: 1rem; // Prevent squish
        &:checked {
            background-size: 125%;
            border: 1px solid $blue;
            + label {
                p {
                    font-weight: 500;
                    font-size: 0.875rem;
                    color: $black;
                }
            }
        }
        &:focus {
            box-shadow: 0 0 0 2.5px $blue;
        }
        &:checked[type="checkbox"] {
            background-color: $blue;
            background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill='none' stroke='%23ffffff' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='M6 10l3 3l6-6'/%3e%3c/svg%3e");
        }
    }
    &:hover {
        .form-check-input {
            box-shadow: 0 0 0 4px $blue-300;
        }
    }
    p {
        font-weight: 400;
        font-size: 0.875rem;
        color: $gray-600;
    }
}
</style>
