<template>
  <div class="app-container">
    <div style="width: 50%">
      <ExpertForm
        v-model="form"
        :showTitle="true"
        :formType="formType"
        :labelWidth="labelWidth"
        ref="expertForm"
      />
      <!-- :isAdd="false" -->
    </div>
    <el-button @click="$router.go(-1)">取消</el-button>
    <el-button type="primary" @click="submitForm">提交</el-button>
  </div>
</template>
<script>
import ExpertForm from "@/views/components/ExpertForm";
import {
  companyAdd,
  companyDetail,
  companyEdit,
} from "@/api/dataList/enterprise";
export default {
  components: {
    ExpertForm,
  },
  data() {
    return {
      formType: 2,
      labelWidth: 140,
      form: {
        image: undefined,
        name: undefined,
        tenant_id: undefined,
        province: undefined, // 省code
        city: undefined, // 市code
        district: undefined, // 区code
        address: undefined, // 详细地址
        product: undefined,
        kind: undefined,
        code: undefined,
        inviter_code: undefined,
        url: undefined,
        industrys: [],
        keywords: [],
        directions: [],
        introduce: undefined,
        license: undefined,
      },
    };
  },
  methods: {
    submitForm() {
      if (this.$refs.expertForm.submitForm()) {
        console.log(this.form);
        // todo...  提交出错
        if (this.form.id != undefined) {
          companyEdit(this.form).then((response) => {
            this.$store.dispatch("tagsView/delView", this.$route);
            this.$router.go(-1);
            this.$modal.msgSuccess("修改成功");
          });
        } else {
          companyAdd(this.form).then((res) => {
            this.$store.dispatch("tagsView/delView", this.$route);
            this.$router.go(-1);
            this.$modal.msgSuccess("新增成功");
          });
        }
      } else {
        console.log("校验未通过");
      }
    },
  },
  created() {
    const { id } = this.$route.query;
    if (id) {
      companyDetail({ id }).then((res) => {
        this.form = res.data;
      });
    }
  },
};
</script>