<template>
  <div id="index">
    <index-detail
      :info="site"
      :menuList="menu"
      :newsList="newsList"
      :carouselList="carouselList"
      :noticeNewsList="noticeList"
      :zhidaoList="zhidaoList"
      :lawList="lawList"
      :serviceList="serviceList"
      :hujiList="hujiList"
      :xiazaiList="xiazaiList"
      :recruitList="jobinfoList"
      :zixunList="zixunList"
      :nativeList="nativeList"
      :backColor="backColor"
    ></index-detail>
  </div>
</template>

<script>
import indexDetail from '@/components/index.vue';
import { urlList } from '@/config/url';
import { mapActions, mapState } from 'vuex';
import _ from 'lodash';
export default {
  metaInfo() {
    return {
      title: this.siteTitle ? this.siteTitle : '就业信息网',
    };
  },
  name: 'index',
  props: {},
  components: {
    indexDetail,
  },
  data: () => ({
    info: {
      title: '吉林省高等学校毕业生就业信息网',
      english: 'Jilin University Graduate Employment Guidance Center',
      banner: require('@/assets/banner.jpg'),
      logo: require('@/assets/logo.png'),
      copyright: '吉ICP备09006292号-1 Copyright 2019 版权所有 吉林省高等学校毕业生就业指导中心 All Rights Reserved',
    },
    site: {}, //站点信息
    siteTitle: {}, //站点标题
    menu: [], //菜单
    modules: [], //模块
    newsList: [],
    carouselList: [], //图片新闻
    noticeList: [], //通知公告+就业动态
    zhidaoList: [], //就业指导
    lawList: [], //政策法规
    serviceList: [], //档案服务
    hujiList: [], //户籍管理
    xiazaiList: [], //下载专区
    recruitList: [], //招聘信息
    zixunList: [], //就业咨询
    jobinfoList: [], //招聘信息
    nativeList: urlList, //网站导航
    memberList: [], //党员之家
    backColor: '#0457c7',
  }),
  async created() {
    await this.toGetSite(); //获取主站信息
    this.loadMenu(); //获取菜单信息
    this.toGetModule(); //获取模块信息
    this.toGetJobInfo(); //获取招聘信息
    // this.toGetLinks(); //招聘信息+网站链接
    // this.self();
  },
  computed: {},
  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');
      if (modules) {
        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));
      for (const item of modules) {
        if (`${item.is_use}` === `0`) {
          let res = await this.completeModules(item);
          item.children = res;
        }
      }
      for (const item of modules) {
        this.$set(this, `${item.category}List`, item);
      }
      this.$set(this, `modules`, 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 = `/newsList/module/${col.content_id}`;
            col.children = await this.completeColumn(col);
          } else if (col.type === `column`) {
            col.path = `/newsList/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 toGetJobInfo() {
      let result = await this.getJobInfo({ type: 'list', data: { limit: 8 } });
      if (`${result.errcode}` === '0') {
        for (const item of result.data) {
          if (!item.jobs || !(item.jobs.length > 0)) continue;
          let jobs = [];
          for (let job of item.jobs) {
            let jobInfo = await this.toGetPosts(job);
            jobs.push(jobInfo);
          }
          item.jobs = jobs;
        }
        this.$set(this, 'jobinfoList', result.data);
      } else {
        this.$message.error(result.errmsg ? result.errmsg : 'error');
      }
    },
    async toGetPosts(id) {
      let result = await this.getPosts({ type: 'fetch', data: { id: id } });
      if (result.errcode === 0) return result.data;
    },
  },
};
</script>

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