<template>
    <a-modal
        :open="visible"
        :closable="false"
        :centered="true"
        :title="pageTitle"
        @ok="onSubmit"
    >
        <a-form layout="vertical">
            <a-row :gutter="16">
                <a-col :xs="24" :sm="24" :md="24" :lg="24">
                    <a-form-item
                        :label="$t('website_language.name')"
                        name="name"
                        :help="rules.name ? rules.name.message : null"
                        :validateStatus="rules.name ? 'error' : null"
                        class="required"
                    >
                        <a-input-group compact style="display: flex">
                            <a-select
                                v-model:value="formData.flag_code"
                                style="width: 80px"
                                show-search
                                :filter-option="filterCountryOption"
                            >
                                <a-select-option
                                    v-for="country in countryOptions"
                                    :key="country.code"
                                    :value="country.code"
                                    :label="country.label"
                                    :filterLabel="country.filterLabel"
                                >
                                    {{ country.label }}
                                </a-select-option>
                            </a-select>
                            <a-input
                                v-model:value="formData.name"
                                :placeholder="
                                    $t('common.placeholder_default_text', [$t('website_language.name')])
                                "
                                style="flex: 1"
                            />
                        </a-input-group>
                    </a-form-item>
                </a-col>
            </a-row>
            <a-row :gutter="16">
                <a-col :xs="24" :sm="24" :md="12" :lg="12">
                    <a-form-item
                        :label="$t('website_language.is_rtl')"
                        name="is_rtl"
                        :help="rules.is_rtl ? rules.is_rtl.message : null"
                        :validateStatus="rules.is_rtl ? 'error' : null"
                    >
                        <a-switch
                            v-model:checked="formData.is_rtl"
                            :checkedValue="true"
                            :unCheckedValue="false"
                        />
                        <span style="margin-left: 10px">
                            {{ formData.is_rtl ? $t('website_language.rtl') : $t('website_language.ltr') }}
                        </span>
                    </a-form-item>
                </a-col>
                <a-col :xs="24" :sm="24" :md="12" :lg="12">
                    <a-form-item
                        :label="$t('website_language.enabled')"
                        name="enabled"
                    >
                        <a-switch
                            v-model:checked="formData.enabled"
                            :checkedValue="true"
                            :unCheckedValue="false"
                        />
                    </a-form-item>
                </a-col>
            </a-row>
        </a-form>
        <template #footer>
            <a-button key="submit" type="primary" :loading="loading" @click="onSubmit">
                <template #icon>
                    <SaveOutlined />
                </template>
                {{ addEditType == "add" ? $t("common.create") : $t("common.update") }}
            </a-button>
            <a-button key="back" @click="onClose">
                {{ $t("common.cancel") }}
            </a-button>
        </template>
    </a-modal>
</template>
<script setup>
import { computed, watch } from "vue";
import { SaveOutlined } from "@ant-design/icons-vue";
import { countryCodes, getFlagEmoji, filterCountryOption, detectUserCountry, getLanguageInfoFromCountry } from "../../../common/scripts/countryCodes";

const props = defineProps([
    "formData",
    "data",
    "visible",
    "url",
    "addEditType",
    "pageTitle",
    "successMessage",
]);

const emit = defineEmits(["addEditSuccess", "closed"]);

const { addEditRequestAdmin, loading, rules } = window.__composables.apiAdmin();

const countryOptions = computed(() => {
    return countryCodes.map((country) => ({
        code: country.code,
        label: getFlagEmoji(country.code),
        filterLabel: `${country.name} ${country.code}`,
    }));
});

// Set default flag_code based on user's country when adding new language
watch(
    () => props.visible,
    (newVal) => {
        if (newVal && props.addEditType === "add" && !props.formData.flag_code) {
            const defaultCountry = detectUserCountry();
            const langInfo = getLanguageInfoFromCountry(defaultCountry);
            props.formData.flag_code = defaultCountry;
            props.formData.key = langInfo.key;
            props.formData.name = langInfo.name;
            props.formData.is_rtl = langInfo.is_rtl;
        }
    }
);

// Update key and name when flag_code changes
watch(
    () => props.formData.flag_code,
    (newVal) => {
        if (newVal && props.addEditType === "add") {
            const langInfo = getLanguageInfoFromCountry(newVal);
            props.formData.key = langInfo.key;
            props.formData.name = langInfo.name;
            props.formData.is_rtl = langInfo.is_rtl;
        }
    }
);

const onSubmit = () => {
    addEditRequestAdmin({
        url: props.url,
        data: props.formData,
        successMessage: props.successMessage,
        success: (res) => {
            emit("addEditSuccess", res.xid);
        },
    });
};

const onClose = () => {
    rules.value = {};
    emit("closed");
};
</script>
