<template>
  <div id="request">
    <el-row>
      <pages-one
        v-if="!loading"
        :topInfo="site"
        :notice="notice"
        :news="news"
        :enterList="self1List"
        :danweiList="self2List"
        :talksList="talksList"
        :talksoutList="talksOutList"
        :fairsList="jobfairList"
        :infoList="jobinfoList"
        :linkImgList="linkList"
        :lunboList="coopList"
      >
        <template #native>
          <top-menu></top-menu>
        </template>
        <template #calendar>
          <self-calendar></self-calendar>
        </template>
      </pages-one>
    </el-row>
  </div>
</template>

<script>
import _ from 'lodash';
import topMenu from '@/layout/layout-part/top-menu.vue';
import selfCalendar from '@/components/self-calendar.vue';
import pagesOne from '@publics/src/views/index.vue';
import { mapActions, mapState } from 'vuex';
export default {
  name: 'request',
  props: {},
  components: {
    pagesOne,
    topMenu,
    selfCalendar,
  },
  data: () => ({
    loading: true,
    limit: 4,
    newList: [
      { url: require('@/assets/a1.jpg'), id: 1, name: '应用技术学院召开首届专场人才招聘会' },
      { url: require('@/assets/a2.jpg'), id: 2, name: '浙江湖州城市综合推介会暨2019年重点用人单位招聘会在我校举行' },
      { url: require('@/assets/a3.jpg'), id: 3, name: '我校与亚信科技（中国）有限公司举行校企战略合作签约暨客座教授聘任仪式' },
      { url: require('@/assets/a4.jpg'), id: 4, name: '求职创业补贴学生名单公示' },
      { url: require('@/assets/a5.jpg'), id: 5, name: '学校隆重举行2019届毕业生春季供需洽谈会' },
    ],
    talksList: [],
    jobfairList: [],
    talksOutList: [],
    jobinfoList: [],
    site: {},
    news: [],
    notice: [],
    self1List: [],
    self2List: [],
    coopList: [],
    linkList: [],
  }),
  async created() {
    await this.getSite();
    // this.search();
    this.getModule();
    // this.getLink();
  },
  computed: {},
  methods: {
    ...mapActions([
      'jobfairOperation',
      'postTalksInfo',
      'jobinfoOperation',
      'newsOperation',
      'siteOperation',
      'moduleOperation',
      'columnOperation',
      'linkOperation',
    ]),
    async getSite() {
      let site = sessionStorage.getItem('site');
      if (!site) {
        let result = await this.siteOperation({ type: 'search', data: { site: this.$site } });
        if (`${result.errcode}` === `0`) {
          sessionStorage.setItem('site', JSON.stringify(result.data));
          this.$set(this, `site`, result.data);
        }
      } else {
        this.$set(this, `site`, JSON.parse(site));
      }
      this.$set(this, `loading`, false);
    },
    async search() {
      // 1直接拿着参数发送请求
      let result = await this.postTalksInfo({ type: 'list', data: { schid: this.$site } });
      if (`${result.errcode}` === '0') {
        //给this=>vue的实例下在中的list属性，赋予result。data的值
        this.$set(this, `talksList`, result.data);
      } else {
        this.$message.error(result.errmsg ? result.errmsg : 'error');
      }
      result = await this.jobfairOperation({ type: 'list', data: { schid: this.$site } });
      if (`${result.errcode}` === '0') {
        //给this=>vue的实例下在中的list属性，赋予result。data的值
        this.$set(this, `jobfairList`, result.data);
      } else {
        this.$message.error(result.errmsg ? result.errmsg : 'error');
      }
      result = await this.postTalksInfo({ type: 'list', data: {} });
      if (`${result.errcode}` === '0') {
        //给this=>vue的实例下在中的list属性，赋予result。data的值
        this.$set(this, `talksOutList`, result.data);
      } else {
        this.$message.error(result.errmsg ? result.errmsg : 'error');
      }
      result = await this.jobinfoOperation({ type: 'list', data: { limit: this.limit } });
      if (`${result.errcode}` === '0') {
        //给this=>vue的实例下在中的list属性，赋予result。data的值
        this.$set(this, 'jobinfoList', result.data);
      } else {
        this.$message.error(result.errmsg ? result.errmsg : 'error');
      }
    },
    async getModule() {
      //获取分站所有模块 TODO:site=>_tenant
      let result = await this.moduleOperation({ type: 'list' });
      if (`${result.errcode}` === '0') {
        let moduleList = result.data;
        for (let item of moduleList) {
          //item为模块信息,拿着模块信息去查该模块下有什么栏目
          if (`${item.is_use}` === '0') {
            item = await this.getColumn(item);
            this.makeList(item);
          } // console.log(item);
        }
      }
    },
    async getColumn(item) {
      // console.log(item);
      // console.log(this.site);
      if (!item.category.includes('self')) {
        let res = await this.columnOperation({ type: 'list', data: { parent_id: item.id } });
        if (`${res.errcode}` === '0') {
          //组合path:res.data内容都为栏目.所以,点击这些栏目显示的列表应该是信息列表,需要用栏目的id作为查询信息的parten_id查出不同栏目的信息
          for (const col of res.data) {
            col.path = `/info/list/${col.id}`;
            //再将栏目下的前几条数据查出来(暂定limit=6)
            col.children = await this.getNews(col, '1');
          }
          item.children = res.data;
        }
      } else if (this.site && this.site.bug_column) {
        let selfUseList = JSON.parse(this.site.bug_column);
        let res = selfUseList.filter(fil => {
          if (fil) return fil.parent_id === item.id;
        });
        for (const col of res) {
          col.id = col.column;
          col.path = `/info/list/${col.id}`;
          col.children = await this.getNews(col, '0');
        }
        item.children = res;
      }
      return item;
    },
    async getNews(item, news_type) {
      //这个item是栏目列表,循环查每个栏目6条信息
      let res = await this.newsOperation({ type: 'list', data: { parent_id: item.id, skip: 0, limit: 6 } });
      if (`${res.errcode}` === '0') {
        return res.data;
      }
    },
    makeList(item) {
      if (!item) return;
      if (item.category === 'news') {
        let arr = [];
        let colObject = {};
        for (const col of item.children) {
          if (!colObject.id) colObject = col;
          for (const news of col.children) {
            arr.push(news);
          }
        }
        let object = { ...JSON.parse(JSON.stringify(item)), infoList: arr, column: colObject };
        this.$set(this, `news`, object);
      } else if (item.category === 'notice') {
        let arr = [];
        let colObject = {};
        for (const col of item.children) {
          if (!colObject.id) colObject = col;
          for (const news of col.children) {
            arr.push(news);
          }
        }
        let object = { ...JSON.parse(JSON.stringify(item)), infoList: arr, column: colObject };
        this.$set(this, `notice`, object);
      } else if (item.category === 'self1') {
        this.$set(this, `self1List`, item.children);
      } else if (item.category === 'self2') {
        this.$set(this, `self2List`, item.children);
      }
    },
    async getLink() {
      //合作单位
      let coop = await this.linkOperation({ type: 'list', data: { site: this.$site, type: 'cooperation' } });
      if (`${coop.errcode}` === '0') {
        this.$set(this, `coopList`, coop.data);
      }
      //友情链接
      let link = await this.linkOperation({ type: 'list', data: { site: this.$site, type: 'link' } });
      if (`${coop.errcode}` === '0') {
        this.$set(this, `linkList`, link.data);
      }
    },
  },
};
</script>

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