<template>
  <div id="detail">
    <login-Detail
      v-loading="loading"
      :topinfo="site"
      :menulist="menu"
      :type="type"
      :bannerinfo="master.bannerinfo"
      :noticedetail="info"
      :footinfo="site"
    ></login-Detail>
  </div>
</template>

<script>
import loginDetail from '@publics/src/views/master/news/detail.vue';
import { mapActions, mapState } from 'vuex';
import { masterInfo } from '@/config/master-info';
import _ from 'lodash';

export default {
  name: 'detail',
  props: {},
  components: {
    loginDetail,
  },
  async created() {
    await this.getSite();
    await this.getMenu();
    this.masterData();
    await this.search();
    this.$set(this, `loading`, false);
  },
  data: () => ({
    site: {},
    master: { ...masterInfo },
    info: {},
    menu: [],
    jobList: [],
    //talk用
    corpInfo: {},
    //jobfair用
    corpList: [],
    loading: true,
  }),
  computed: {
    type() {
      return this.$route.params.type;
    },
    id() {
      return this.$route.params.id;
    },
  },

  methods: {
    ...mapActions([
      'siteOperation',
      'columnOperation ',
      'newsOperation',
      'menuOperation',
      'jobfairOperation',
      'jobinfoOperation',
      'postTalksInfo',
      'postOperation',
      'fairInfoOperation',
      'corpOperation',
    ]),
    //获取站点信息
    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() {
      if (sessionStorage.getItem('menu')) {
        this.$set(this, `menu`, JSON.parse(sessionStorage.getItem('menu')));
        return;
      }
      //获取菜单
      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() {
      if (this.type === 'jobinfo') this.getJobInfo();
      if (this.type === 'talk') this.getTalk();
    },
    //jobinfo部分
    async getJobInfo() {
      //1直接拿着参数发送请求
      let result = await this.jobinfoOperation({ type: 'search', data: { id: this.id } });
      if (`${result.errcode}` === '0') {
        //给this=>vue的实例下在中的list属性，赋予result.data的值
        this.$set(this, `info`, result.data);
        // this.searchCorpInfo();
        this.searchJobs();
      } else {
        this.$message.error(result.errmsg ? result.errmsg : 'error');
      }
    },
    async searchJobs() {
      let result;
      let jobIds = this.info.jobs.length > 0 ? this.info.jobs : [];
      let jobList = [];
      for (const item of jobIds) {
        result = await this.postOperation({ type: 'search', data: { id: item } });
        if (`${result.errcode}` === '0') {
          jobList.push(result.data);
        }
      }
      this.$set(this, `jobList`, jobList);
    },
    //jobinfo结束
    //talk部分
    async getTalk() {
      let result = await this.postTalksInfo({ type: 'search', data: { id: this.id } });
      if (`${result.errcode}` === '0') {
        //给this=>vue的实例下在中的list属性，赋予result。data的值
        this.$set(this, 'info', result.data);
        this.searchCorpInfo(this.info.corpid);
        this.searchJobs();
      } else {
        this.$message.error(result.errmsg ? result.errmsg : 'error');
      }
    },
    async searchCorpInfo(corpid) {
      let result = await this.corpOperation({ type: 'search', data: { corpid: corpid } });
      if (`${result.errcode}` === '0') {
        //给this=>vue的实例下在中的list属性，赋予result。data的值
        this.$set(this, 'corpInfo', result.data);
        this.$set(this.corpInfo, `id`, this.info.corpid);
        this.$set(this.corpInfo, `corpname`, this.info.corpname);
      } else {
        this.$message.error(result.errmsg ? result.errmsg : 'error');
      }
    },
    //talk结束
    //jobfair部分
    async getJobfair() {
      //1直接拿着参数发送请求
      let result = await this.jobfairOperation({ type: 'search', data: { id: this.id } });
      if (`${result.errcode}` === '0') {
        //给this=>vue的实例下在中的list属性，赋予result.data的值
        this.$set(this, `info`, result.data);
        this.searchCorps();
      } else {
        this.$message.error(result.errmsg ? result.errmsg : 'error');
      }
    },
    async searchCorps() {
      let result = await this.fairInfoOperation({ type: 'searchCorp', data: { fairid: this.id } });
      if (`${result.errcode}` === '0') {
        this.$set(this, `corpList`, result.data);
        this.searchFairJobs();
      }
    },
    async searchFairJobs() {
      let jobsList = [];
      if (this.corpList.length > 0) {
        for (const item of this.corpList) {
          let result = await this.fairInfoOperation({ type: `searchJobs`, data: { id: item.id } });
          if (`${result.errcode}` === '0') {
            result.data.map(item => jobsList.push(item));
          }
        }
        this.$set(this, `jobList`, jobsList);
      }
    },
    //jobfair结束
    //info部分
    async getInfo() {
      let result = await this.newsOperation({ type: 'search', data: { id: this.id } });
      if (`${result.errcode}` === '0') {
        this.$set(this, `info`, result.data);
      }
    },
  },
};
</script>

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