<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>
        <jobs-head :info="info" @toDeliver="deliver"></jobs-head>
        <jobs-context title="职位描述" :info="info"></jobs-context>
        <web-intro title="单位简介" :info="corpInfo"></web-intro>
      </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 recruitdetailLayout from '@/layout/recruitdetail-layout.vue';
import jobsHead from '@/layout/detail/jobs/head.vue';
import jobsContext from '@/layout/detail/jobs/context.vue';
import webIntro from '@/layout/detail/web-intro.vue';
import { mapActions, mapState } from 'vuex';

export default {
  name: 'detail',
  props: {},
  components: {
    recruitdetailLayout,
    jobsHead,
    webIntro,
    jobsContext,
    corpInfo,
    share,
    toLogin,
  },
  data: () => ({
    info: {},
    corpInfo: {},
    loginDialog: false,
  }),
  created() {
    this.search();
  },
  computed: {
    ...mapState(['user']),
  },
  methods: {
    ...mapActions(['postOperation', 'corpOperation', 'resumesOperation', 'userOperation', 'lettersOperation']),
    async search() {
      let result = await this.postOperation({ 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);
      } 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 deliver() {
      if (!this.user.id) {
        this.loginDialog = true;
        return false;
      }
      let result = await this.userOperation({ type: 'search', data: { studid: this.user.id } });
      let info = result.data.info;
      result = await this.resumesOperation({ type: 'search', data: { id: this.user.id } });
      let resume = result.data;
      let query = {};
      query.post_id = this.info.id;
      query.resume_id = resume.id;
      let body = {};
      body.title = this.info.job_name;
      body.corpname = this.info.corpname;
      body.studname = this.user.xm;
      this.$route.query.type ? (body.type = this.$route.query.type) : '';
      this.$route.query.origin ? (body.origin = this.$route.query.origin) : '';
      result = await this.lettersOperation({ type: 'apply', 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>
