<template>
  <div id="governmentList">
    <governmentLists-layout
      :info="info"
      :nativeList="nativeList"
      :display="display"
      :columnName="columnName"
      :contentList="contentList"
      :total="total"
      :columnTitle="columnTitle"
      :policyInfo="policyInfo"
      @clickLists="submit"
      @fetch="fetchInfo"
    ></governmentLists-layout>
  </div>
</template>

<script>
import governmentListsLayout from '@/components/government/governmentList.vue';
import { createNamespacedHelpers, mapGetters } from 'vuex';
import _ from 'loadsh';
const { mapActions: mapSite } = createNamespacedHelpers('site');
const { mapActions: mapColumn } = createNamespacedHelpers('affairsColumn');
const { mapActions: mapNews } = createNamespacedHelpers('affairsNews');

export default {
  name: 'governmentList',
  props: {},
  components: {
    governmentListsLayout,
  },
  data: () => ({
    info: {},
    nativeList: [],
    display: 'list',
    columnName: '',
    contentList: [],
    total: 1,
    leftId: '',
    columnTitle: '',
    policyInfo: {},
  }),
  async created() {
    this.searchSite();
    await this.searchColumn();
    await this.defaultColumn();
  },
  computed: {},
  methods: {
    ...mapSite(['showInfo']),
    ...mapColumn({ columnList: 'query', columnInfo: 'fetch' }),
    ...mapNews({ policyNew: 'query', newsFetch: '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 searchColumn({ ...info } = {}) {
      const res = await this.columnList({ ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `nativeList`, res.data);
      }
    },
    submit({ id }) {
      this.$set(this, `leftId`, id);
      this.display = 'list';
      this.searchRight();
    },
    async searchRight({ skip = 0, limit = 10, column_id } = {}) {
      const res = await this.policyNew({ skip, limit, column_id: this.leftId });
      for (const val of res.data) {
        const result = await this.columnInfo(val.column_id);
        val.column_name = result.data.name;
      }
      this.$set(this, `contentList`, res.data);
      this.$set(this, `total`, res.data.length);
      for (const val of res.data) {
        this.$set(this, `columnName`, val.column_name);
      }
    },
    async defaultColumn() {
      if (!(this.nativeList.length > 0)) return;
      let id = _.get(this.nativeList[0], 'id', false);
      if (!id) return;
      this.submit({ id });
    },
    async fetchInfo(id) {
      this.display = 'detail';
      const res = await this.newsFetch(id);
      const result = await this.columnInfo(res.data.column_id);
      this.$set(this, `columnTitle`, result.data.name);
      this.$set(this, `policyInfo`, res.data);
      // 查询详情，赋值
    },
  },
};
</script>

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