<template>
  <div id="memberList">
    <member-lists :backColor="backColor" :info="site" :menuList="menu" :rightList="rightList" @search="toGetNews" :total="total" :title="title"></member-lists>
  </div>
</template>

<script>
import memberLists from '@/components/memberList.vue';
import { mapActions, mapState } from 'vuex';
import _ from 'lodash';
export default {
  metaInfo() {
    return {
      title: this.siteTitle ? this.siteTitle : '就业信息网',
    };
  },
  name: 'memberList',
  props: {},
  components: {
    memberLists,
  },
  data: () => ({
    backColor: '#be0101',
    info: {
      title: '吉林省高等学校毕业生就业信息网',
      banner: require('@/assets/memberBg.jpg'),
      copyright: '吉ICP备09006292号-1 Copyright 2019 版权所有 吉林省高等学校毕业生就业指导中心 All Rights Reserved',
    },
    rightList: [],
    site: {}, //站点信息
    siteTitle: '', //站点标题
    menu: [], //菜单
    modules: [], //模块
    total: 0,
  }),
  async created() {
    await this.toGetSite(); //获取主站信息
    this.loadMenu(); //获取菜单信息
    // this.toGetModule(); //获取模块信息
    this.toGetNews(); //获取数据
  },
  computed: {
    type() {
      return this.$route.params.type;
    },
    id() {
      return this.$route.params.id;
    },
    title() {
      return this.$route.query.title;
    },
    listSpan() {
      return this.type === 'menu' ? 18 : 24;
    },
  },
  watch: {
    type: 'getSubMenu',
    id: 'toGetNews',
    title: 'getSubMenu',
  },
  methods: {
    ...mapActions(['getSite', `getMenu`, 'getColumn', 'getModule', 'getNews', 'getJobInfo', 'getPosts', 'getLink']),
    //站点信息
    async toGetSite() {
      let site = sessionStorage.getItem('site');
      if (site) {
        this.$set(this, `site`, JSON.parse(site));
        this.$set(this, `siteTitle`, this.site.name);
      } else {
        let result = await this.getSite({ type: 'search' });
        if (result.errcode === 0) {
          sessionStorage.setItem('site', JSON.stringify(result.data));
          if (_.get(result.data, `custom`)) {
            let item = result.custom;
          }
          this.$set(this, `site`, result.data);
          this.$set(this, `siteTitle`, this.site.name);
        }
      }
    },
    //菜单
    async loadMenu() {
      let menu = sessionStorage.getItem('menu');
      if (menu) {
        this.$set(this, `menu`, JSON.parse(menu));
        this.finishedMenu();
        return;
      } else this.toGetMenu();
    },
    async toGetMenu() {
      let result = await this.getMenu({ type: `list` });
      if (result.errcode === 0) {
        // sessionStorage.setItem('menu', JSON.stringify(result.data));
        this.$set(this, `menu`, result.data);
        this.finishedMenu();
      }
    },
    async finishedMenu() {
      let menus = JSON.parse(JSON.stringify(this.menu));
      for (const item of menus) {
        if (item.type === 'content') {
          item.path = `/detail?id=${item.content_id}`;
        } else if (item.type !== 'url') {
          let res = await this.completeMenu(item);
          item.children = res;
        }
      }
      this.$set(this, `menu`, menus);
    },
    async completeMenu(item) {
      let result = await this.getColumn({
        type: `list`,
        data: { parent_id: item.id },
      });
      if (result.errcode === 0) {
        let columns = result.data;
        for (const col of columns) {
          if (col.type === 'content') {
            col.path = `/detail/${col.content_id}`;
          } else if (col.type !== 'url') {
            col.path = `/newsList/menu/${col.id}?title=${col.title}`;
            if (col.parent.includes('党员')) {
              col.path = `/memberList/menu/${col.id}?title=${col.title}`;
            }
          }
        }
        return columns;
      }
    },
    //模块
    // async loadModule() {
    //   let modules = sessionStorage.getItem('modules');
    //   for (const item of modules) {
    //     if (item.category === 'member') {
    //       this.$set(this, `modules`, JSON.parse(modules));
    //       return;
    //     }
    //   }
    //   this.toGetMenu();
    // },
    // async toGetModule() {
    //   let result = await this.getModule({ type: `list` });
    //   if (result.errcode === 0) {
    //     sessionStorage.setItem('modules', JSON.stringify(result.data));
    //     this.$set(this, `modules`, result.data);
    //     this.finishedModule();
    //   }
    // },
    // async finishedModule() {
    //   let modules = JSON.parse(JSON.stringify(this.modules));
    //   let object = {};
    //   for (const item of modules) {
    //     if (item.category === 'member') {
    //       if (`${item.is_use}` === `0`) {
    //         let res = await this.completeModules(item);
    //         item.children = res;
    //         object = item;
    //       }
    //     }
    //   }
    //   for (const item of object.children) {
    //     this.$set(this, `${item.category}List`, item);
    //   }
    // },
    // async completeModules(item) {
    //   let result = await this.getColumn({
    //     type: `list`,
    //     data: { parent_id: item.id },
    //   });
    //   if (result.errcode === 0) {
    //     let columns = result.data;
    //     for (const col of columns) {
    //       if (col.type === `bugList`) {
    //         col.path = `/memberList/module/${col.content_id}`;
    //         col.children = await this.completeColumn(col);
    //       } else if (col.type === `column`) {
    //         col.path = `/memberList/module/${col.id}`;
    //         col.children = await this.completeColumn(col);
    //       } else if (col.type === `content`) {
    //         col.path = `/detail/${col.content_id}`;
    //       }
    //     }
    //     return columns;
    //   }
    // },
    // async completeColumn(item) {
    //   let data = { skip: 0, limit: 8 };
    //   if (item.type === `bugList`) {
    //     data.news_type = `0`;
    //     data.parent_id = item.content_id;
    //   } else {
    //     data.news_type = `1`;
    //     data.parent_id = item.id;
    //   }
    //   let result = await this.getNews({ type: 'bugList', data: data });
    //   if (result.errcode === 0) {
    //     return result.data;
    //   }
    // },
    //请求数据
    async toGetNews({ skip = 0, limit = 8 } = {}) {
      let result = await this.getNews({ type: 'list', data: { parent_id: this.id, skip: skip, limit: limit } });
      if (result.errcode === 0) {
        let data = result.data;
        for (const item of data) {
          let info = await this.getNews({ type: 'fetch', data: { id: item.id } });
          if (info.errcode === 0) item.content = info.data.content;
          this.$set(this, `total`, result.total);
        }
        this.$set(this, `rightList`, result.data);
      }
    },
  },
};
</script>

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