<template>
  <div id="index">
    <admin-frame
      @search="search"
      :limit="limit"
      :total="total"
      topType="2"
      :usePage="active == '0' ? false : true"
      @back="back"
      :rightArrow="false"
      :useNav="false"
    >
      <template v-slot:info>
        <van-tabs v-model="active">
          <van-tab title="维权委托书">
            <van-col span="24" class="one">
              <van-button type="info" size="small" @click="searchPatent()">查询专利</van-button>
            </van-col>
            <van-col span="24" class="two">
              <form-1 :form="form" @onSubmit="onSubmit"></form-1>
            </van-col>
          </van-tab>
          <van-tab title="已委托">
            <list-1 :list="list" @toView="toView" @toResult="toResult"></list-1>
          </van-tab>
        </van-tabs>
      </template>
    </admin-frame>
    <van-dialog class="dialog" v-model="dialog.show" :title="dialog.title" :show-confirm-button="false" show-cancel-button cancel-button-text="返回">
      <search-1 v-if="dialog.type == '1'" @conPatent="conPatent"></search-1>
      <info-1 v-else-if="dialog.type == '2'" :info="info"></info-1>
      <result-1 v-else-if="dialog.type == '3'" :info="info"></result-1>
    </van-dialog>
  </div>
</template>

<script>
import form1 from '@/layout/safe/form-1.vue';
import search1 from '@/layout/assessment/search-1.vue';
import list1 from '@/layout/safe/list-1.vue';
import info1 from '@/layout/safe/info-1.vue';
import result1 from '@/layout/safe/result-1.vue';
import adminFrame from '@frame/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: patentsafeg } = createNamespacedHelpers('patentsafeg');
const { mapActions: patentinfo } = createNamespacedHelpers('patentinfo');
const { mapActions: adminLogin } = createNamespacedHelpers('adminLogin');
export default {
  name: 'index',
  props: {},
  components: {
    adminFrame,
    form1,
    search1,
    list1,
    info1,
    result1,
  },
  data: function () {
    return {
      // 管理员信息
      adminInfo: {},
      active: 0,
      // 列表
      list: [],
      total: 0,
      limit: 5,
      // 表单
      form: {
        questions: {},
      },
      // 专利信息
      patentInfo: {},
      // 弹框
      dialog: { show: false, title: '查询专利', type: '1' },
      // 详细信息
      info: {},
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...patentsafeg(['query', 'fetch', 'create', 'update']),
    ...patentinfo({ patentFetch: 'fetch' }),
    ...adminLogin({ adminQuery: 'query', adminFetch: 'fetch' }),
    async search({ skip = 0, limit = this.limit, ...info } = {}) {
      let res = await this.query({ skip, limit, user_id: this.user._id, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 提交
    async onSubmit({ data }) {
      let res = await this.create(data);
      if (this.$checkRes(res)) {
        this.$toast({ type: `success`, message: `专利维权申报成功` });
        this.search();
        this.searchOther();
      } else {
        this.$toast({ type: `error`, message: `${res.errmsg}` });
      }
    },
    // 返回
    back() {
      this.$router.push({ path: '/patent/index' });
    },
    // 查询用户
    async searchOther() {
      let res = await this.adminQuery({ code: this.user.code });
      if (this.$checkRes(res)) {
        let arr = await this.adminFetch(res.data[0].pid);
        if (this.$checkRes(arr)) this.$set(this, `adminInfo`, arr.data);
        let data = {
          admin_id: this.adminInfo.id,
          user_id: this.user.id,
          contact: this.user.phone,
          phone: this.user.name,
          email: this.user.email,
          questions: {},
        };
        this.$set(this, `form`, data);
      }
    },
    // 查询专利
    searchPatent() {
      let dialog = { show: true, title: '查询专利', type: '1' };
      this.$set(this, `dialog`, dialog);
    },
    // 已有專利，使用id查詢專利
    async searchInfo(id) {
      let res = await this.patentFetch(id);
      if (this.$checkRes(res)) this.conPatent(res.data);
    },
    // 确认选择
    conPatent(data) {
      this.$set(this, `patentInfo`, data);
      let arr = {
        user_id: this.user.id,
        admin_id: this.adminInfo.id,
        create_number: data.create_number,
        name: data.name,
        apply_personal: data.apply_personal,
        type: data.type,
        inventor: data.inventor,
        contact: this.user.name,
        phone: this.user.phone,
        email: this.user.email,
        questions: {},
      };
      this.$set(this, `form`, arr);
      let dialog = { show: false, title: '查询专利', type: '1' };
      this.$set(this, `dialog`, dialog);
    },
    // 查看信息
    toView(data) {
      this.$set(this, `info`, data);
      let dialog = { show: true, title: '详细信息', type: '2' };
      this.$set(this, `dialog`, dialog);
    },
    // 查看结果
    toResult(data) {
      this.$set(this, `info`, data);
      let dialog = { show: true, title: '审核结果', type: '3' };
      this.$set(this, `dialog`, dialog);
    },
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    id: {
      deep: true,
      immediate: true,
      handler(val) {
        if (val) {
          this.searchInfo(val);
        }
      },
    },
  },
};
</script>

<style lang="less" scoped>
.one {
  background-color: #fff;
  text-align: center;
  padding: 5px 0;
  border-bottom: 1px solid #cccccc;
}
.dialog {
  /deep/.van-dialog__content {
    max-height: 350px;
    overflow-y: auto;
  }
}
</style>
