<script setup lang="ts">
import { ref, watch } from "vue";
import { message } from "@/utils/message";
import { FormInstance } from "element-plus";

// const SELECT_OPTIONS = [
//   { label: "网关", value: 1 },
//   { label: "人工智能", value: 2 },
//   { label: "CVM", value: 3 },
//   { label: "防火墙", value: 4 },
//   { label: "未知", value: 5 }
// ];

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  data: {
    type: Object,
    default: () => {
      return {};
    }
  }
});

const ruleFormRef = ref<FormInstance>();

const formVisible = ref(false);
const formData = ref(props.data);
// const textareaValue = ref("");

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(valid => {
    if (valid) {
      const data = formData;
      console.log(data);

      message("提交成功", { type: "success" });
      emit("submit-form", data);
      formVisible.value = false;
      formData.value = {};
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const closeDialog = () => {
  formVisible.value = false;
  resetForm(ruleFormRef.value);
};

const emit = defineEmits(["update:visible", "submit-form"]);
watch(
  () => formVisible.value,
  val => {
    emit("update:visible", val);
  }
);

watch(
  () => props.visible,
  val => {
    formVisible.value = val;
  }
);

watch(
  () => props.data,
  val => {
    formData.value = val;
  }
);

const rules = {
  name: [{ required: true, message: "请输入项目名称", trigger: "blur" }]
};
</script>

<template>
  <el-dialog
    v-model="formVisible"
    title="新建项目"
    :width="560"
    class="project-dialog"
    append-to-body
    style="border-radius: 8px"
    :before-close="closeDialog"
  >
    <!-- 表单内容 -->
    <el-form
      ref="ruleFormRef"
      :model="formData"
      label-position="top"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="项目名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入项目名称" />
      </el-form-item>
      <el-form-item label="项目负责人" prop="leader">
        <el-input v-model="formData.leader" placeholder="请输入项目负责人" />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入联系方式" />
      </el-form-item>
      <!-- <el-form-item label="产品状态" prop="status">
        <el-radio-group v-model="formData.status">
          <el-radio label="0">已停用</el-radio>
          <el-radio label="1">已启用</el-radio>
        </el-radio-group>
      </el-form-item> -->
      <!-- <el-form-item label="项目简介" prop="description">
        <el-input
          v-model="formData.description"
          type="textarea"
          :style="{ width: '480px' }"
          placeholder="请输入项目简介"
        />
      </el-form-item> -->
      <!-- <el-form-item label="产品类型" prop="type">
        <el-select
          v-model="formData.type"
          clearable
          :style="{ width: '480px' }"
        >
          <el-option
            v-for="(item, index) in SELECT_OPTIONS"
            :key="index"
            :value="item.value"
            :label="item.label"
          >
            {{ item.label }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="mark">
        <el-input
          v-model="textareaValue"
          type="textarea"
          :style="{ width: '480px' }"
          placeholder="请输入内容"
        />
      </el-form-item> -->
    </el-form>
    <template #footer>
      <el-button class="btn-color" @click="closeDialog">取消</el-button>
      <el-button
        type="primary"
        color="#1C0D82"
        @click="submitForm(ruleFormRef)"
      >
        确定
      </el-button>
    </template>
  </el-dialog>
</template>
<style lang="scss">
// :deep(.el-dialog, .is-draggable, .el-dialog__header) {
//   border-bottom: 1px solid #eee !important;
// }
.project-dialog {
  .el-dialog__header {
    margin-right: 0;
    border-bottom: 1px solid #e0e0e0;
  }

  .el-dialog__body {
    padding: 30px 24px;
  }

  .btn-color:hover {
    color: #fff;
    background-color: #1c0d82;
    opacity: 0.25;
  }
}
</style>
