<template>
  <div class="add">
    <el-dialog
      v-model="dialogVisible"
      :title="isEdit1 ? '修改测试项目' : '新建测试项目'"
      width="500"
      :close-on-click-modal="false"
      @closed="handleClose"
    >
      <el-form
        :label-position="labelPosition"
        label-width="auto"
        ref="formRef"
        :rules="rules"
        v-model:model="formLabelAlign"
        style="max-width: 600px"
      >
        <el-form-item
          label="项目名称"
          :label-position="itemLabelPosition"
          prop="projectName"
        >
          <el-input v-model="formLabelAlign.projectName" />
        </el-form-item>

        <!--        <el-form-item-->
        <!--          label="路线详情"-->
        <!--          :label-position="itemLabelPosition"-->
        <!--          prop="routeData"-->
        <!--        >-->
        <!--          <el-input v-model="formLabelAlign.routeData" />-->
        <!--        </el-form-item>-->
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="submitForm(formRef)">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, watch } from "vue";

const dialogVisible = ref(false);

import type { FormItemProps, FormProps, FormRules } from "element-plus";
// import { addProject, updateProject } from "@/api/project";
import { addTestModule, updateTestModule } from "@/api/project";

const labelPosition = ref<FormProps["labelPosition"]>("right");
const itemLabelPosition = ref<FormItemProps["labelPosition"]>("");

interface VersionItem {
  engineeringId: string;
  projectName: string;
  // routeData: string;
}

const formRef = ref<FormProps>();

const formLabelAlign = ref<VersionItem>({
  engineeringId: "",
  projectName: "",
  // routeData: "",
});
const rules = reactive({
  projectName: [
    { required: true, message: "请输入 项目名称", trigger: "blur" },
  ],
});

const emit = defineEmits(["saveFinish"]);
const handleClose = (done: () => void) => {
  formRef.value && formRef.value.resetFields();
  dialogVisible.value = false;
};

const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      if (isEdit1) {
        await updateTestModule(formLabelAlign.value);
      } else {
        await addTestModule(formLabelAlign.value);
      }
      emit("saveFinish");

      handleClose();
    } else {
      console.log("error submit!", fields);
    }
  });
};

let isEdit1 = false;
const openDialog = (
  isEdit: boolean,
  row: VersionItem,
  currentProjectId: string
) => {
  dialogVisible.value = true;

  formLabelAlign.value.projectName = "";
  formLabelAlign.value.engineeringId = "";

  isEdit1 = isEdit;
  if (isEdit) {
    formLabelAlign.value = row;
  }
  formLabelAlign.value.engineeringId = currentProjectId;
};

defineExpose({ openDialog });
</script>
<style scoped lang="scss">
// .add {
//   // position: fixed;
//   // width: 100%;
//   // height: 100%;
//   // z-index: 10;
//   // background-color: #000;
// }
</style>
