<template>
  <div id="member">
    <member-detail
      :backColor="backColor"
      :info="site"
      :menuList="menu"
      :messageInfo="messageInfo"
      :carouselList="tupianList"
      :newsList="newsList"
      :rencaiList="rencaiList"
      :dangjianList="dangjianList"
      :yaowenList="yaowenList"
      :dangguiList="dangguiList"
      :zhishiList="zhishiList"
      :zhunti="zhuantiList"
      :tujiList="huodongList"
    ></member-detail>
  </div>
</template>

<script>
import memberDetail from '@/components/member.vue';
import { mapActions, mapState } from 'vuex';
import _ from 'lodash';
export default {
  metaInfo() {
    return {
      title: this.siteTitle ? this.siteTitle : '就业信息网',
    };
  },
  name: 'member',
  props: {},
  components: {
    memberDetail,
  },
  data: () => ({
    backColor: '#be0101',
    info: {
      title: '吉林省高等学校毕业生就业信息网',
      banner: require('@/assets/memberBg.jpg'),
      copyright: '吉ICP备09006292号-1 Copyright 2019 版权所有 吉林省高等学校毕业生就业指导中心 All Rights Reserved',
    },
    messageInfo: {
      title: '市委常委会召开“不忘初心、牢记使命“专题民主生活会',
      content: '吉林省高等学校毕业生就业信息网',
    },
    carouselList: [
      {
        url: require('@/assets/news.jpg'),
        title: '吉林省高等学校毕业生就业信息网',
      },
    ],
    tupianList: [], //高层声音中左侧图片新闻
    newsList: [], //高层声音中右侧文字新闻列表
    rencaiList: [], //人才建设
    dangjianList: [], //党建综合
    yaowenList: [], //党建要闻
    dangguiList: [], //党规党章
    zhishiList: [], //知识问答
    zhuantiList: [], //专题学习
    huodongList: [], //活动图集
    site: {}, //站点信息
    siteTitle: {}, //站点标题
    menu: [], //菜单
    modules: [], //模块
  }),
  async created() {
    await this.toGetSite(); //获取主站信息
    this.loadMenu(); //获取菜单信息
    this.toGetModule(); //获取模块信息
  },
  computed: {},
  methods: {
    ...mapActions(['getSite', `getMenu`, 'getColumn', 'getAllNews', 'getAllColumn', '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.toGetModule();
    },
    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 res = await this.completeAllModules(modules);
    },
    //弃用
    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 completeAllModules(modules) {
      let newMod = modules.filter(fil => fil.category === 'member');
      let ids = newMod.map(item => item.id);
      let result = await this.getAllColumn(ids);

      if (result.length > 0) {
        //整理栏目
        result = result.map(item => {
          if (item.type === 'column') item.path = `/memberList/module/${item.id}`;
          else item.path = `/memberList/module/${item.content_id}`;
          return item;
        });
        //请求信息
        let res = await this.getAllNews(result);
        //合并栏目和信息 这部分少一层,直接将栏目set就可以
        result = result.map(item => {
          let newsList = res.filter(fil => fil.parent_id === item.id);
          item.children = newsList;
          this.$set(this, `${item.category}List`, item);
          return item;
        });
      }
    },
  },
};
</script>

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