<template>
  <div class="app-container">
    <el-card shadow="always" style="width: 55%; margin: 0 auto">
      <p><b>基本资料</b></p>
      <el-form
        ref="personalInfoFormRef"
        :model="PersonalInfoForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="姓名:" prop="nickName">
          <el-input
            v-model="PersonalInfoForm.nickName"
            placeholder="请输入姓名"
          />
        </el-form-item>
        <el-form-item label="手机:" prop="mobile">
          <el-input
            v-model="PersonalInfoForm.mobile"
            placeholder="请输入手机号"
          />
        </el-form-item>
        <el-form-item label="邮箱:" prop="email">
          <el-input v-model="PersonalInfoForm.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="职务:" prop="post">
          <el-input v-model="PersonalInfoForm.post" placeholder="请输入职务" />
        </el-form-item>
        <el-form-item label="固定电话:" prop="phone">
          <el-input
            v-model="PersonalInfoForm.phone"
            placeholder="请输入固定电话"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitPersonalInfo">提交</el-button>
        </el-form-item>
      </el-form>
      <p><b>专家资料</b></p>
      <ExpertForm
        v-model="expertForm"
        :isAdd="false"
        :labelWidth="labelWidth"
        ref="expertFormRef"
      />
      <div :style="{ marginLeft: labelWidth + 'px' }">
        <el-button type="primary" @click="submitExpertForm">提交</el-button>
      </div>
    </el-card>
  </div>
</template>
<script setup>
// import { expert } from "@/api/identity/index";
import { getInfo } from "@/api/login";
import { insertClientUser } from "@/api/admin/enterprise";
import { updateExpert } from "@/api/admin/expert/account";
import ExpertForm from "@/views/components/ExpertForm";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
const data = reactive({
  PersonalInfoForm: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    postCode: undefined,
  },
  expertForm: {
    gender: 1,
  },
  rules: {
    name: [{ required: true, message: "公司名称不能为空", trigger: "blur" }],
    dutyParagraph: [
      { required: true, message: "公司税号不能为空", trigger: "blur" },
    ],
    bank: [{ required: true, message: "开户行不能为空", trigger: "blur" }],
    bankAccount: [
      { required: true, message: "开户行账号不能为空", trigger: "blur" },
    ],
    bankPhone: [
      { required: true, message: "开户行电话不能为空", trigger: "blur" },
    ],
    email: [{ required: true, message: "电子邮箱不能为空", trigger: "blur" }],
    phone: [{ required: true, message: "联系电话不能为空", trigger: "blur" }],
    address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
    username: [{ required: true, message: "联系人不能为空", trigger: "blur" }],
    userPhone: [
      { required: true, message: "联系人电话不能为空", trigger: "blur" },
    ],
    userAddress: [
      { required: true, message: "邮寄地址不能为空", trigger: "blur" },
    ],
  },
});

const { expertForm, PersonalInfoForm, rules } = toRefs(data);

const router = useRouter();
const expertFormRef = ref();
const labelWidth = 140;

// 获取基础信息用于回显
const getBasicInfo = async () => {
  const { data } = await getInfo();
  PersonalInfoForm.value = data.user ?? {};
  expertForm.value = data.enterprise ?? {};
};
getBasicInfo();

const form = reactive({
  // laboratory_id: undefined, // 所属实验室
  gender: 1,
});
const personalInfoFormRef = ref();

// 更新个人信息
const submitPersonalInfo = async () => {
  try {
    await personalInfoFormRef.value.validate();
    await insertClientUser(PersonalInfoForm.value);
    ElMessage.success("更新个人信息成功");
    const { data } = await getInfo();

    PersonalInfoForm.value = data.user ?? {};
  } catch (error) {
    console.log(error);
  }
};

async function submitExpertForm(status) {
  const valid = await expertFormRef.value.validateForm();
  if (valid) {
    updateExpert(expertForm.value).then((res) => {
      ElMessage.success("新增成功");
      router.back();
    });
  } else {
    console.log("校验未通过");
  }
}
</script>
