<template>
  <div id="shujulist">
    <shujulist-detail
      :info="info"
      :liebiaoList="liebiaoList"
      :display="display"
      :columnName="columnName"
      :contentList="contentList"
      :total="total"
      :columnTitle="columnTitle"
      :policyInfo="policyInfo"
      @clickLists="submit"
      @fetch="fetchInfo"
    ></shujulist-detail>
  </div>
</template>

<script>
import shujulistDetail from '@/components/shuju/shujulist.vue';
import { createNamespacedHelpers, mapGetters } from 'vuex';
import _ from 'loadsh';
const { mapActions: mapSite } = createNamespacedHelpers('site');
const { mapActions: mappolicyColumn } = createNamespacedHelpers('recordColumn');
const { mapActions: mappolicyNews } = createNamespacedHelpers('recordNews');

export default {
  name: 'shujulist',
  props: {},
  components: { shujulistDetail },
  data: () => ({
    info: {},
    liebiaoList: [],
    display: 'list',
    columnName: '',
    contentList: [],
    total: 1,
    leftId: '',
    columnTitle: '',
    policyInfo: {},
  }),
  async created() {
    this.searchSite();
    await this.policyColumn();
    await this.defaultColumn();
  },
  computed: {},
  methods: {
    ...mapSite(['showInfo']),
    ...mappolicyColumn({ policyColumns: 'query', policyfetch: 'fetch' }),
    ...mappolicyNews({ 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 policyColumn({ ...info } = {}) {
      const res = await this.policyColumns({ ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `liebiaoList`, 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({ column_id: this.leftId });
      for (const val of res.data) {
        const result = await this.policyfetch(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.liebiaoList.length > 0)) return;
      let id = _.get(this.liebiaoList[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.policyfetch(res.data.column_id);
      this.$set(this, `columnTitle`, result.data.name);
      this.$set(this, `policyInfo`, res.data);
      // 查询详情，赋值
    },
  },
};
</script>

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