<template>
  <Dialog v-model="visible" diaTitle="编辑名称" :width="350" @close="closeFun">
    <template #default>
      <el-form :model="formData" labelWidth="auto">
        <el-form-item label="名称" prop="modifyFileName">
          <el-input v-model="formData.modifyFileName" clearable></el-input>
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <div>
        <el-button @click="closeFun">取消</el-button>
        <el-button type="primary" @click="submitFun">确定</el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue';
import Dialog from '@/components/common/dialog/index.vue';
import { editSimulationParameterApi } from '@/api/data/simulationParameterLibrary';
import { ElMessage } from 'element-plus';

interface Props {
  modelValue: boolean;
  data: any;
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  data: {},
});

watch(
  () => props.modelValue,
  (val: boolean) => {
    visible.value = val;
    if (val) {
      formData.value.modifyFileName = props.data.name;
    }
  }
);

const emit = defineEmits(['update:modelValue', 'confirm']);
const visible = ref(false);

const formData = ref({
  modifyFileName: '',
});

const submitFun = () => {
  if (!formData.value.modifyFileName) {
    ElMessage.warning('请输入名称');
    return;
  }
  const params = {
    ...formData.value,
    ...props.data,
  };
  editSimulationParameterApi(params).then((res: any) => {
    if (res.code === 200) {
      ElMessage.success('操作成功');
      closeFun();
      emit('confirm', params);
    }
  });
};

const closeFun = () => {
  emit('update:modelValue', false);
};
</script>
