<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 :lg="12">
          <el-form-item :label-width="labelWidth" label="产品类型" prop="product_type_guid">
            <el-cascader class="w100" filterable :options="dataList"
              :props="{ checkStrictly: true, value: 'product_type_guid', label: 'product_type_name', emitPath: false }"
              placeholder="请选择产品类型" clearable v-model="formData.product_type_guid">
              <template #default="{ node, data }">
                <span>{{ data.product_type_name }}</span>
                <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
              </template>
            </el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label-width="labelWidth" label="语言类型" prop="i18n_lang_type">
            <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-row>
        <el-col :span="24">
          <el-form-item :label-width="labelWidth + 10" label="多语言名称" prop="product_type_lang_name">
            <el-input v-model='formData.product_type_lang_name' type="text" placeholder='请输入产品类型多语言名称'></el-input>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="handleAddClick(formRef)">添加</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 { addProductTypeLang } from "~/service/product_type_lang";
import { useLoginStore } from "~/store";
import { getProductTypeTree } from "~/service/product_type";
import { getI18nLangTypeList } from '~/service/i18n_lang_type';

// --业务参数
const dataList = ref();
const i18n_lang_type = ref();


// --业务方法
async function getList() {
  getProductTypeTree().then((res) => {
    if (res.code == 0) {
      dataList.value = res.data
    }
  })
}

async function geti18n_lang_typeList() {
  getI18nLangTypeList({ limit: 9999 }).then((res) => {
    if (res.code == 0) {
      i18n_lang_type.value = res.data
    }
  })
}


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

const isBtnLod = ref(false);
const formRef = ref();
const labelWidth = 90;
const props = defineProps({
  modelValue: Boolean,
  done: Function,
});
const emits = defineEmits(["update:modelValue"]);
const dialogVisible = ref(props.modelValue);
const formData = reactive({});

const uoloadData = ref({
  dirName: "ProductTypeLang"
})

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

const rules = reactive({
  product_type_guid: [
    {
      required: true,
      message: '产品类型不能为空'
    }
  ],
  i18n_lang_type_guid: [
    {
      required: true,
      message: '语言类型不能为空'
    }
  ],
  product_type_lang_name: [
    {
      required: true,
      message: '产品类型多语言名称不能为空'
    }
  ],

});


// --基础方法

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

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

const handleAddClick = async (formEl) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (!valid) {
      return;
    }
    isBtnLod.value = true;


    const { code } = await addProductTypeLang(formData);
    if (code == 0) {
      closeDialog();
      props.done();
    }
    isBtnLod.value = flase;
  });
};

const handleResetClick = async (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

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