<template>
  <div id="index">
    <admin-frame @search="search" :limit="limit" :total="total" topType="2" @back="back" :rightArrow="false" :useNav="false">
      <template v-slot:info>
        <list-1 :list="list" @toView="toView"></list-1>
      </template>
    </admin-frame>
    <van-dialog class="dialog" v-model="show" title="文件下载" :show-confirm-button="false" :show-cancel-button="true" cancel-button-text="返回">
      <van-col span="24" class="list" v-for="(item, index) in fileList" :key="index">
        <van-col span="24" class="name"
          >文件路径：<span>{{ item }}</span>
        </van-col>
        <van-col span="24" class="btn">
          <van-button type="info" size="small" @click="download(item)">下载文件</van-button>
        </van-col>
      </van-col>
    </van-dialog>
  </div>
</template>

<script>
import list1 from './parts/list-4.vue';
import adminFrame from '@frame/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: patentwarning } = createNamespacedHelpers('patentwarning');
export default {
  name: 'index',
  props: {},
  components: {
    adminFrame,
    list1,
  },
  data: function () {
    return {
      limit: 5,
      total: 0,
      list: [],
      // 文件下载
      show: false,
      fileList: [],
    };
  },
  async created() {
    this.search();
  },
  methods: {
    ...patentwarning(['query', 'fetch']),
    async search({ skip = 0, limit = this.limit, searchName, ...info } = {}) {
      info.to_id = this.user._id;
      let res = await this.query({ skip, limit, ...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, `fileList`, res.data.file_url);
        this.show = true;
      }
    },
    download(data) {
      if (data || data.url) {
        window.location.href = `${process.env.VUE_APP_HOST}/${data.url || data}`;
      } else {
        this.$toast({ type: `fail`, message: `未上传文件` });
      }
    },
    // 返回
    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>
.dialog {
  padding: 0 8px;
  /deep/.van-dialog__content {
    height: 350px;
    overflow-y: auto;
  }
  .list {
    .name {
      font-size: 14px;
      word-break: break-all;
      color: #666;
      margin: 0 0 5px 0;
      span {
        color: #000;
      }
    }
    .btn {
      text-align: center;
    }
  }
}
</style>
