<template>
    <div class="textbox-question" :class="(errors && errors.length) || hasError ? 'has-error' : ''">
        <label
            :for="inputId"
            :aria-label="questionText"
            class="form-label"
            :class="[questionAlignment === 'center' ? 'text-center w-100 mb-5' : '']"
            v-html="labelText"></label>
        <div
            class="input-wrapper"
            :class="[
                includeSearchIcon ? 'has-search-icon' : '',
            ]">
            <input
                class="form-control"
                v-model.trim="value"
                v-maska="mask"
                :type="type"
                :ref="inputId"
                :id="inputId"
                :name="inputId"
                :placeholder="placeholderText"
                :aria-disabled="isDisabled"
                :disabled="isDisabled"
                :aria-required="isRequired"
                :aria-label="questionText"
                :min="min"
                :max="max"
                required
                :class="[
                    hasIcon ? 'has-icon' : '',
                    iconRight ? 'icon-right' : '',
                    cornerStyle === 'rounded' ? 'rounded-pill' : '',
                ]"
                :validationRules="validationRules"
                @change="validationRules ? handleChange : () => {}"
                @blur="validationRules ? handleChange : () => {}"
                :maxlength="maxLength ? maxLength : '999'"
                @focus="$emit('focus', $event.target.value)" />
            <button v-if="includeSearchIcon" type="submit" aria-label="Search button" />
        </div>
        <div v-show="errorMessage" class="row my-2 form-test-error">
            <span class="d-inline-flex mt-0" role="alert">{{ errorMessage }}</span>
        </div>
    </div>
</template>

<script>
import { useField, validate } from "vee-validate";

export default {
    name: "textbox-question",
    props: {
        type: {
            type: String,
            default: "text",
        },
        placeholderText: {
            type: String,
            default: "",
        },
        modelValue: String,
        inputId: String,
        isDisabled: Boolean,
        isRequired: Boolean,
        hasIcon: Boolean, // If input has an icon
        iconRight: Boolean, // Place icon on right side of text input, otherwise default is left if hasIcon prop is used
        hasError: Boolean,
        mask: {
            type: String,
            default: "",
        },
        validationRules: String,
        cmsWidgetName: String,
        maxLength: String,
        questionAlignment: String, // Left or center. Left is default.
        cornerStyle: String, // Rounded or square. Square is default.
        includeSearchIcon: Boolean,
        min: String,
        max: String,
        disableAutoFill: Boolean
    },
    setup(props) {
        const propsClone = Object.assign({}, props);
        const modelValue = propsClone.modelValue;
        let initialValue;

        switch (typeof modelValue) {
            case "number":
                initialValue = modelValue;
                break;
            default:
                initialValue = modelValue && modelValue.length > 0 ? modelValue : "";
                break;
        }

        const fieldOptions = {
            type: "text",
            value: modelValue,
            initialValue: initialValue,
        };

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

        return {
            errorMessage,
            handleBlur,
            handleChange,
            validate,
            meta,
            errors,
        };
    },
    computed: {
        questionText() {
            return this.getCmsContent(this.cmsWidgetName, "QuestionText");
        },
        value: {
            get: function () {
                return this.modelValue;
            },
            set: function (newValue) {
                this.$emit("update:modelValue", newValue);
            },
        },
        labelText: {
            get: function () {
                const noBreakChar = "&NoBreak;";
                var questionText = "";
                if (this.disableAutoFill) {
                    var words = this.questionText.toString().split(/[ ]+/);
                    words.forEach(function (word) {
                        const position = 1;
                        word = [
                            word.toString().slice(0, position),
                            noBreakChar,
                            word.toString().slice(position),
                        ].join("");
                        questionText += `${word} `;
                    });

                    questionText = questionText.trimEnd();
                } else {
                    questionText = this.questionText.toString();
                }

                return questionText;
            },
        },
    },
    watch: {
        async value(newValue) {
            const result = await validate(newValue, this.validationRules); // do a test validation check, without triggering full validation
            if (result.valid) {
                this.handleChange(newValue); // trigger full validation on this field only
            }
        },
    },
};
</script>

<style lang="scss">

input[type="date"]:invalid::-webkit-datetime-edit-year-field, 
input[type="date"]:invalid::-webkit-datetime-edit-month-field, 
input[type="date"]:invalid::-webkit-datetime-edit-day-field,
input[type="date"]:invalid::-webkit-datetime-edit-text
{
    color: white;
    background-color: white;
    ::placeholder {
        content: " "
    }
}

input[type="date"]::-webkit-inner-spin-button{ 	
    display: none;
}

