<template>
  <div class="add">
    <el-dialog
      v-model="dialogVisible"
      title="新建测试工程"
      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="engineeringProductName"
        >
          <el-input v-model="formLabelAlign.engineeringProductName" />
        </el-form-item>
        <el-form-item
          label="类型"
          :label-position="itemLabelPosition"
          prop="engineeringType"
        >
          <el-input v-model="formLabelAlign.engineeringType" />
        </el-form-item>
        <el-form-item
          label="版本"
          :label-position="itemLabelPosition"
          prop="engineeringVersion"
        >
          <el-input v-model="formLabelAlign.engineeringVersion" />
        </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";

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

interface VersionItem {
  engineeringProductName: string;
  engineeringType: string;
  engineeringVersion: string;
}

const formRef = ref<FormProps>();

const formLabelAlign = ref<VersionItem>({
  engineeringProductName: "",
  engineeringType: "",
  engineeringVersion: "",
});
const rules = reactive({
  engineeringProductName: [
    { required: true, message: "请输入 产品名称", trigger: "blur" },
  ],
  engineeringType: [
    { required: true, message: "请输入 工程类型", trigger: "blur" },
  ],
  engineeringVersion: [
    { 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 updateProject(formLabelAlign.value);
      } else {
        await addProject(formLabelAlign.value);
      }
      emit("saveFinish");
      handleClose();
    } else {
      console.log("error submit!", fields);
    }
  });
};

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

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