<template>
  <div class="app-container">
    <div style=" width:100%">
      <task-info ref="taskInfo"></task-info>
      <el-divider  v-if="task.processStatus==0 || task.processStatus==1" content-position="left"><h3>任务提醒</h3></el-divider>
      <el-form label-width="150px" class="demo-ruleForm" label-position="left">
        <el-row v-if="task.processStatus==0">
          <el-col :span="24">
            <el-form-item label="发布有效期截止">
             {{task.expireTime}}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="task.processStatus==1">
          <el-col :span="24">
            <el-form-item label="工期截止">
              {{task.deadline}}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div>
        <el-divider content-position="left"><h3>应征者名单</h3></el-divider>
        <el-table v-loading="loading" :data="this.applicantList">
          <el-table-column
            label="序号"
            type="index"
            width="50">
          </el-table-column>
          <el-table-column label="应征时间" align="center" key="applyTime" prop="applyTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.applyTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="应征者类型" align="center" key="testerType" prop="testerType">
            <template slot-scope="scope">
              <span>{{ scope.row.testerType==2 ? "认证测试公司" : "个人测试者"}}</span>
            </template>
          </el-table-column>
          <el-table-column label="名称" align="center" key="userName" prop="userName"/>
          <el-table-column label="状态" align="center" key="status" prop="status" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ scope.row.status==0 ? "待定" : "选中"}}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="viewTesterDetail(scope.row.applicantId)"
              >查看详情
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-key"
                @click="assignTester(scope.row.applicantId)"
                :disabled="task.processStatus!=0"
              >选中
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div>
      <tester-detail ref="testerDetail" :status="task.processStatus" :taskId="task.taskId"
                     @closeDialog="closeDialog"></tester-detail>
    </div>
    <el-button type="warning" @click="back" class="btn-blue"  style="margin-right: 50%;margin-left: 50%; margin-top: 10px;">返回</el-button>
  </div>
</template>

<script>
  import {getById, getApplicant, assignTester} from "@/api/crowdsource/crowdsource";
  import TesterDetail from "@/views/components/TesterDetail";
  import TaskInfo from "../../../components/TaskInfo";

  export default {
    name: "crowdsourcePublishDetail",
    components: {TesterDetail,TaskInfo},
    data() {
      return {
        loading: false,
        task: {},
        applicantList: [],
        userId: null
      }
    },
    methods: {
      viewTesterDetail(userId) {
        this.userId = userId;
        this.$refs.testerDetail.viewDetail(userId);
      },
      assignTester(userId) {
        this.$confirm("确认要将众包任务指定给该测试者吗?").then(() => {
          let param = {};
          param.testerId = userId;
          param.taskId = this.task.taskId;
          assignTester(param).then(response => {
            this.applicantList = response.data;
            this.task = response.task;
          })
        })
      },
      closeDialog(isFresh) {
        if (isFresh) {
          getApplicant(this.id).then(response => {
            this.applicantList = response.data;
          })
          getById(this.id).then(response => {
            this.task = response.task;
            this.$refs.taskInfo.setTask(this.task);
          })
        }
      },
      back(){
        this.$store.dispatch("tagsView/delView", this.$route);
        this.$router.push({ path: "/crowdsource/myCrowdsource" });
      }
    },
    created() {
      this.id = this.$route.params && this.$route.params.id;
      getById(this.id).then(response => {
        this.task = response.task;
        this.$refs.taskInfo.setTask(this.task);
      })
      getApplicant(this.id).then(response => {
        this.applicantList = response.data;
      })
    }
  }
</script>

<style scoped>
</style>
