<template>
  <div id="index">
    <admin-frame @search="search" :limit="limit" :total="total" topType="2" @back="back" @add="toAdd" :useNav="false">
      <template v-slot:info>
        <van-col span="24" class="one">
          <van-button type="info" size="small" @click="toSearch()">查询条件</van-button>
          <van-button type="info" size="small" @click="toImport()">导入专利</van-button>
          <van-button type="info" size="small" @click="downPatent()">下载专利</van-button>
          <van-button type="info" size="small" @click="downSpeed()">下载进度</van-button>
          <p>
            专利总数：<span class="total">({{ total }})</span>
          </p>
        </van-col>
        <van-col span="24" class="two">
          <list-1 :list="list" @toView="toView" @toEdit="toEdit" @toDel="toDel"></list-1>
        </van-col>
      </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>
      <speed-1 :list="speedList" v-if="dialog.type == '2'"></speed-1>
      <search-1 v-if="dialog.type == '3'" :searchForm="searchForm" @reseat="reseat" @onSubmit="onSubmit"></search-1>
    </van-dialog>
  </div>
</template>

<script>
import list1 from '@/layout/patentInfo/list-1.vue';
import info1 from '@/layout/patentInfo/info-1.vue';
import speed1 from '@/layout/patentInfo/speed-1.vue';
import search1 from '@/layout/patentInfo/search-1.vue';
import adminFrame from '@frame/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: patentinfo } = createNamespacedHelpers('patentinfo');
const { mapActions: mission } = createNamespacedHelpers('mission');
const { mapActions: personal } = createNamespacedHelpers('personal');
const { mapActions: adminLogin } = createNamespacedHelpers('adminLogin');
export default {
  name: 'index',
  props: {},
  components: { adminFrame, list1, info1, speed1, search1 },
  data: function () {
    return {
      list: [],
      limit: 4,
      total: 0,
      // 弹框
      dialog: { show: false, title: '详细信息', type: '1' },
      // 详细信息
      info: {},
      // 下载专利进度
      speedList: [],
      // 查询条件
      searchForm: {},
    };
  },
  created() {
    this.search();
  },
  methods: {
    ...patentinfo(['query', 'delete', 'toExport', 'fetch']),
    ...mission({ missionQuery: 'query' }),
    ...personal({ personalFetch: 'fetch' }),
    ...adminLogin({ adminQuery: 'query' }),
    async search({ skip = 0, limit = this.limit, ...info } = {}) {
      let res = await this.query({ skip, limit, ...this.searchForm, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.data);
        this.$set(this, `total`, res.total);
      }
    },
    // 详细信息
    async toView(data) {
      let res = await this.fetch(data.id);
      if (this.$checkRes(res)) {
        this.$set(this, `info`, res.data);
        this.dialog = { show: true, title: '详细信息', type: '1' };
      }
    },
    // 修改信息
    toEdit(data) {
      this.$router.push({ path: '/patent/admin/patent/detail', query: { id: data.id } });
    },
    async toDel(data) {
      let res = await this.delete(data.id);
      if (this.$checkRes(res)) {
        this.$toast({ type: `success`, message: `删除成功` });
        this.search();
      }
    },
    // 添加
    toAdd() {
      this.$router.push({ path: '/patent/admin/patent/detail' });
    },
    // 导入专利
    toImport() {
      this.$router.push({ path: '/patent/admin/patent/import' });
    },
    // 下载专利
    async downPatent() {
      let res = await this.toExport({ query: this.searchForm, user: this.user.id });
      console.log(res);
      if (this.$checkRes(res)) {
        this.$toast({ type: `success`, message: `操作完成` });
      }
    },
    // 下载进度
    async downSpeed() {
      let res = await this.missionQuery({ user: this.user.id });
      if (this.$checkRes(res)) {
        this.$set(this, `speedList`, res.data);
        this.dialog = { show: true, title: '下载进度', type: '2' };
      }
    },
    // 查询专利
    // 查询
    toSearch() {
      this.dialog = { show: true, title: '查询条件', type: '3' };
    },
    // 重置条件
    reseat() {
      this.searchForm = {};
      this.search();
    },
    // 提交查询
    onSubmit({ data }) {
      this.search(data);
      this.dialog = { show: false, title: '查询条件', type: '3' };
    },
    // 批量删除
    selectsDel() {
      console.log('批量删除');
    },
    // 返回
    back() {
      this.$router.push({ path: '/patent/index' });
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.one {
  text-align: center;
  margin: 8px 0;
  .van-button {
    margin: 0 5px 5px 0;
  }
  p {
    font-size: 16px;
    margin: 5px 0 0 0;
    span {
      color: #000;
      font-weight: bold;
    }
  }
}
.dialog {
  /deep/.van-dialog__content {
    max-height: 350px;
    overflow-y: auto;
  }
}
</style>
