<template>
  <div class="app-container">
    <div style="width:100%">
      <task-info ref="taskInfo"></task-info>
      <el-divider content-position="left"><h3>公司信息</h3></el-divider>
      <el-form>
<!--        <div style="height: 100%; margin: 20px;">-->
<!--          <img :src="task.icon" style="width: 50px;">-->
<!--        </div>-->
        <el-form-item label="公司名称">
          {{company.name}}
        </el-form-item>
        <el-form-item label="公司简介">
          {{company.companyDesc}}
        </el-form-item>
      </el-form>
    </div>
    <div style="width: 1200px;display: inline-block;text-align: center;margin-top: 10px;">
    <el-button type="warning" class=" btn-primary" @click="claimTask" :disabled="(this.task.companyAppCount+this.task.personalAppCount)>=20" :title="(this.task.companyAppCount+this.task.personalAppCount)>=20 ? '应征者已满':''">我要应征任务</el-button>
    <el-button type="warning" @click="back" class=" btn-blue">返回</el-button>
    </div>
  </div>
</template>

<script>
  import {getById, claimTask} from "@/api/crowdsource/crowdsource";
  import {getInfo} from '@/api/login';
  import TesterType from "@/views/components/enum/TesterType";
  import OnsiteType from "../../components/enum/OnsiteType";
  import TestType from "../../components/enum/TestType";
  import ProcessStatus from "../../components/enum/ProcessStatus";
  import TaskStatus from "../../components/enum/TaskStatus";
  import TaskInfo from "../../components/TaskInfo";
  import store from "@/store";
  export default {
      name: "myCrowdsourceDetail",
      components:{TaskInfo, TestType, OnsiteType, TesterType, ProcessStatus, TaskStatus, TaskInfo},
      data(){
          return {
             task:{
               publisherId:0,
             },
             user:{},
             company:{}
          }
      },
      methods: {
        claimTask() {
          if (parseInt(store.getters.userId) == this.task.publisherId) {
            this.$alert("您是发布者，无法应征自己发布的任务。")
            return;
          }
          getInfo().then(response => {
            console.log('用户信息1---',response)
            this.user = response.user;
            if (this.user.testerStatus != 2) {//不是测试者
              this.$confirm('您不是测试者用户不能应征众包任务，点确定将前往个人中心进行测试者认证。', '提示', {type: 'warning'}).then(
                () => {
                  this.$router.push("/user/profile/userUpgrade");
                });
            } else {
              if (this.task.applicantType != 2){//2时个人测试者或企业测试都都可以
                if (this.task.applicantType == 0) { //要求个人测试者
                  if (this.user.companyStatus != 0) {
                    this.$alert("您是认证测试公司或正在申请成为认证测试公司，无法认证该任务，该任务只允许个人测试者应征。")
                    return ;
                  }
                } else  {//要求企业测试者
                  if (this.user.companyStatus != 2) {
                    this.$alert("对不起，您是个人测试者，该任务只允许认证测试公司应征。")
                    return ;
                  }
                }
              }
              this.$confirm("确定要应征此众包任务吗？").then(()=>{
                claimTask(this.task.taskId).then(response => {
                  if (response.data == 0) {
                    this.$alert("应征任务成功，请等待应征结果通知。")
                  } else  if (response.data == 1){
                    this.$alert("该任务应征者人数已满。")
                  } else  if (response.data == 2){
                    this.$alert("您已应征了该任务，请等待应征结果通知。")
                  }
                })
              })
            }
          })
        },
        back(){
          this.$store.dispatch("tagsView/delView", this.$route);
          this.$router.push({ path: "/crowdsource/crowdsourceMarket" });
        }
      },
      created() {
        this.id = this.$route.params && this.$route.params.id;
        getById(this.id).then(response => {
          this.task = response.task;
          this.company = response.company;
          this.$refs.taskInfo.setTask(this.task);
        })
      }
    }
</script>

<style scoped>

</style>
