<template>
  <el-dialog :close-on-click-modal="false"
             :title="!this.taskForm.taskId == '' ? $t('处理') : $t('详情')"
             :visible.sync="visible"
             :append-to-body="true"
             width="60%">
    <el-descriptions :column="2" border size="small">
      <el-descriptions-item label="员工姓名" :label-style="labelStyle">{{ this.dataForm.tTsNew.staffName }}
      </el-descriptions-item>
      <el-descriptions-item label="培训状态" :label-style="labelStyle">
        <el-tag v-if="this.dataForm.tTsNew.planStatus == 2" size="small" type="warning">待考试</el-tag>
        <el-tag v-if="this.dataForm.tTsNew.planStatus == 3" size="small" type="success">已完成</el-tag>
        <el-tag v-if="this.dataForm.tTsNew.planStatus == 1" size="small" type="danger">已中止</el-tag>
        <el-tag v-else-if="this.dataForm.tTsNew.planStatus == 0" size="small" type="info">未完成</el-tag>
        <el-tag v-else-if="this.dataForm.tTsNew.planStatus == 4" size="small" type="danger">已中止待审批</el-tag>
        <el-tag v-else-if="this.dataForm.tTsNew.planStatus == 5" size="small" type="success">已完成待审批</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="开始时间">
        <template>
          <span>{{ parseTime(dataForm.tTsNew.startdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-descriptions-item>
      <el-descriptions-item label="结束时间" :label-style="labelStyle">
        <template>
          <span>{{ parseTime(dataForm.tTsNew.enddate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-descriptions-item>
      <el-descriptions-item label="导师姓名">
        {{ this.dataForm.tTsNew.mentorStaffName }}
      </el-descriptions-item>
      <el-descriptions-item label="岗位" :label-style="labelStyle">
        <template>
          <span v-if="dataForm.tTsNew.planType == 1" size="small">裂解</span>
          <span v-else-if="dataForm.tTsNew.planType == 2" size="small">压缩</span>
          <span v-else-if="dataForm.tTsNew.planType == 3" size="small">分离</span>
          <span v-else-if="dataForm.tTsNew.planType == 4" size="small">芳烃</span>
        </template>
      </el-descriptions-item>
      <el-descriptions-item label="考评表" :label-style="labelStyle">
        <template>
          <el-button  icon="el-icon-folder" style="color:#6e96fa;" @click="handleDownloadWord(dataForm.tTsNew, 'appraisalForm')" circle></el-button>
        </template>
      </el-descriptions-item>
      <el-descriptions-item label="备注" :span="2" :label-style="labelStyle">
        {{ this.dataForm.tTsNew.remarks }}
      </el-descriptions-item>
    </el-descriptions>
    <el-form ref="form" :model="form" label-position="left" :rules="rules" label-width="340px"
             style="margin-top: 30px">
      <div>
          <el-form-item label="评语" prop="mentorComments" v-if="taskName=='装置经理'">
            <el-input disabled type="textarea" v-model="form.mentorComments" placeholder="请输入评语"/>
          </el-form-item>
      </div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button v-if="!this.taskForm.taskId == ''" type="primary"
                 @click="dataFormSubmit(1)" :disabled="submitDisabled">{{ this.dataForm.tTsNew.planStatus==5?'通过':'中止' }}</el-button>
<!--      <el-button v-if="!this.taskForm.taskId == ''&&taskName==='装置经理'" type="danger"-->
<!--                 @click="dataFormSubmit(0)" :disabled="submitDisabled">{{ $t('拒绝') }}</el-button>-->
      <el-button @click="visible = false">{{ $t('返回') }}</el-button>
    </span>
    <div>
      <span>{{ $t('流转详情') }}</span>
      <el-table :data="historyList" border v-loading="historyLoading" style="width: 100%;">
        <el-table-column prop="taskName" header-align="center" align="center"
                         :label="$t('流程进度')"></el-table-column>
        <el-table-column prop="userName" header-align="center" align="center" :label="$t('姓名')"></el-table-column>
        <el-table-column prop="taskCreateTime" header-align="center" align="center"
                         :label="$t('开始时间')"></el-table-column>
        <el-table-column prop="taskEndTime" header-align="center" align="center"
                         :label="$t('结束时间')"></el-table-column>
        <el-table-column prop="comment" header-align="center" align="center"
                         :label="$t('审批意见')"></el-table-column>
      </el-table>
    </div>
  </el-dialog>
</template>

<script>
import {getHistorylist} from "@/api/ehs/approvedanger";
import {genAppraisalFormWord, getTsnew, handleTsnew} from "@/api/training/bccnew/tsnew";
import {getApprove} from "@/api/training/bccnew/tsApprove";

export default {
  name: "dsdtTraining-detail",
  data() {
    return {
      //label样式
      labelStyle: {'width': '200px'},
      unitOptions: [],
      //流转列表
      historyList: [],
      historyLoading: true,
      submitDisabled: false,
      taskName: '',
      visible: false,
      taskForm: {
        comment: '',
        taskId: '',
        files: '',
        govDate: '',
        govFileList: [],
        businessKey: '',
        tTsApprove: {}
      },
      devList: [],
      dataForm: {
        id: null,
        applicant: null,
        applicationTime: null,
        approver: null,
        approveTime: null,
        remarks: null,
        status: 0,
        delFlag: null,
        approverName: null,
        applicantName: null,
        apNo: null,
        processId: null,
        tTsNew:{
          mentorComments: null,
          remarks: null,
          planType: null,
          mentorStaffName: null,
          enddate: null,
          startdate: null,
          planStatus: null,
          staffName: null,
        }
      },
      form: {},
      rules: {},
      flag: false
    }
  },
  methods: {
    init(id, taskId, processId, taskName) {
      this.submitDisabled = false;
      this.reset();
      this.devList = [];
      this.dataForm = {
        id: null,
        applicant: null,
        applicationTime: null,
        approver: null,
        approveTime: null,
        remarks: null,
        status: 0,
        delFlag: null,
        approverName: null,
        applicantName: null,
        apNo: null,
        processId: null,
        tTsNew:{
          mentorComments: null,
          remarks: null,
          planType: null,
          mentorStaffName: null,
          enddate: null,
          startdate: null,
          planStatus: null,
          staffName: null,
        }
      };
      this.visible = true;
      this.taskName = taskName;
      this.taskForm.taskId = taskId;
      this.taskForm.businessKey = id;
      this.historyList = [];
      console.log(this.taskForm.taskId)
      this.dataForm.id = id || 0;
      this.form.id = id || 0;
      this.dataForm.processId = processId;
      getApprove(id).then(res => {
        this.dataForm = res.data;
        this.form = res.data.tTsNew;
        if(res.data.tTsNew.planStatus==4){
          this.form.mentorComments='该同志未能通过阶段性的培训考核，因此，中止其师徒关系。';
        }
        if(res.data.tTsNew.planStatus==5){
          this.form.mentorComments='该同志通过三个阶段的培训学习，对装置的基本操作已经胜任，并且通过了考核，达到一名合格操作人员所具备的优良素质，同意结束其师徒关系。';
        }
        this.devList.push(this.dataForm);
      });
      getHistorylist({processId: processId}).then(response => {
        this.historyList = response.rows;
        this.historyLoading = false
      });
    },
    dataFormSubmit(val) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.submitDisabled = true;
          this.taskForm.condition = val;
          this.taskForm.tTsApprove = this.dataForm;
          this.taskForm.tTsApprove.tTsNew=this.form;
          handleTsnew(this.taskForm).then(res => {
            this.msgSuccess("审核成功");
            this.visible = false;
            this.$emit('refreshDataList')
          })
        }
      })
    },
    reset() {
      this.form = {
        id: null,
      };
      this.resetForm("form");
    },
    // 文件下载处理
    handleDownloadWord(row, type) {
      let name;
      let url;
      console.log(row)
      if (type == 'mentorAgreement') {
        name = row.mentorAgreementWordPath;
        url = row.mentorAgreementWordPath;
        const a = document.createElement('a')
        a.setAttribute('download', name)
        a.setAttribute('target', '_blank')
        a.setAttribute('href', process.env.VUE_APP_BASE_API + url)
        a.click()
      } else if (type == 'targetPlan') {
        name = row.targetPlanWordPath;
        url = row.targetPlanWordPath;
        const a = document.createElement('a')
        a.setAttribute('download', name)
        a.setAttribute('target', '_blank')
        a.setAttribute('href', process.env.VUE_APP_BASE_API + url)
        a.click()
      } else if (type == 'appraisalForm') {
        genAppraisalFormWord(row.newId).then(res=>{
          name = res.msg;
          url = res.msg;
          console.log(url)
          const a = document.createElement('a')
          a.setAttribute('download', name)
          a.setAttribute('target', '_blank')
          a.setAttribute('href', process.env.VUE_APP_BASE_API + url)
          a.click()
        })
      }
    },
  }

}
</script>

<style scoped>

</style>
