<template>
  <div id="index">
    <admin-frame @search="search" :limit="limit" :total="total" topType="3" @back="back" :rightArrow="false" :useNav="false">
      <template v-slot:info>
        <list-1
          :list="list"
          @toView="toView"
          @toResult="toResult"
          @toHeavy="toHeavy"
          @toRevoke="toRevoke"
          @toContract="toContract"
          @toEndtrans="toEndtrans"
          @toConfirm="toConfirm"
        ></list-1>
      </template>
    </admin-frame>
    <van-dialog class="dialog" v-model="dialog.show" :title="dialog.title" :show-confirm-button="false" show-cancel-button cancel-button-text="返回">
      <info-1 :info="info" v-if="dialog.type == '1'"></info-1>
      <result-1 :info="info" v-if="dialog.type == '2'"></result-1>
      <endtrans-1 :form="info" v-if="dialog.type == '3'" @onSubmit="oneSubmit"></endtrans-1>
    </van-dialog>
  </div>
</template>

<script>
import list1 from '@/layout/trans/list-1.vue';
import info1 from '@/layout/trans/info-1.vue';
import result1 from '@/layout/trans/result-1.vue';
import endtrans1 from '@/layout/trans/endtrans-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: 'index',
  props: {},
  components: {
    adminFrame,
    list1,
    info1,
    result1,
    endtrans1,
  },
  data: function () {
    return {
      list: [],
      limit: 5,
      total: 0,
      // 弹框
      dialog: { show: false, title: '详细信息', type: '1' },
      // 详细信息
      info: {},
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...patenttrans(['query', 'update', 'check', 'delete']),
    async search({ skip = 0, limit = this.limit, searchName, ...info } = {}) {
      let res = await this.query({ skip, limit, type: this.type, user_id: this.user.id, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 详细信息
    toView(data) {
      this.$set(this, `info`, data);
      this.dialog = { show: true, title: '详细信息', type: '1' };
    },
    // 审核结果
    toResult(data) {
      this.$set(this, `info`, data);
      this.dialog = { show: true, title: '审核结果', type: '2' };
    },
    // 重申交易
    toHeavy(data) {
      this.$router.push({ path: '/patent/user/patent/information/trans_create', query: { trans_id: data.id } });
    },
    // 撤回交易
    async toRevoke(data) {
      let res = await this.delete(data.id);
      if (this.$checkRes(res)) {
        this.$toast({ type: `success`, message: `操作完成` });
        this.search();
      }
    },
    // 填写合同
    toContract(data) {
      this.$router.push({ path: '/patent/user/transaction/contract', query: { id: data.id } });
    },
    // 结束交易
    toEndtrans(data) {
      this.$set(this, `info`, data);
      this.dialog = { show: true, title: '填写合同', type: '3' };
    },
    // 提交合同
    async oneSubmit(data) {
      data.is_contract = '0';
      data.status = '5';
      data.transfer_date = moment(new Date()).format('YYYY-DD-MM');
      let res = await this.update(data);
      if (this.$checkRes(res)) {
        this.$toast({ type: `success`, message: `交易完成` });
        this.search();
        this.dialog = { show: false, title: '填写合同', type: '3' };
      }
    },
    // 用户确认
    async toConfirm(data) {
      data.status = '4';
      let res = await this.update(data);
      if (this.$checkRes(res)) {
        this.$toast({ type: `success`, message: `交易完成` });
        this.search();
      }
    },
    // 返回
    back() {
      this.$router.push({ path: '/patent/index' });
    },
  },
  computed: {
    ...mapState(['user']),
    type() {
      return this.$route.query.type;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.dialog {
  /deep/.van-dialog__content {
    max-height: 350px;
    overflow-y: auto;
  }
}
</style>
