<template>
  <div id="list">
    <login-Detail
      :topinfo="site"
      :menulist="menu"
      :bannerinfo="master.bannerinfo"
      :newsrightinfo="titleInfo"
      :leftmenu="leftmenu"
      :newsrightlist="list"
      :footinfo="site"
      :totalRow="totalRow"
      :type="type"
      @search="search"
    ></login-Detail>
  </div>
</template>

<script>
import loginDetail from '@publics/src/views/master/news/list.vue';
import { mapActions, mapState } from 'vuex';
import { masterInfo } from '@/config/master-info';
import _ from 'lodash';
export default {
  name: 'list',
  props: {},
  components: {
    loginDetail,
  },
  async created() {
    await this.getSite();
    await this.getMenu();
    this.masterData();
    await this.search();
  },
  data: () => ({
    site: {},
    master: { ...masterInfo },
    menu: [],
    list: [],
    totalRow: 0,
    leftmenu: [
      {
        title: '招聘会',
        path: `/master/jobs/jobfair`,
      },
      {
        title: '宣讲会',
        path: `/master/jobs/talk`,
      },
      {
        title: '招聘信息',
        path: `/master/jobs/jobinfo`,
      },
    ],
  }),
  computed: {
    type() {
      return this.$route.params.type;
    },
    titleInfo() {
      let object = {};
      if (this.$route.params.type === 'jobfair') {
        object = { title: '招聘会', english: 'JOBFAIR' };
      } else if (this.$route.params.type === 'talk') {
        object = { title: '宣讲会', english: 'CAMPUS' };
      } else {
        object = { title: '招聘信息', english: 'JOBINFO' };
      }
      return object;
    },
  },
  watch: {
    type: 'search',
  },
  methods: {
    ...mapActions(['siteOperation', 'columnOperation', 'menuOperation', 'jobfairOperation', 'jobinfoOperation', 'postTalksInfo']),
    //获取站点信息
    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 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 });
    },
    async search(page) {
      let skip = 0;
      if (page && _.isNumber(page)) {
        skip = (page - 1) * 4;
      }
      let newData = { skip: skip, limit: 4, ...this.searchInfo };
      let result;
      if (this.type === 'jobfair') result = await this.jobfairOperation({ type: 'list', data: newData });
      else if (this.type === 'talk') result = await this.postTalksInfo({ type: 'list', data: newData });
      else result = await this.jobinfoOperation({ type: 'list', data: newData });
      if (`${result.errcode}` === '0') {
        //给this=>vue的实例下在中的list属性，赋予result。data的值
        this.$set(this, 'list', result.data);
        this.$set(this, `totalRow`, result.total);
        window.location.hash = '#list_top';
      } else {
        this.$message.error(result.errmsg ? result.errmsg : 'error');
      }
    },
  },
};
</script>

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