<template>
  <div id="detail">
    <recruitdetail-layout>
      <template v-slot:mainInfoTop>
        <el-row>
          <el-col :span="24">
            <el-breadcrumb separator-class="el-icon-arrow-right">
              <el-breadcrumb-item :to="{ path: '/' }"
                ><i class="el-icon-s-home"></i>
                <span>网站首页</span>
              </el-breadcrumb-item>
              <el-breadcrumb-item><span>宣讲会</span></el-breadcrumb-item>
              <el-breadcrumb-item class="webDetail">详情</el-breadcrumb-item>
            </el-breadcrumb>
          </el-col>
        </el-row>
        <share></share>
      </template>
      <template v-slot:mainLeft>
        <talk-head title="宣讲会信息" :info="info" @getTicket="getTicket"></talk-head>
        <web-intro title="单位简介" :info="corpInfo"></web-intro>
        <talk-context title="招聘简章" :info="info"></talk-context>
        <web-jobs title="招聘职位" :info="jobList" type="2" :origin="info.id"> </web-jobs>
      </template>
      <template v-slot:mainRight>
        <corpInfo :corpid="info.corpid"></corpInfo>
      </template>
    </recruitdetail-layout>
    <toLogin :display="loginDialog" @close="loginDialog = false" title="请登录"></toLogin>
  </div>
</template>

<script>
import toLogin from '@/components/to-login.vue';
import corpInfo from '@/layout/corpInfo.vue';
import share from '@/layout/share.vue';
import webIntro from '@/layout/detail/web-intro.vue';
import webJobs from '@/layout/detail/web-jobs.vue';
import talkHead from '@/layout/detail/talk/talk-head.vue';
import talkContext from '@/layout/detail/talk/talk-context.vue';
import recruitdetailLayout from '@/layout/recruitdetail-layout.vue';
import { mapActions, mapState } from 'vuex';

export default {
  name: 'detail',
  props: {},
  components: {
    recruitdetailLayout,
    talkHead,
    talkContext,
    webIntro,
    webJobs,
    corpInfo,
    share,
    toLogin,
  },
  data: () => ({
    info: {},
    corpInfo: {},
    jobList: [],
    loginDialog: false,
  }),
  created() {
    this.search();
  },
  computed: {
    ...mapState(['user']),
  },
  methods: {
    ...mapActions(['postTalksInfo', 'corpOperation', 'userOperation', 'ticketsOperation', 'postOperation']),
    async search() {
      let result = await this.postTalksInfo({ type: 'search', data: { id: this.$route.query.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');
      }
    },
    async searchJobs() {
      let jobs = this.info.jobs;
      if (jobs.length > 0) {
        jobs.map(async item => {
          let result = await this.postOperation({ type: 'search', data: { id: item } });
          if (`${result.errcode}` === '0') {
            result.data ? this.jobList.push(result.data) : '';
          }
        });
      }
    },
    async getTicket() {
      if (!this.user.id) {
        this.loginDialog = true;
        return false;
      }
      let query = {};
      query.is_talk = `1`;
      query.studid = this.user.id;
      query.fairid = this.info.id;
      query.schid = this.user.schid;
      let result = await this.userOperation({ type: 'search', data: { studid: this.user.id } });
      let info = result.data.info;
      let body = {};
      body.schid = this.user.schid;
      body.year = info.year;
      body.xm = info.xm;
      body.xb = info.xb;
      body.yx = info.yx;
      body.zy = info.zy;
      body.xl = info.xl;
      body.syszd = info.syszd;
      body.zzmm = info.zzmm;
      result = await this.ticketsOperation({ type: 'add', data: { query: query, body: body } });
      this.$message({
        type: `${result.errcode}` === '0' ? 'success' : 'error',
        message: `${result.errcode}` === '0' ? '成功领到门票' : result.errmsg === '数据已存在' ? '已领取过门票,不能重复领取' : result.errmsg,
      });
    },
  },
};
</script>

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