<template>
  <el-dialog width="500px" append-to-body :title="isEdit ? '修改数据字典' : '新增数据字典'" v-model="visible" @close="handleClose">
    <el-form :model="formData" ref="formRef" label-width="120px" :rules="formRules">
      <el-form-item label="字典归属" prop="dataDictVest">
        <el-select
              v-model="formData.dataDictVest"
              placeholder="请选择字典归属"
              maxlength="30"
              @change="changeCodeType"
            >
              <el-option
                v-for="dict in std_code_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="来源系统" prop="sysId" v-show="showSys">
        <el-select
              v-model="formData.srcSys"
              placeholder="请选择来源系统"
              maxlength="30"
              @change="dataChange"
            >
              <el-option
                v-for="dict in dbResourceOldList"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              ></el-option>
            </el-select>  
        </el-form-item>
        <el-form-item label="数据字典类型" prop="dictType">
        <el-select v-model="formData.dictType" placeholder="请选择数据字典类型">
          <el-option label="基础数据" value="0" />
          <el-option label="指标数据" value="1" />
        </el-select>    
        </el-form-item>
      <el-form-item label="数据字典编号" prop="dataDictNo">
        <el-input v-model="formData.dataDictNo" :disabled=isEdit placeholder="请输入数据字典编号" clearable />
      </el-form-item>
      <el-form-item label="字典英文名" prop="dataDictEngName">
        <el-input v-model="formData.dataDictEngName" placeholder="请输入字典英文名" clearable />
      </el-form-item>
      <el-form-item label="字典中文名" prop="dataDictCnName">
        <el-input v-model="formData.dataDictCnName" placeholder="请输入字典中文名" clearable />
      </el-form-item>
      <el-form-item label="字典业务定义" prop="dataDictBusiMean">
        <el-input v-model="formData.dataDictBusiMean" placeholder="请输入业务定义" clearable />
      </el-form-item>
      <el-form-item label="数据类型" prop="dataDictDataType">
        <el-input v-model="formData.dataDictDataType" placeholder="请输入数据类型" clearable />
      </el-form-item>
      <el-form-item label="数据标准" prop="dataStdNo">
        <el-select v-model="formData.dataStdNo" placeholder="数据标准">
          <el-option
                v-for="dict in stdList"
                :key="dict.onum"
                :label="dict.dataStdNo+'-'+dict.dataStdEngName+'-'+dict.dataStdCnName"
                :value="dict.dataStdNo"
              ></el-option>
        </el-select>  
      </el-form-item>
      

         <!-- 底部按钮区域 -->
         <el-row justify="center" style="margin-top: 20px;">
        <el-col :span="4" style="padding-right: 10px;">
          <el-button @click="handleClose" style="width: 100%" plain>取消</el-button>
        </el-col>
        <el-col :span="4" style="padding-left: 10px;">
          <el-button type="primary" @click="handleSubmit" style="width: 100%">保存</el-button>
        </el-col>
      </el-row>
  

    </el-form>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue';
import { updateStdDict, addStdDict } from '@/api/datastd/std'; // 确保接口存在
import {  listUser,  deptTreeSelect } from "@/api/system/user";
const { proxy } = getCurrentInstance();
const { std_code_status,std_code_appr } = proxy.useDict("std_code_status","std_code_appr");

const props = defineProps({
  data: {
    type: Object,
    required: false,
  },
  stdList: {
    type: Object,
    required: false,
  },
  isEdit: {
    type: Boolean,
  },
  dbResourceOldList: {
    type: Object,
  },
  visible: {
    type: Boolean,
  },
});
// 定义表单校验规则
const formRules = ref({
  dataDictVest: [{ required: true, message: '请选择字典归属', trigger: 'blur' }],
  dictType: [{ required: true, message: '请选择数据字典类型', trigger: 'blur' }],
  dataStdBusiOwnershipDept: [{ required: true, message: '请选择业务认责部门', trigger: 'blur' }],
  dataStdItOwnershipDept: [{ required: true, message: '请选择技术认责部门', trigger: 'blur' }],
  dataDictNo: [{ required: true, message: '请输入数据字典编号', trigger: 'blur' }],
  dataStdBusiOwnershipPrsn: [{ required: true, message: '请输入业务认责人', trigger: 'blur' }],
  dataStdItOwnershipPrsn: [{ required: true, message: '请输入技术认责人', trigger: 'blur' }],
});
const showSys = ref(false);
const emit = defineEmits(['update:visible', 'refresh']);
const userList = ref([]);
const formData = ref({ ...props.data });
const visible = ref(props.visible);  // 使用 prop 传入的 visible
function dataChange(data) {
  // 从 dbResourceOldList 中查找对应的 name
  const selectedItem = props.dbResourceOldList.find(item => item.id === data);
  if (selectedItem) {
    // 如果找到对应的项，将 name 赋值给 formData.value.dbRName
    formData.value.sysName = selectedItem.name;
  } else {
    // 如果未找到，清空 formData.value.dbRName
    formData.value.sysName = '';
  }
}
// 监听 props.visible 的变化，确保同步到 local visible
watch(() => props.visible, (newVal) => {
  visible.value = newVal;
  formData.value = { ...props.data }
});

function getList() {
  listUser({pageSize:1000,pageNum:1}).then(res => {
    userList.value = res.rows;
  });
};
const handleClose = () => {
  emit('update:visible', false);  // 关闭弹框
};
const deptOptions = ref(undefined);

function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
};
function changeCodeType(data) {
  if (data != "company") {
    showSys.value = true;
    formData.value.sysName = "";
    formData.value.srcSys = undefined;
  } else {
    showSys.value = false;
    formData.value.sysName = "公司级";
    formData.value.srcSys = 10000;
  }
}
getDeptTree();
getList();
const handleSubmit = () => {
  const formRef = proxy.$refs.formRef;
  formRef.validate((valid) => {
    if (valid) {
      const apiCall = props.isEdit ? updateStdDict(formData.value) : addStdDict(formData.value);
      apiCall
        .then(() => {
          emit('refresh');
          handleClose();
        })
        .catch((error) => {
          console.error(error);
        });
    } else {
      console.log('表单验证失败');
      return false;
    }
  });
};
</script>
