<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"
      :list="list"
      @onsave="onsaveClick"
    ></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: [], //党员之家
    list: [
      {
        pic: require('@/assets/kuaisu1.jpg'),
        title: '就业派遣',
        // url: 'http://www.jilinjobs.cn/JobSending.htm',
      },
      {
        pic: require('@/assets/kuaisu2.jpg'),
        title: '档案管理',
        // url: 'http://www.jilinjobs.cn/FileManagement.htm',
      },
      {
        pic: require('@/assets/kuaisu3.jpg'),
        title: '流动党员',
        // url: 'http://www.jilinjobs.cn/CaucusRelation.htm',
      },
      {
        pic: require('@/assets/kuaisu4.jpg'),
        title: '户籍管理',
        // url: 'http://www.jilinjobs.cn/RegisterManagement.htm',
      },
      {
        pic: require('@/assets/kuaisu5.jpg'),
        title: '学历认证',
        // url: 'http://www.jilinjobs.cn/EducationCertify.htm',
      },
      {
        pic: require('@/assets/kuaisu6.jpg'),
        title: '常见问题',
        url:
          'https://mp.weixin.qq.com/s?__biz=MzAxMzg2ODE0NA==&mid=100000727&idx=1&' +
          'sn=c2a38aca7482a930ceeff01779827faa&chksm=' +
          '1b9d4ec82ceac7dee8cf6f89701e9af1fdedeed6c7e5ecba20356b1584d6ee76b069649f5cd9&' +
          'mpshare=1&scene=23&srcid=0917J2IHeM4fxs3dHnJBhis4#rd',
      },
    ],
    backColor: '#0457c7',
  }),
  async created() {
    await this.toGetSite(); //获取主站信息
    this.loadMenu(); //获取菜单信息
    this.toGetModule(); //获取模块信息
    this.toGetJobInfo(); //获取招聘信息
    // this.toGetLinks(); //招聘信息+网站链接
    // this.self();
  },
  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) {
          console.log(col);
          if (col.type === 'content') {
            col.path = `/detail/${col.content_id}`;
          } else if (col.type === 'bugList') {
            console.log('in function:');
            col.path = `/newsList/menu/${col.content_id}?title=${col.title}`;
          } 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.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));
      // for (const item of modules) {
      //   if (`${item.is_use}` === `0`) {
      //     let res = await this.completeModules(item);
      //     item.children = res;
      //     this.$set(this, `${item.category}List`, item);
      //   }
      // }
      //获取了所有栏目
      let res = await this.completeAllModules(modules);
      // for (const item of modules) {
      //   this.$set(this, `${item.category}List`, item);
      // }
      this.$set(this, `modules`, modules);
    },
    //获取招聘信息
    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;
    },
    //合并请求模块
    async completeAllModules(modules) {
      let ids = modules.map(item => item.id);
      let result = await this.getAllColumn(ids);
      // result = _.flattenDeep(result);
      if (result.length > 0) {
        //整理栏目
        result = result.map(item => {
          if (item.type === 'column') item.path = `/newsList/module/${item.id}`;
          else item.path = `/newsList/module/${item.content_id}`;
          return item;
        });
        //请求信息
        let res = await this.getAllNews(result);
        //合并栏目和信息
        result = result.map(item => {
          let newsList = res.filter(fil => fil.parent_id === item.id);
          item.children = newsList;
          return item;
        });
        modules = modules.map(item => {
          let children = result.filter(fil => fil.parent_id === item.id);
          item.children = children;
          for (const sc of item.children) {
            let schildren = _.get(sc, `children`, []);
            sc.children = _.sortBy(schildren, i => {
              return new Date(_.get(i, `meta.createdA`)).getTime();
            });
            sc.children = sc.children.filter(f => f.is_use === '0');
          }
          // if (item.is_use === '0') {
          this.$set(this, `${item.category}List`, item);
          // }

          return item;
        });
      }
    },
    async onsaveClick({ title }) {
      if (title === '常见问题') {
        window.location.href =
          'https://mp.weixin.qq.com/s?__biz=MzAxMzg2ODE0NA==&mid=100000727&idx=1&sn=c2a38aca7482a930ceeff01779827faa&chksm=1b9d4ec82ceac7dee8cf6f89701e9af1fdede' +
          'ed6c7e5ecba20356b1584d6ee76b069649f5cd9&mpshare=1&scene=23&srcid=0917J2IHeM4fxs3dHnJBhis4#rd';
      } else {
        this.$router.push({ path: '/quiet', query: { title: title } });
      }
    },
  },
};
</script>

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