<template>
  <div class="app-container">
    <div style="width: 50%">
      <LaboratoryForm
        v-model="form"
        :showTitle="true"
        :formType="formType"
        :labelWidth="labelWidth"
        ref="laboratoryForm"
      />
      <!-- :isAdd="false" -->
    </div>
    <el-button @click="$router.go(-1)">取消</el-button>
    <el-button type="primary" @click="submitForm">提交</el-button>
  </div>
</template>
<script>
import LaboratoryForm from "@/views/components/LaboratoryForm";
import {
  laboratoryAdd,
  laboratoryDetail,
  laboratoryEdit,
} from "@/api/dataList/laboratory";
export default {
  components: {
    LaboratoryForm,
  },
  data() {
    return {
      formType: 2,
      labelWidth: 140,
      form: {
        id: undefined,
        name: undefined,
        tenant_id: undefined,
        province: undefined, // 省code
        city: undefined, // 市code
        district: undefined, // 区code
        address: undefined, // 详细地址
        code: undefined,
        url: undefined,
        industrys: [],
        keywords: [],
        researchs: [],
        research_id: undefined,
        introduce: undefined,
      },
    };
  },
  methods: {
    submitForm() {
      if (this.$refs.laboratoryForm.submitForm()) {
        console.log(this.form);
        if (this.form.id != undefined) {
          laboratoryEdit(this.form).then((response) => {
            this.$store.dispatch("tagsView/delView", this.$route);
            this.$router.go(-1);
            this.$modal.msgSuccess("修改成功");
          });
        } else {
          laboratoryAdd(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) {
      laboratoryDetail({ id }).then((res) => {
        this.form = res.data;
      });
    }
  },
};
</script>