<template>
  <div id="test">
    <van-row>
      <van-col span="24" class="main">
        <van-button @click="toAdd()">批量添加专利交易信息</van-button>
        <download-excel :data="textname" :fields="excel_fields.fields" :name="excel_fields.name">
          <el-button type="primary" size="mini">导出</el-button>
        </download-excel>
      </van-col>
      <van-col span="24">
        <van-button @click="toEdit()">修改数据</van-button>
        <van-button @click="toNotcie()">查询通知信息</van-button>
        <van-button @click="toView()">查看信息</van-button>
      </van-col>
      <van-col span="24">
        <van-button @click="notcieSearch()">通知信息</van-button>
        <van-button @click="notcieSchSearch()">查询指定学校的用户通知信息</van-button>
        <van-button @click="notcieSchFilter()">过滤数据</van-button>
      </van-col>
    </van-row>
  </div>
</template>

<script>
const { textname } = require('@frame/src/layout/test');
const { list } = require('../util/test');
import adminFrame from '@frame/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: statistics } = createNamespacedHelpers('statistics');
const { mapActions: personal } = createNamespacedHelpers('personal');
const { mapActions: patentearly } = createNamespacedHelpers('patentearly');
const { mapActions: patentnotice } = createNamespacedHelpers('patentnotice');
const moment = require('moment');
export default {
  name: 'test',
  props: {},
  components: {},
  data: function () {
    return {
      list: [],
      textname: textname,
      // 导出表头配置
      excel_fields: {
        fields: {
          单位名称: 'name',
          专利数量: 'num',
        },
        name: '申请人单位导出',
      },
      // 替换数据
      // 用户信息
      userList: [],
      userList1: [],
      userList2: [],
      // 通知信息
      noticeList: [],
      // 指定人通知信息
      notice4List: [],
      // 工大，工程通知信息
      notice5List: [],
    };
  },
  created() {
    // this.oneSearch();
    // this.twoSearch();
  },
  methods: {
    ...statistics(['patentInfoByApplyPerson']),
    ...personal({
      personalQuery: 'query',
      personalUpdate: 'update',
      personalDelete: 'delete',
      personalFetch: 'fetch',
    }),
    ...patentearly({ patentearlyQuery: 'query' }),
    ...patentnotice({
      patentnoticeQuery: 'query',
      patentnoticeUpdate: 'update',
      patentnoticeDelete: 'delete',
    }),
    // 添加用户
    async toAdd() {
      console.log(this.textname);
    },
    oneSearch() {
      const create_date = '2015-1-22';
      // 专利到期时间
      const expire_date = moment(new Date()).format('YYYY') + '-' + moment(create_date).format('MM-DD');
      // 专利到期时间延后一个月
      const end = moment(expire_date).add(1, 'months').format('YYYY-MM-DD');
      // 专利到期前三个月
      const start = moment(end).subtract(3, 'months').format('YYYY-MM-DD');
      // 专利到期前两个月
      const start_two = moment(end).subtract(2, 'months').format('YYYY-MM-DD');
      // 专利到期前一个月
      const start_thr = moment(end).subtract(1, 'months').format('YYYY-MM-DD');
      // 判断是否是三个月的区间
      const r = moment().isBetween(start, end, null, '[]');
      if (r) {
        // 三个月内的第一天||两个月内的第一天||一个月内的第一天
        const toDay = moment('2022-01-22').format('YYYY-MM-DD');
        if (toDay === start || toDay === start_two || toDay === start_thr) {
          console.log('1');
        }
        const early_num = toDay === start ? 1 : toDay === start_two ? 2 : toDay === start_thr ? 3 : 1;
        console.log(early_num);
      }
    },
    async twoSearch() {
      let res = await this.personalQuery({});
      if (this.$checkRes(res)) {
        let arr = res.data.filter((i) => i.isdel == '1');
        console.log(arr);
        // for (const val of res.data) {
        //   let res = await this.personalUpdate({ id: val.id, isdel: '0' });
        //   console.log(res);
        // }
      }
    },
    // 修改数据
    async toEdit() {
      // 重名数据
      let user = [];
      let p1 = list;
      for (const val of p1) {
        let p2 = list.filter((i) => i.name == val.name);
        if (p2.length >= 2) {
          user.push(...p2);
        }
      }
      this.$set(this, `userList`, user);
      console.log('1');
    },
    // 查询通知信息
    async toNotcie() {
      let notice = [];
      let user = this.userList;
      // 手机号数据，查询通知信息
      let p1 = user.filter((i) => i.phone != i.name);
      this.$set(this, `userList1`, p1);
      // 姓名数据
      let p2 = user.filter((i) => i.phone == i.name);
      this.$set(this, `userList2`, p2);
      for (const val of p1) {
        let res = await this.patentnoticeQuery({
          skip: 0,
          limit: 10000,
          to_type: '4',
          to_id: val._id,
        });
        if (res.errcode == '0' && res.total > 0) {
          notice.push(...res.data);
        }
      }
      this.$set(this, `noticeList`, notice);
      console.log('2');
    },
    // 查看信息
    async toView() {
      // 重名信息，利用手机号的账号查询通知信息
      // 修改通知信息，把to_id改成手机号是姓名的账号身上
      // 重名数据
      console.log(this.userList);
      // 手机号数据
      console.log(this.userList1);
      // 姓名数据
      console.log(this.userList2);
      // 通知数据
      console.log(this.noticeList);
      // 修改数据
      // for (const val of this.noticeList) {
      //   let user = this.userList2.find((i) => i.phone == val.to_name);
      //   if (user) {
      //     let obj = { id: val.id, _id: val._id, to_id: [user._id] };
      //     let res = await this.patentnoticeUpdate(obj);
      //     console.log(res);
      //   }
      // }
      // console.log('3');
    },
    // 查询通知信息
    async notcieSearch() {
      let res = await this.patentnoticeQuery({ skip: 0, limit: 1000, to_type: '4' });
      if (res.errcode == '0') {
        this.$set(this, `notice4List`, res.data);
        console.log('1');
      }
    },
    // 指定学校人的通知信息
    async notcieSchSearch() {
      let user = [];
      let p1 = await this.personalQuery({ code: 'CCUT' });
      let p2 = await this.personalQuery({ code: 'CCIT' });
      user = [...p1.data, p2.data];
      this.$set(this, `userList`, user);
      console.log('2');
    },
    // 过滤数据
    async notcieSchFilter() {
      for (const val of this.notice4List) {
        for (const p1 of val.to_id) {
          let res = await this.personalFetch(p1);
          if (res.data && res.data._id) {
          } else {
            console.log(p1);
            console.log(val);
          }
        }
      }
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

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