<template>
  <div id="detail">
    <recruitdetail-layout :needRight="false">
      <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>
        <jobfair-head title="招聘会信息" :info="info" @getTicket="getTicket"></jobfair-head>
        <jobfair-context title="招聘会说明" :info="info"></jobfair-context>
        <user-defined title="招聘会相关">
          <el-tabs v-model="tabs" type="card">
            <el-tab-pane label="企业列表" name="corp">
              <el-table :data="corpList" style="width: 100%" stripe :show-header="false">
                <el-table-column label="企业" prop="corpname">
                  <template v-slot="scoped">
                    <el-link @click="$router.push({ path: '/corp/detail', query: { id: scoped.row.corpid } })">{{ scoped.row.corpname }}</el-link>
                  </template>
                </el-table-column>
              </el-table>
            </el-tab-pane>
            <el-tab-pane label="招聘职位" name="jobs">
              <web-jobs :info="jobList" type="2" :origin="info.id" @search="searchJobs"> </web-jobs>
            </el-tab-pane>
          </el-tabs>
        </user-defined>
      </template>
    </recruitdetail-layout>
    <toLogin :display="loginDialog" @close="loginDialog = false" title="请登录"></toLogin>
  </div>
</template>

<script>
import userDefined from '@/layout/detail/user-defined.vue';
import toLogin from '@/components/to-login.vue';
import recruitdetailLayout from '@/layout/recruitdetail-layout.vue';
import jobfairHead from '@/layout/detail/jobfair/head.vue';
import jobfairContext from '@/layout/detail/jobfair/context.vue';
import webJobs from '@/layout/detail/web-jobs.vue';
import share from '@/layout/share.vue';
import { mapActions, mapState } from 'vuex';

export default {
  name: 'detail',
  props: {},
  components: {
    recruitdetailLayout,
    jobfairHead,
    jobfairContext,
    webJobs,
    share,
    toLogin,
    userDefined,
  },
  data: () => ({
    url2: 'http://yun-campus-res.oss-cn-shenzhen.aliyuncs.com/company/1536028846-7488.jpg',
    info: {},
    jobList: [],
    corpList: [],
    tabs: 'corp',
    loginDialog: false,
  }),
  created() {
    this.search();
  },
  computed: {
    ...mapState(['user']),
  },
  methods: {
    ...mapActions(['jobfairOperation', 'userOperation', 'ticketsOperation', 'fairInfoOperation', 'postOperation']),
    async search() {
      //1直接拿着参数发送请求
      let result = await this.jobfairOperation({ type: 'search', data: { id: this.$route.query.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.$route.query.id, skip: 0, limit: this.$limit } });
      if (`${result.errcode}` === '0') {
        this.$set(this, `corpList`, result.data);
        this.searchJobs();
      }
    },
    async searchJobs() {
      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);
      }
    },
    async getTicket() {
      if (!this.user.id) {
        this.loginDialog = true;
        return false;
      }
      let query = {};
      query.is_talk = `0`;
      query.studid = this.user.id;
      query.fairid = this.info.id;
      query.schid = this.info.schid;
      let result = await this.userOperation({ type: 'search', data: { studid: this.user.id } });
      let info = result.data.info;
      let body = {};
      body.schid = info.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>
