<template>
  <div id="index">
    <index-detail
      :info="info"
      :map="map"
      :zixunList="gndtList"
      :noticeList="tzggList"
      :lawList="zhzczdList"
      :xinpinList="xinpinList"
      :chuangxinList="chuangxinList"
      :linkList="linkList"
    ></index-detail>
  </div>
</template>

<script>
import indexDetail from '@/components/index.vue';
import { createNamespacedHelpers, mapGetters } from 'vuex';
const { mapActions: mapSite } = createNamespacedHelpers('site');
const { mapActions: mapLink } = createNamespacedHelpers('link');
const { mapActions: mapAffairsColumn } = createNamespacedHelpers('affairsColumn');
const { mapActions: mapAffairsNews } = createNamespacedHelpers('affairsNews');
const { mapActions: mapPolicyColumn } = createNamespacedHelpers('policyColumn');
const { mapActions: mapPolicyNews } = createNamespacedHelpers('policyNews');
const { mapActions: mapProduct } = createNamespacedHelpers('enterpriseproject');
export default {
  name: 'index',
  props: {},
  components: {
    indexDetail,
  },
  data: () => ({
    info: {},
    map: require('@/assets/map.jpg'),
    gndtList: [],
    tzggList: [],
    zhzczdList: [],
    linkList: [],
    xinpinList: [],
    chuangxinList: [],
  }),
  created() {
    this.searchSite();
    this.searchLink();
    this.searchAffairs();
    this.searchPolicy();
    this.searchProduct();
  },
  computed: {},
  methods: {
    ...mapSite(['showInfo']),
    ...mapLink(['query']),
    ...mapAffairsColumn({ affairsColumnList: 'query', affairsColumnInfo: 'fetch' }),
    ...mapAffairsNews({ affairsNews: 'query' }),
    ...mapPolicyColumn({ policyColumnList: 'query', policyColumnInfo: 'fetch' }),
    ...mapPolicyNews({ policyNews: 'query' }),
    ...mapProduct({ productList: 'query', productInfo: 'fetch' }),
    // 查询站点信息
    async searchSite() {
      let res = await this.showInfo();
      let object = JSON.parse(JSON.stringify(res.data));
      if (object) {
        this.$set(this, `info`, res.data);
      } else {
        this.$message.error(res.errmsg ? res.errmsg : 'error');
      }
    },
    // 查询友情链接
    async searchLink({ ...info } = {}) {
      const res = await this.query({ ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `linkList`, res.data);
      }
    },
    // 最新資訊，通知公告（所属科技政务，国内动态，通知公告）
    async searchAffairs({ ...info } = {}) {
      const res = await this.affairsColumnList({ ...info });
      for (const val of res.data) {
        this.tpxwSearch({ column_id: val.id, site: val.site });
      }
    },
    async tpxwSearch({ skip = 0, limit = 8, column_id, site } = {}) {
      if (site === 'gndt') {
        const res = await this.affairsNews({ skip, limit, column_id: column_id });
        for (const val of res.data) {
          const result = await this.affairsColumnInfo(val.column_id);
          val.column_name = result.data.name;
        }
        this.$set(this, `${site}List`, res.data);
      } else if (site === 'tzgg') {
        const res = await this.affairsNews({ skip, limit, column_id: column_id });
        for (const val of res.data) {
          const result = await this.affairsColumnInfo(val.column_id);
          val.column_name = result.data.name;
        }
        this.$set(this, `${site}List`, res.data);
      }
    },
    // 政策法规（所属科技政策，综合政策指导）
    async searchPolicy({ ...info } = {}) {
      const res = await this.policyColumnList({ ...info });
      for (const val of res.data) {
        this.zhzcdPolicy({ column_id: val.id, site: val.site });
      }
    },
    async zhzcdPolicy({ skip = 0, limit = 8, column_id, site } = {}) {
      if (site === 'zhzczd') {
        const res = await this.policyNews({ skip, limit, column_id: column_id });
        for (const val of res.data) {
          const result = await this.policyColumnInfo(val.column_id);
          val.column_name = result.data.name;
        }
        this.$set(this, `${site}List`, res.data);
      }
    },
    // 新品发布，创新服务
    async searchProduct({ skip = 0, limit = 6, ...info } = {}) {
      const res = await this.productList({ skip, limit, ...info });
      for (const val of res.data) {
        if (val.totaltype === '1') {
          var arr = res.data.filter(item => item.totaltype === '1');
          this.$set(this, `xinpinList`, arr);
        } else if (val.totaltype === '2') {
          var arrs = res.data.filter(item => item.totaltype === '2');
          this.$set(this, `chuangxinList`, arrs);
        }
      }
    },
  },
};
</script>

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