<template>
    <el-dialog v-model="dialogVisible" title="复制到其他语言" width="900px" @closed="closeDialog" @open="openDialog">

        <el-form ref="formRef" :model="formData" :rules="rules">
            <el-row>
                <el-col :span="12">
                    <el-form-item :label-width="labelWidth" label="语言类型" prop="i18n_lang_type_guid">
                        <el-select v-model="formData.i18n_lang_type_guid" clearable placeholder="请选择">
                            <el-option v-for="item in i18n_lang_type" :key="item.i18n_lang_type_guid"
                                :label="item.i18n_lang_type_code" :value="item.i18n_lang_type_guid"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <template #footer>
            <span class="dialog-footer">
                <el-button type="primary" @click="handleConfirmClick(formRef)">确定</el-button>
                <el-button @click="closeDialog()">取消</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script setup>
import { reactive, ref, watch } from "vue";
import { useLoginStore } from "~/store";
import { getI18nLangTypeList } from '~/service/i18n_lang_type';
import { editAboutUs } from "~/service/about_us";

// 语言类型code
const i18n_lang_type = ref();
async function geti18n_lang_typeList() {
    getI18nLangTypeList({ limit: 9999 }).then((res) => {
        if (res.code == 0) {
            i18n_lang_type.value = res.data.filter(v => v.i18n_lang_type_guid != props.locale)
        }
    })
}

// --基础参数
const store = useLoginStore();
const headers = {
    Accept: "application/json",
    ...store.headers,
};

const formRef = ref();
const labelWidth = 90;
const props = defineProps({
    modelValue: Boolean,
    done: Function,
    locale: String,
    data: Object,

});
const emits = defineEmits(["update:modelValue"]);
const dialogVisible = ref(props.modelValue);
const formData = reactive({
    i18n_lang_type_guid: "",
});


watch(props, (v) => {
    dialogVisible.value = v.modelValue;
});

const rules = reactive({
    i18n_lang_type_guid: [{ required: true, message: "请选择语言类型" }],
})


// 打开弹窗时执行
const openDialog = async () => {
    await geti18n_lang_typeList()
};

const closeDialog = () => {
    dialogVisible.value = false;
    emits("update:modelValue", false);
};

const handleConfirmClick = async (formRef) => {
    if (!formRef) return;
    formRef.validate(async (valid) => {
        if (!valid) {
            return
        }
        // 执行复制操作
        const copy = {
            about_us_profile: props.data.about_us_profile,
            about_us_profile_history: props.data.about_us_profile_history,
            about_us_honor: props.data.about_us_honor,
            about_us_honor_more: props.data.about_us_honor_more,
            about_us_factory: props.data.about_us_factory,
            about_us_exhibition: props.data.about_us_exhibition,
            i18n_lang_type_guid: formData.i18n_lang_type_guid
        }
        // 发起编辑请求
        const { code } = await editAboutUs(copy);
        closeDialog();

        // 调用父组件的 done 方法，参数为复制到的语言类型
        props.done(formData.i18n_lang_type_guid);
    });
};
</script>

<style lang="less" scoped>
.copy-btn-box {
    width: 100%;
    display: flex;
    justify-content: right;
    margin-bottom: 15px;
}
</style>