input[type="date"]::-webkit-calendar-picker-indicator {
	position: absolute;
	top: 50%;
	transform: translateY(-50%);
	right: 1px;
	background-image: url("data:image/svg+xml,%3Csvg width='16' height='16' viewBox='0 0 16 16' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M14.1762 1.52764H13.7656V1.50352C13.7656 1.10476 13.6076 0.722334 13.3263 0.44037C13.0451 0.158406 12.6636 0 12.2659 0C11.8681 0 11.4866 0.158406 11.2054 0.44037C10.9241 0.722334 10.7661 1.10476 10.7661 1.50352V1.52764H5.42476V1.50352C5.42476 1.10476 5.26675 0.722334 4.9855 0.44037C4.70424 0.158406 4.32277 0 3.92501 0C3.52725 0 3.14579 0.158406 2.86453 0.44037C2.58327 0.722334 2.42526 1.10476 2.42526 1.50352V1.52764H1.82376C1.34046 1.52891 0.877316 1.72195 0.53557 2.06455C0.193824 2.40716 0.00127018 2.87146 0 3.35598V14.1717C0.0016909 14.656 0.194379 15.1201 0.536035 15.4626C0.87769 15.8051 1.34059 15.9983 1.82376 16H14.1746C14.6581 15.9987 15.1214 15.8057 15.4634 15.4632C15.8054 15.1206 15.9983 14.6563 16 14.1717V3.35598C15.9987 2.87146 15.8062 2.40716 15.4644 2.06455C15.1227 1.72195 14.6595 1.52891 14.1762 1.52764ZM11.8889 1.50352C11.8889 1.4033 11.9286 1.30718 11.9993 1.23631C12.07 1.16544 12.1659 1.12563 12.2659 1.12563C12.3658 1.12563 12.4617 1.16544 12.5324 1.23631C12.6031 1.30718 12.6428 1.4033 12.6428 1.50352V2.99899C12.6428 3.09922 12.6031 3.19534 12.5324 3.2662C12.4617 3.33707 12.3658 3.37688 12.2659 3.37688C12.1659 3.37688 12.07 3.33707 11.9993 3.2662C11.9286 3.19534 11.8889 3.09922 11.8889 2.99899V1.50352ZM3.54807 1.50352C3.54807 1.4033 3.58778 1.30718 3.65847 1.23631C3.72916 1.16544 3.82504 1.12563 3.92501 1.12563C4.02498 1.12563 4.12086 1.16544 4.19155 1.23631C4.26224 1.30718 4.30195 1.4033 4.30195 1.50352V2.99899C4.30195 3.09922 4.26224 3.19534 4.19155 3.2662C4.12086 3.33707 4.02498 3.37688 3.92501 3.37688C3.82504 3.37688 3.72916 3.33707 3.65847 3.2662C3.58778 3.19534 3.54807 3.09922 3.54807 2.99899V1.50352ZM14.8772 14.1717C14.8747 14.3573 14.8001 14.5345 14.6691 14.6658C14.5382 14.797 14.3614 14.8719 14.1762 14.8744H1.82536C1.63995 14.8723 1.4627 14.7976 1.33144 14.6663C1.20018 14.5351 1.12531 14.3575 1.12281 14.1717V6.59296H14.8772V14.1717Z' fill='%23167CAC'/%3E%3Cpath d='M2.33063 11.282H3.93464V12.6006C3.93464 12.7499 3.99379 12.8931 4.09907 12.9986C4.20435 13.1041 4.34715 13.1634 4.49604 13.1634C4.64494 13.1634 4.78773 13.1041 4.89301 12.9986C4.9983 12.8931 5.05745 12.7499 5.05745 12.6006V11.282H7.46346V12.6006C7.46346 12.7499 7.52261 12.8931 7.62789 12.9986C7.73318 13.1041 7.87597 13.1634 8.02486 13.1634C8.17376 13.1634 8.31655 13.1041 8.42184 12.9986C8.52712 12.8931 8.58627 12.7499 8.58627 12.6006V11.282H10.9923V12.6006C10.9923 12.7499 11.0514 12.8931 11.1567 12.9986C11.262 13.1041 11.4048 13.1634 11.5537 13.1634C11.7026 13.1634 11.8454 13.1041 11.9507 12.9986C12.0559 12.8931 12.1151 12.7499 12.1151 12.6006V11.282H13.7191C13.868 11.282 14.0108 11.2227 14.1161 11.1172C14.2214 11.0116 14.2805 10.8685 14.2805 10.7192C14.2805 10.57 14.2214 10.4268 14.1161 10.3213C14.0108 10.2157 13.868 10.1564 13.7191 10.1564H12.1151V8.84425C12.1151 8.69498 12.0559 8.55183 11.9507 8.44628C11.8454 8.34073 11.7026 8.28143 11.5537 8.28143C11.4048 8.28143 11.262 8.34073 11.1567 8.44628C11.0514 8.55183 10.9923 8.69498 10.9923 8.84425V10.1628H8.58627V8.84425C8.58627 8.69498 8.52712 8.55183 8.42184 8.44628C8.31655 8.34073 8.17376 8.28143 8.02486 8.28143C7.87597 8.28143 7.73318 8.34073 7.62789 8.44628C7.52261 8.55183 7.46346 8.69498 7.46346 8.84425V10.1628H5.05745V8.84425C5.05745 8.69498 4.9983 8.55183 4.89301 8.44628C4.78773 8.34073 4.64494 8.28143 4.49604 8.28143C4.34715 8.28143 4.20435 8.34073 4.09907 8.44628C3.99379 8.55183 3.93464 8.69498 3.93464 8.84425V10.1628H2.33063C2.18174 10.1628 2.03894 10.2221 1.93366 10.3277C1.82837 10.4332 1.76923 10.5764 1.76923 10.7257C1.76923 10.8749 1.82837 11.0181 1.93366 11.1236C2.03894 11.2292 2.18174 11.2885 2.33063 11.2885V11.282Z' fill='%23167CAC'/%3E%3C/svg%3E");
	background-repeat: no-repeat;
	background-position: center;
	width: 16px; // check this
	height: 16px;
	display: flex;
	border-radius: 0 7px 7px 0;
	background-color: #E4F1F7;
	&:hover {
		cursor: pointer;
	}
    padding: 15px 0;
    min-width: 48px;
}

