<template>
  <div class="container">
    <n-form ref="formRef" label-placement="left" :label-width="100" :model="form" :rules="rules" size="large" require-mark-placement="left">
      <n-form-item label="公司名称：" path="companyName">
        <n-input v-model:value="form.companyName" placeholder="请输入公司名称" />
      </n-form-item>
      <n-form-item label="公司职务：" path="addr">
        <n-input v-model:value="form.addr" placeholder="请输入公司职务" />
      </n-form-item>
      <n-form-item label="邮箱：" path="contact">
        <n-input v-model:value="form.contact" placeholder="请输入邮箱" />
      </n-form-item>
      <n-form-item label="联系电话：" path="contact">
        <n-input v-model:value="form.contact" placeholder="请输入联系电话" />
      </n-form-item>
      <n-form-item label="联系人：" path="name">
        <n-input v-model:value="form.name" placeholder="请输入联系人" />
      </n-form-item>
      <n-form-item label="描述：" path="demand">
        <n-input v-model:value="form.demand" type="textarea" placeholder="请输入描述" />
      </n-form-item>
    </n-form>
    <div class="btns">
      <n-button class="login-btn" attr-type="button" type="info" color="#E6E6E6" @click="handleClose">
        取消
      </n-button>
      <n-button class="login-btn" attr-type="button" type="info" color="#18A058" @click="handleSure">
        确认
      </n-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {
  NForm,
  NFormItem,
  NInput,
  NButton,
  FormRules,
  FormItemRule,
  createDiscreteApi,
} from "naive-ui";
import { ref, reactive } from "vue";

const emit = defineEmits(["closeToolsDialog"]);

const message = createDiscreteApi(["message"]);

const formRef = ref();

const form: register = reactive({
  name: "",
  contact: "",
  email: "",
  companyName: "",
  demand: "",
  addr: "",
});

const rules: FormRules = {
  name: [
    {
      required: true,
      message: "请输入联系人",
      trigger: ["input", "blur"],
    },
  ],
  contact: [
    {
      required: true,
      message: "请输入联系电话",
      trigger: ["input", "blur"],
    },
  ],
  email: [
    {
      required: true,
      message: "请输入邮箱",
      trigger: ["input", "blur"],
    },
  ],
  companyName: [
    {
      required: true,
      message: "请输入公司名称",
      trigger: ["input", "blur"],
    },
  ],
  addr: [
    {
      required: true,
      message: "请输入公司职务",
      trigger: ["input", "blur"],
    },
  ],
  demand: [
    {
      required: true,
      message: "请输入需求",
      trigger: ["input", "blur"],
    },
  ],
};

const handleClose = () => {
  emit("closeToolsDialog", "success");
};

const handleSure = () => {
  formRef.value?.validate(async (errors: any) => {
    if (!errors) {
      const { code } = await register_Api(form);
      if (code === 200) {
        message.message.success("注册成功");
        emit("closeToolsDialog", "success");
      }
    }
  });
};
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  padding: 30px 20px;

  .btns {
    display: flex;
    justify-content: center;
  }

  .login-btn {
    display: block;
    width: 120px;
    height: 50px;
    color: #fff;
    font-size: 18px;
    border-radius: 8px;
    margin-right: 17px;

    :deep(.n-button__content) {
      display: block;
    }
  }
}
</style>
