<template>
  <el-dialog v-model="props.modelValue" 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_name" :value="item.i18n_lang_type_guid"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label-width="labelWidth" label="语言key" prop="i18n_lang_key">
            <el-input v-model='formData.i18n_lang_key' type="text" placeholder='请输入语言key'></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label-width="labelWidth" label="语言name" prop="i18n_lang_name">
            <el-input v-model='formData.i18n_lang_name' type="textarea" :rows="5" placeholder='请输入语言name'></el-input>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleEditClick(formRef)" :loading="isBtnLod">保存</el-button>
        <el-button @click="handleResetClick(formRef)">重置</el-button>
        <el-button @click="closeDialog()">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

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

// --业务参数
let i18n_lang_type = ref([{}]);


// --业务方法
async function get_i18n_lang_type() {
  await getI18nLangTypeList().then((res) => {
    i18n_lang_type.value = res.data
  })
}
get_i18n_lang_type()





// --基础参数
const store = useLoginStore();
const headers = {
  Accept: "application/json",
  ...store.headers,
};
const isBtnLod = ref(false);
const formRef = ref();
const labelWidth = 100;
const props = defineProps({
  modelValue: Boolean,
  data: Object,
  done: Function,
});
const emits = defineEmits(["update:modelValue"]);
const formData = ref({
  ...props.data,
});
const uoloadData = ref({
  dirName: "I18nLang"
})


// --基础方法
watch(props, (v) => {
  formData.value = v.data;


});

// 打开弹窗时执行
const openDialog = () => {

};

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

const rules = reactive({
  i18n_lang_type_guid: [
    {
      required: true,
      message: '语言类型guid不能为空'
    }
  ],
  i18n_lang_key: [
    {
      required: true,
      message: '语言key不能为空'
    }
  ],
  i18n_lang_name: [
    {
      required: true,
      message: '语言name不能为空'
    }
  ],

});

const handleEditClick = async (formEl) => {
  console.log(formData.value);
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }
    isBtnLod.value = true;


    const { code } = await editI18nLang(formData.value);
    if (code == 0) {
      closeDialog();
      props.done();
    }
    isBtnLod.value = false;
  });
};
const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<style lang="less" scoped></style>