.textbox-question {
    label {
        color: $black;
        font-weight: 500;
    }
    .input-wrapper {
        position: relative;
        &.has-search-icon {
            input[type="text"] {
                border-radius: 50rem;
            }
            button[type="submit"] {
                position: absolute;
                top: 50%;
                transform: translateY(-50%);
                right: 0;
                background-image: url("data:image/svg+xml,%3Csvg width='16' height='16' viewBox='0 0 16 16' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M15.7817 14.7328L11.8252 10.7762C12.8833 9.45005 13.3936 7.76911 13.2513 6.07849C13.1091 4.38788 12.325 2.81587 11.0601 1.6852C9.79515 0.554524 8.14538 -0.0490261 6.44946 -0.00154744C4.75353 0.0459312 3.14012 0.740836 1.94045 1.94051C0.740775 3.14018 0.0458701 4.75359 -0.00160848 6.44952C-0.0490871 8.14545 0.554463 9.79521 1.68514 11.0601C2.81581 12.325 4.38782 13.1091 6.07843 13.2514C7.76905 13.3937 9.44999 12.8834 10.7762 11.8252L14.7349 15.7839C14.8044 15.8527 14.8869 15.907 14.9774 15.9439C15.068 15.9808 15.165 15.9995 15.2628 15.9989C15.3606 15.9983 15.4573 15.9784 15.5475 15.9405C15.6376 15.9025 15.7194 15.8471 15.7881 15.7776C15.8568 15.708 15.9112 15.6256 15.9481 15.535C15.985 15.4444 16.0036 15.3474 16.0031 15.2496C16.0025 15.1518 15.9826 15.0551 15.9446 14.965C15.9067 14.8748 15.8513 14.7931 15.7817 14.7243V14.7328ZM6.63737 11.7913C5.61803 11.7913 4.62157 11.4891 3.77402 10.9228C2.92646 10.3564 2.26587 9.5515 1.87578 8.60975C1.4857 7.668 1.38363 6.63172 1.5825 5.63196C1.78136 4.6322 2.27222 3.71386 2.99301 2.99307C3.7138 2.27229 4.63214 1.78142 5.6319 1.58256C6.63166 1.38369 7.66793 1.48576 8.60969 1.87585C9.55144 2.26593 10.3564 2.92652 10.9227 3.77408C11.489 4.62163 11.7913 5.61809 11.7913 6.63743C11.7896 8.00382 11.2461 9.31376 10.2799 10.2799C9.3137 11.2461 8.00376 11.7897 6.63737 11.7913Z' fill='%231574A1'/%3E%3C/svg%3E%0A");
                background-repeat: no-repeat;
                background-position: center;
                border-radius: 0 50rem 50rem 0;
                background-color: $blue-100;
                width: 2.75rem;
                height: 100%;
                border: 1px solid $gray-500;
                border-left: none;
                display: flex;
            }
        }
        
    }
    input {
        &.has-icon {
            background-image: url(~@/assets/img/icons/location-pin.svg);
            background-repeat: no-repeat;
            background-position: 0.75rem 50%;
            background-size: 1rem auto;
            padding: 0 0.75rem 0 2.5rem;
            &.icon-right {
                background-position: calc(100% - 0.75rem) 50%;
                padding: 0 2.5rem 0 0.75rem;
            }
        }
    }
    .form-label {
        margin-bottom: 0.25rem;
    }
    .form-control {
        border: 1px solid $gray-500;
        border-radius: 0.5rem;
        min-height: 3rem;
        max-height: 48px;
        padding: 12px 16px;
        &::placeholder {
            color: $gray-500;
        }
        &:focus {
            box-shadow: 0 0 0 2.5px $blue;
        }
        &:disabled,
        &.disabled {
            background-color: $gray-100;
            &:hover {
                box-shadow: 0 0 0 4px transparent;
                border: 1px solid $gray-500;
            }
        }
        &:hover {
            border: 1px solid $gray-500;
            box-shadow: 0 0 0 4px $blue-300;
        }
    }
    p {
        display: none;
    }
}
</style>
