<template>
  <div class="app-container">
    <div style="width: 50%">
      <Agent
        v-model="form"
        :showTitle="true"
        :formType="formType"
        :labelWidth="labelWidth"
        ref="agent"
      />
      <!-- :isAdd="false" -->
    </div>
    <el-button @click="$router.go(-1)">取消</el-button>
    <el-button type="primary" @click="submitForm">提交</el-button>
  </div>
</template>
<script>
import Agent from "@/views/components/Agent";
import { agentAdd, agentDetail, agentEdit } from "@/api/dataList/agent";
export default {
  components: {
    Agent,
  },
  data() {
    return {
      formType: 2,
      labelWidth: 140,
      form: {
        id: undefined,
        name: undefined,
        mobile: undefined,
        id_card: undefined,
        tenant_id: undefined,
        work_experience: undefined, // 工作经验
        work_place: undefined, // 工作地点
        credential_image: undefined,
        keywords: [],
        researchs: [],
        id_image: {
          front: undefined,
          behind: undefined,
          hold: undefined,
        },
      },
    };
  },
  methods: {
    submitForm() {
      if (this.$refs.agent.submitForm()) {
        console.log(this.form);
        return false;
        // todo 不支持添加操作
        if (this.form.id != undefined) {
          agentEdit(this.form).then((response) => {
            this.$store.dispatch("tagsView/delView", this.$route);
            this.$router.go(-1);
            this.$modal.msgSuccess("修改成功");
          });
        } else {
          agentAdd(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) {
      agentDetail({ id }).then((res) => {
        this.form = res.data;
      });
    }
  },
};
</script>