<template>
  <div id="info_create">
    <admin-frame topType="2" @back="back" :rightArrow="false" :usePage="false" :useNav="false">
      <template v-slot:info>
        <detail-1 :form="form" @onSubmit="onSubmit"></detail-1>
      </template>
    </admin-frame>
  </div>
</template>

<script>
import detail1 from './info/detail-1.vue';
import adminFrame from '@frame/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: patentinfo } = createNamespacedHelpers('patentinfo');
export default {
  name: 'info_create',
  props: {},
  components: {
    adminFrame,
    detail1,
  },
  data: function () {
    return {
      form: { inventor: [] },
    };
  },
  async created() {
    if (this.id) await this.search();
  },
  methods: {
    ...patentinfo(['fetch', 'create', 'update']),
    async search() {
      let res = await this.fetch(this.id);
      if (this.$checkRes(res)) {
        this.$set(this, `form`, res.data);
      }
    },
    async onSubmit() {
      let data = this.form;
      if (data.id) {
        let res = await this.update(data);
        if (this.$checkRes(res)) {
          this.$toast({ type: `success`, message: `修改成功` });
          this.back();
        } else {
          this.$toast({ type: `fail`, message: `${res.errmsg}` });
        }
      } else {
        data.trans_status = '0';
        let res = await this.create(data);
        if (this.$checkRes(res)) {
          this.$toast({ type: `success`, message: `添加成功` });
          this.back();
        } else {
          this.$toast({ type: `fail`, message: `${res.errmsg}` });
        }
      }
    },
    // 返回
    back() {
      this.$router.push({ path: '/patent/admin/patent/info' });
    },
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped></style>
