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

<script>
import form1 from '@/layout/trans/form-1.vue';
import adminFrame from '@frame/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: patenttrans } = createNamespacedHelpers('patenttrans');
var moment = require('moment');
export default {
  name: 'contract',
  props: {},
  components: { adminFrame, form1 },
  data: function () {
    return {
      // 表单
      form: { contract: {} },
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...patenttrans(['fetch', 'update']),
    async search() {
      let res = await this.fetch(this.id);
      if (this.$checkRes(res)) {
        let data = res.data;
        let contract = {
          transfer_id: this.user.id,
          transfer: this.user.phone,
          patent_id: data.patent_id,
          create_number: data.create_number,
          patent_name: data.patent_name,
        };
        if (data.contract == undefined) data.contract = contract;
        this.$set(this, `form`, data);
      }
    },
    // 提交合同
    async onSubmit({ data }) {
      data.status = '2';
      data.is_contract = '1';
      data.on_afterobligee = data.contract.transferee;
      data.transfer_date = moment(new Date()).format('YYYY-MM-DD');
      let res = await this.update(data);
      if (this.$checkRes(res)) {
        this.$toast({ type: `success`, message: `操作完成` });
        this.back();
      }
    },
    // 返回
    back() {
      window.history.go(-1);
    },
  },
  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>
