<template>
  <div class="app-container">
    <div style="width: 50%">
      <ExpertForm
        v-model="form"
        :showTitle="true"
        :formType="formType"
        :labelWidth="140"
        ref="expertFormRef"
      />
    </div>
    <el-button @click="cancel">取消</el-button>
    <el-button type="primary" @click="submitForm">提交</el-button>
  </div>
</template>

<script setup name="EnterpriseAdd">
import ExpertForm from "@/views/components/ExpertForm";
import tab from "@/plugins/tab";
import {
  companyAdd,
  companyDetail,
  companyEdit,
} from "@/api/dataList/enterprise";
import { reactive, toRefs } from "vue";
import { useRoute, useRouter } from "vue-router";
import { ElMessage } from "element-plus";
const router = useRouter();
const route = useRoute();
const formType = ref(2);
const expertFormRef = ref(null);
const data = reactive({
  form: {
    image: undefined,
    name: undefined,
    tenantId: undefined,
    province: undefined, // 省code
    city: undefined, // 市code
    district: undefined, // 区code
    address: undefined, // 详细地址
    product: undefined,
    kind: undefined,
    code: undefined,
    inviterCode: undefined,
    url: undefined,
    industrys: [],
    industry: "",
    keywords: [],
    directions: [],
    introduce: undefined,
    license: undefined,
    workTime: "",
  },
});
const { form } = toRefs(data);
const cancel = () => {
  router.back();
  tab.closeOpenPage();
};
const submitForm = async () => {
  const valid = await expertFormRef.value.validateForm();
  if (valid) {
    if (form.value.id != undefined) {
      await companyEdit(form.value);
      cancel();
      ElMessage.success("修改成功");
    } else {
      await companyAdd(form.value);
      cancel();
      ElMessage.success("新增成功");
    }
  } else {
    console.log("校验未通过");
  }
};

const getDetailById = async () => {
  if (route.query.id) {
    const { data } = await companyDetail(route.query.id);
    form.value = data;
  }
};
getDetailById();
</script>

<!-- <script>
export default {
  components: {
    ExpertForm,
  },
  data() {
    return {
      // formType: 2,
      // labelWidth: 140,
    };
  },
  methods: {},
  created() {
    const { id } = this.$route.query;
    if (id) {
      companyDetail({ id }).then((res) => {
        form.value = res.data;
      });
    }
  },
};
</script> -->
