<template>
  <div id="request">
    <el-row v-if="user !== 'master'">
      <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>
    <el-row v-else>
      <master
        v-if="!loading"
        :topinfo="site"
        :menulist="menu"
        :bannerinfo="master.bannerinfo"
        :newsList="news.infoList"
        :noticeinfo="master.noticeinfo"
        :noticelist="notice.infoList"
        :huodonginfo="master.jobsinfo"
        :fairsList="jobfairList"
        :talksList="talksOutList"
        :infoList="jobinfoList"
        :recruitList="self1List"
        :otherList="self2List"
        :fasttop="master.fasttop"
        :fastlist="master.fastlist"
        :contactinfo="master.contactinfo"
        :content="site.content"
        :companytop="master.companytop"
        :unittop="master.unittop"
        :unitlist="coopList"
        :footinfo="site"
        :rilitop="master.rilitop"
      ></master>
      <!--  :companylist="companylist" -->
    </el-row>
  </div>
</template>

<script>
import _ from 'lodash';
import { jobMenu } from '@/config/jobs-menu';
import master from '@publics/src/views/master/index/index.vue';
import topMenu from '@/layout/layout-part/top-menu.vue';
import selfCalendar from '@/components/self-calendar.vue';
import pagesOne from '@publics/src/views/index.vue';
//master部分
import { masterInfo } from '@/config/master-info';
import { mapActions, mapState } from 'vuex';
export default {
  name: 'request',
  props: {},
  components: {
    pagesOne,
    topMenu,
    selfCalendar,
    master,
  },
  data: () => ({
    user: 'master',
    //menu部分
    activeIndex: '1',
    menu: [],
    jobs: jobMenu,
    loading: true,
    limit: 4,
    newList: [],
    talksList: [],
    jobfairList: [],
    talksOutList: [],
    jobinfoList: [],
    site: {},
    news: [],
    notice: [],
    self1List: [],
    self2List: [],
    coopList: [],
    linkList: [],
    //master数据
    master: { ...masterInfo },
  }),
  async created() {
    await this.checkUser();
    await this.getSite();
    this.search();
    this.getModule();
    this.getLink();
    if (this.user === 'master') this.masterData();
    await this.getMenu();
    this.$set(this, `loading`, false);
  },
  computed: {},
  methods: {
    ...mapActions([
      'jobfairOperation',
      'postTalksInfo',
      'jobinfoOperation',
      'newsOperation',
      'siteOperation',
      'moduleOperation',
      'columnOperation',
      'linkOperation',
      'menuOperation',
    ]),
    //检查用户身份
    checkUser() {
      let schId = sessionStorage.getItem('schId');
      if (schId === 'master') {
        this.$set(this, `user`, schId);
      }
    },
    //获取站点信息
    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));
      }
    },
    //查询招聘类信息
    async search() {
      // 1直接拿着参数发送请求
      let result = await this.postTalksInfo({ type: 'list', data: { schid: this.user === 'master' ? null : 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.user === 'master' ? null : 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');
      }
    },
    //获取固定的4个模块
    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: 4, news_type: news_type } });
      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);
      }
    },
    //检查+读取菜单
    async loadMenu() {
      let menu = sessionStorage.getItem('menu');
      if (menu) {
        this.$set(this, `menu`, JSON.parse(menu));
        return;
      } else this.getMenu();
    },
    //获取菜单
    async getMenu() {
      //获取菜单
      let result = await this.menuOperation({ type: 'list', data: { site: this.$site } });
      if (`${result.errcode}` === '0') {
        //获取菜单的栏目
        let allMenu = result.data;
        for (let item of allMenu) {
          if (item.type === 'content') {
            item.path = `/info/detail?id=${item.content_id}`;
          } else if (item.type !== 'url') {
            let res = await this.completeMenu(item);
            item.children = res;
          }
        }
        sessionStorage.setItem('menu', JSON.stringify(allMenu));
        this.$set(this, `menu`, allMenu);
      }
    },
    //将菜单完善至栏目级别
    async completeMenu(item) {
      let res = await this.columnOperation({ type: 'list', data: { parent_id: item.id, site: item.site } });
      if (`${res.errcode}` === '0') {
        //组合path:res.data内容都为栏目.所以,点击这些栏目显示的列表应该是信息列表,需要用栏目的id作为查询信息的parten_id查出不同栏目的信息
        for (const col of res.data) {
          if (col.type === 'content') col.path = `/info/detail?id=${col.content_id}`;
          else if (col.type !== 'url') col.path = `/info/list/${col.id}`;
        }
        return res.data;
      }
    },
    //主站信息组合
    masterData() {
      this.$set(this.master, `bannerinfo`, { banner: this.site.banner });
    },
  },
};
</script>

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