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

<script>
import trans1 from '@/layout/patentInfo/trans-1.vue';
import adminFrame from '@frame/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: adminLogin } = createNamespacedHelpers('adminLogin');
const { mapActions: patentinfo } = createNamespacedHelpers('patentinfo');
const { mapActions: patentassess } = createNamespacedHelpers('patentassess');
const { mapActions: patenttrans } = createNamespacedHelpers('patenttrans');
const moment = require('moment');
export default {
  name: 'detail',
  props: {},
  components: {
    adminFrame,
    trans1,
  },
  data: function () {
    return {
      // 机构信息
      mechInfo: {},
      // 专利信息
      patentInfo: {},
      // 价值评估信息
      assessInfo: {},
      form: { promise_file: {}, report: [] },
    };
  },
  async created() {
    if (this.id) await this.searchOther();
    if (this.trans_id) await this.searchInfo();
  },
  methods: {
    ...adminLogin({ adminQuery: 'query' }),
    ...patentinfo({ patentFetch: 'fetch' }),
    ...patentassess({ assessQuery: 'query' }),
    ...patenttrans(['create', 'fetch', 'update']),
    async searchInfo() {
      let res = await this.fetch(this.trans_id);
      if (this.$checkRes(res)) {
        this.$set(this, `form`, res.data);
      }
    },
    async searchOther() {
      // 查询机构信息
      let res = await this.adminQuery({ code: this.user.code });
      if (this.$checkRes(res)) this.$set(this, `mechInfo`, res.data[0]);
      // 查询专利信息
      res = await this.patentFetch(this.id);
      if (this.$checkRes(res)) this.$set(this, `patentInfo`, res.data);
      // 查询价值评估信息
      res = await this.assessQuery({ user_id: this.user.id, patent_id: this.id, status: '2' });
      if (this.$checkRes(res)) this.$set(this, `assessInfo`, res.data[0]);
      // 查询专利评估报告
      let data = {
        user_id: this.user.id,
        user_name: this.user.phone,
        mech_id: this.mechInfo.id,
        mech_name: this.mechInfo.name,
        patent_id: this.patentInfo.id,
        patent_name: this.patentInfo.name,
        create_number: this.patentInfo.create_number,
        on_obligee: this.patentInfo.on_obligee,
        abstract: this.patentInfo.abstract || '',
        contact: this.user.phone,
        phone: this.user.name,
        email: this.user.email,
        is_report: this.assessInfo ? true : false,
        // report: this.assessInfo ? this.assessInfo.report : [],
        report: [],
        promise_file: {
          province: '吉林省',
          city: '长春市',
          end_date: moment(new Date()).format('YYYY-MM-DD'),
        },
      };
      this.$set(this, `form`, data);
    },
    // 发起专利交易
    async onSubmit({ data }) {
      if (data.budget_type == '1') data.budget = '面议';
      else if (data.budget_type == '2') data.budget = data.budget_min + '-' + data.budget_max;
      if (data.id) {
        data.status = '0';
        let res = await this.update(data);
        if (this.$checkRes(res)) {
          this.$toast({ type: `success`, message: `操作完成` });
          this.back();
        }
      } else {
        let res = await this.create(data);
        if (this.$checkRes(res)) {
          this.$toast({ type: `success`, message: `操作完成` });
          this.back();
        }
      }
    },
    // 返回
    back() {
      this.$router.push({ path: '/patent/user/patent/information' });
    },
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
    trans_id() {
      return this.$route.query.trans_id;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

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