<template >
  <div class="app-container">
    <!-- 顶部工具栏 -->
    <el-row :gutter="10">
      <div class="top-right-btn">
        <el-row>
          <el-tooltip  effect="dark" :content="$t('tagsView.refresh')" placement="top">
            <el-button size="mini" circle icon="el-icon-refresh" @click="getList()" />
          </el-tooltip>
        </el-row>
      </div>
    </el-row>
    <el-table v-loading="loading" :data="approvedangerList" @selection-change="handleSelectionChange" :height="clientHeight" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('流程')+' '+ $t('ID')" align="center" prop="processId" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('流程')+' ' + $t('名称')" align="center" prop="processName" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('流程')+' ' + $t('创建时间')" align="center" prop="processCreateTime" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('待办任务')+' ' + $t('名称')" align="center" prop="taskName" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('待办任务')+' ' + $t('创建时间')" align="center" prop="taskCreateTime" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('申请编号')" align="center" prop="apNo" :show-overflow-tooltip="true">
<!--        <template slot-scope="scope">-->
<!--          <span v-if="scope.row.processName === ($t('事件') + $t('审批流程')) ">{{scope.row.tApproveAccident.approveNo}}</span>-->
<!--          <span v-if="scope.row.processName === ($t('重大隐患') + $t('审批流程') )|| scope.row.processName === ($t('普通隐患') + $t('审批流程') )">{{scope.row.tApprove.approveNo}}</span>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="addOrUpdateHandle(scope.row)"
          >{{ $t('处理') }}</el-button>
          <el-button
            size="mini"
            type="text"
            @click="processImg(scope.row.processId)"
          >{{ $t('流程图') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 弹窗, 新增 / 修改 -->
    <div v-if="addOrUpdateVisible">
      <el-dialog  :close-on-click-modal="false" v-dialogDrag :visible.sync="addOrUpdateVisible" :destroy-on-close="true">
        <add-or-update v-if="this.infoprocessName === ($t('重大隐患')+' '+ $t('审批流程') )|| this.infoprocessName === ($t('普通隐患')+' ' + $t('审批流程') )" :info="approveInfo"></add-or-update>
        <accident v-if="this.infoprocessName === ($t('事件')+ $t('审批流程'))" :info="approveInfo" @recordInfo="approveChange($event)"></accident>
        <el-form>
          <el-form-item :label="$t('上传措施前照片')" prop="before" v-if="this.infoTaskName === '执行人'">
            <el-upload
              ref="uploadbefore"
              :limit="1"
              :headers="uploadbefore.headers"
              :action="uploadbefore.url"
              :disabled="uploadbefore.isUploading"
              :on-progress="handleFileUploadProgressBefore"
              :on-success="handleFileSuccessBefore"
              :auto-upload="true"
              drag
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">
                {{ $t('将文件拖到此处，或') }}
                <em>{{ $t('点击上传') }}</em>
              </div>
            </el-upload>
          </el-form-item>
          <el-form-item :label="$t('上传措施后照片')" prop="after" v-if="this.infoTaskName === $t('执行人')">
            <el-upload
              ref="uploadafter"
              :limit="1"
              :headers="uploadafter.headers"
              :action="uploadafter.url"
              :disabled="uploadafter.isUploading"
              :on-progress="handleFileUploadProgressAfter"
              :on-success="handleFileSuccessAfter"
              drag
            >
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">
                {{ $t('将文件拖到此处，或') }}
                <em>{{ $t('点击上传') }}</em>
              </div>
            </el-upload>
          </el-form-item>
          <el-form-item :label="$t('审批意见')" prop="comment">
            <el-input v-model="comment" :placeholder="$t('请输入') + $t('审批意见')" maxlength="100" show-word-limit></el-input>
          </el-form-item>
        </el-form>
        <span v-if="this.infoprocessName ===  ($t('重大隐患')+' ' + $t('审批流程') ) || this.infoprocessName === ($t('普通隐患')+' '  + $t('审批流程') )" slot="footer">
          <span v-if="this.infoTaskName === $t('执行人')" class="dialog-footer">
            <el-button type="primary" @click="dataFormSubmit(1)">{{ $t('已完成') }}</el-button>
            <el-button type="danger" @click="dataFormSubmit(0)">{{ $t('无法执行') }}</el-button>
          </span>
          <span v-if="this.infoTaskName === $t('验证人')" class="dialog-footer">
            <el-button type="primary" @click="dataFormSubmit(1)">{{ $t('通过') }}</el-button>
            <el-button type="danger" @click="dataFormSubmit(0)">{{ $t('无效') }}</el-button>
            <el-button type="warning" @click="dataFormSubmit(2)">{{ $t('未达标') }}</el-button>
          </span>
          <span v-if="this.infoTaskName === $t('记录人')" class="dialog-footer">
            <el-button type="primary" @click="dataFormSubmit(1)">{{ $t('重新提交') }}</el-button>
          </span>
          <span v-if="this.infoTaskName === $t('确认')" class="dialog-footer">
            <el-button type="primary" @click="dataFormSubmit(1)">{{ $t('确认') }}</el-button>
            <el-button type="danger" @click="dataFormSubmit(0)">{{ $t('确认')+' ' + $t('不通过') }}</el-button>
          </span>
        </span>
        <span v-if="this.infoprocessName === ($t('事件') + $t('审批流程'))" slot="footer">
          <span v-if="this.infoTaskName === $t('记录人')" class="dialog-footer">
            <el-button type="primary" @click="dataFormSubmit(1)">{{ $t('提交') }}</el-button>
          </span>
        </span>
      </el-dialog>
    </div>

    <add-or-update v-if="specDealVisible" ref="specDeal" @refreshDataList="getList"></add-or-update>
    <spec-modify v-if="specModifyDealVisible" ref="specModifyDeal" @refreshDataList="getList"></spec-modify>
    <spec-training-plan v-if="specTrainingPlanDealVisible" ref="specTrainingPlanDeal" @refreshDataList="getList"></spec-training-plan>
    <process-img v-if="processImgVisible" ref="processImg" @refreshDataList="getList"></process-img>
    <intact-resolve v-if="intactResolveVisible" ref="intactResolveDeal" @refreshDataList="getList"></intact-resolve>
    <kekao-resolve v-if="kekaoResolveVisible" ref="kekaoResolveDeal" @refreshDataList="getList"></kekao-resolve>
    <spec-maintenance v-if="specMaintenanceVisible" ref="specMaintenance" @refreshDataList="getList"></spec-maintenance>
    <invoice-detail v-if="invoiceDetailVisible" ref="invoiceDetail" @refreshDataList="getList"></invoice-detail>
    <offlinevalve-detail v-if="offlinevalveDetailVisible" ref="offlinevalveDetail" @refreshDataList="getList"></offlinevalve-detail>
    <safetychange-detail v-if="safetychangeDetailVisible" ref="safetychangeDetail" @refreshDataList="getList"></safetychange-detail>
    <sai-apply-detail v-if="saiApplyVisible" ref="saiApplyDetail" @refreshDataList="getList"></sai-apply-detail>
    <sems-week-check-detail v-if="semsWeekcheckDetailVisible" ref="semsWeekcheckDetail" @refreshDataList="getList"></sems-week-check-detail>
    <dsdt-training-detail v-if="dsdtTrainingDetailVisible" ref="dsdtTrainingDetail" @refreshDataList="getList"></dsdt-training-detail>
    <dsdt-nopass-detail v-if="dsdtNopassDetailVisible" ref="dsdtNopassDetail" @refreshDataList="getList"></dsdt-nopass-detail>
    <pssr-approve-detail v-if="pssrApproveDetailVisible" ref="pssrApproveDetail" @refreshDataList="getList"></pssr-approve-detail>
    <pssr-mgr-approve-detail v-if="pssrMgrApproveDetailVisible" ref="pssrMgrApproveDetail" @refreshDataList="getList"></pssr-mgr-approve-detail>

  </div>
</template>

<script>
import {getPendinglist, handleApprovedanger} from "@/api/ehs/approvedanger";
import AddOrUpdate from './spec-detail';
import SpecModify from './specModify-deal';
import SpecTrainingPlan from './specTrainingPlan-deal';
import IntactResolve from '../approveDetail/intact-resolve';
import Accident from '../approveaccidentDetail/index';
import ProcessImg from '../processImg/index';
import {treeselect} from "@/api/system/dept";
import {getToken} from "@/utils/auth";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import SpecMaintenance from "@/views/approve/pending/specMaintenance-deal";
import KekaoResolve from '../approveDetail/kekao-resolve';
import InvoiceDetail from "@/views/approve/approveDetail/invoice-detail";
import OfflinevalveDetail from "@/views/approve/approveDetail/offlinevalve-detail";
import SafetychangeDetail from "@/views/approve/approveDetail/safetychange-detail";
import SaiApplyDetail from "@/views/approve/approveDetail/sai-apply-detail";
import SemsWeekCheckDetail from "@/views/approve/approveDetail/semsWeekCheck-detail";
import DsdtTrainingDetail from "@/views/approve/approveDetail/dsdtTraining-detail.vue";
import DsdtNopassDetail from "@/views/approve/approveDetail/dsdtNoPass-detail.vue";
import PssrApproveDetail from "@/views/approve/approveDetail/pssrApprove-detail.vue";
import PssrMgrApproveDetail from "@/views/approve/approveDetail/pssrMgrApprove-detail.vue";

export default {
    name: "Pending",
    components: {
      PssrMgrApproveDetail,
      PssrApproveDetail,
      DsdtNopassDetail,
      DsdtTrainingDetail,
      SemsWeekCheckDetail,
      SaiApplyDetail,
      SafetychangeDetail,
      OfflinevalveDetail,
      Treeselect,
      AddOrUpdate,
      SpecModify,
      Accident,
      SpecTrainingPlan,
      ProcessImg,
      IntactResolve,
      SpecMaintenance,
      KekaoResolve,
      InvoiceDetail
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        //弹窗界面是否开启
        addOrUpdateVisible: false,
        processImgVisible: false,
        specDealVisible: false,
        specModifyDealVisible: false,
        specTrainingPlanDealVisible: false,
        intactResolveVisible:false,
        kekaoResolveVisible:false,
        specMaintenanceVisible: false,
        invoiceDetailVisible:false,
        offlinevalveDetailVisible:false,
        safetychangeDetailVisible:false,
        saiApplyVisible:false,
        semsWeekcheckDetailVisible: false,
        dsdtTrainingDetailVisible: false,
        dsdtNopassDetailVisible: false,
        pssrApproveDetailVisible: false,
        pssrMgrApproveDetailVisible: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: false,
        // 总条数
        total: 0,
        // 我的申请表格数据
        approvedangerList: [],
        // 弹出层标题
        title: "",
        // 部门树选项
        deptOptions: undefined,
        clientHeight:300,
        // 是否显示弹出层
        open: false,
        // 装置名称字典
        plantCodeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
          plantCode: null,
          recorderId: null,
          hiddendangerLevel: null,
          hiddendangerContent: null,
          measures: null,
          completeTime: null,
          status: null,
          creattime: null,
          endtime: null,
          processId: null,
          approveNo: null,
          deptId: null,
        },
        // 措施前上传照片参数
        uploadbefore: {
          file: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/common/upload",
          // 报告附件上传位置编号
          ids: 0,
          commonfileList: null,
        },
        // 措施后上传照片参数
        uploadafter: {
          file: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/common/upload",
          // 报告附件上传位置编号
          ids: 0,
          commonfileList: null,
        },
        // 表单参数
        form: {},
        //事件记录人更新内容
        recordInfo: {},
        //处理数据传输
        approveInfo: null,
        //审批显示按键判断依据
        infoprocessName: null,
        infoTaskName: null,
        //上传措施前后照片url，文件名称
        beforeUrl: null,
        beforeFilename: null,
        afterUrl: null,
        afterFilename: null,
        //审批备注
        comment: null
      };
    },
    watch: {
      // 根据名称筛选部门树
      deptName(val) {
        this.$refs.tree.filter(val);
      }
    },
    created() {
      //设置表格高度对应屏幕高度
      this.$nextTick(() => {
        this.clientHeight = document.body.clientHeight -250
      })
      this.getList();
      this.getTreeselect();
      this.getDicts("PLANT_DIVIDE").then(response => {
        this.plantCodeOptions = response.data;
      });
    },
    methods: {
      /** 查询隐患申请列表 */
      getList() {
        this.loading = true;
        getPendinglist(this.queryParams).then(response => {
          this.approvedangerList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 查询部门下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      // 装置名称字典翻译
      plantCodeFormat(row, column) {
        return this.selectDictLabel(this.plantCodeOptions, row.plantCode);
      },
      // 取消按钮
      cancel() {
        this.addOrUpdateVisible = false;
        this.reset();
      },
      /** 事件子组件传值 */
      approveChange(value) {
        this.recordInfo = value
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          plantCode: null,
          recorderId: null,
          hiddendangerLevel: null,
          hiddendangerContent: null,
          measures: null,
          completeTime: null,
          status: 0,
          creattime: null,
          endtime: null,
          processId: null,
          approveNo: null,
          deptId: null,
          delFlag: null,
          submitType: null,
          commont: null,
          accidentLevel: null,
          executorId: null

        };
        this.resetForm("form");
      },
      //操作审批流程
      addOrUpdateHandle (row) {
        if (row.processName == "特种设备审核"){
          this.specDealVisible = true
          this.$nextTick(() => {
            console.log(row.taskName)
            this.$refs.specDeal.init(row.specApprove.id, row.taskId, row.processId,row.taskName)
          })
        }else if (row.processName == "年度检查报告申请"){
          this.specDealVisible = true
          this.$nextTick(() => {
            console.log(row.taskName)
            this.$refs.specDeal.init(row.specApprove.id, row.taskId, row.processId,row.taskName)
          })
        }else if (row.processName == "月度检查"){
          this.specDealVisible = true
          this.$nextTick(() => {
            console.log(row.taskName)
            this.$refs.specDeal.init(row.specApprove.id, row.taskId, row.processId,row.taskName)
          })
        }else if (row.processName == "设备修改/删除审核" || row.processName == "设备新增/删除审核"){
          this.specModifyDealVisible = true
          this.$nextTick(() => {
            console.log(row.taskName)
            this.$refs.specModifyDeal.init(row.approveSpecModify.id, row.taskId, row.processId,row.taskName)
          })
        }else if (row.processName == "专项培训计划审核"){
          this.specTrainingPlanDealVisible = true
          this.$nextTick(() => {
            console.log(row.taskName)
            this.$refs.specTrainingPlanDeal.init(row.approveObj.id, row.taskId, row.processId,row.taskName)
          })
        }else if (row.processName == "设备维修审批流程"){
          this.specMaintenanceVisible = true
          this.$nextTick(() => {
            console.log(row.taskName)
            this.$refs.specMaintenance.init(row.approveObj.id, row.taskId, row.processId,row.taskName)
          })
        }else if (row.processName == "设备变更申请"){
          this.intactResolveVisible = true
          this.$nextTick(() => {

            console.log(row)

            this.$refs.intactResolveDeal.init(row.approveObj.id, row.taskId, row.processId,row.taskName)
          })
        }else if (row.processName == "可靠性管理申请"){
          this.kekaoResolveVisible = true
          this.$nextTick(() => {

            console.log(row)

            this.$refs.kekaoResolveDeal.init(row.approveObj.id, row.taskId, row.processId,row.taskName)
          })
        } else if (row.processName == "预约开票审批流程"){
          this.invoiceDetailVisible = true
          this.$nextTick(() => {
            console.log(row)
            console.log(row.approveObj)
            this.$refs.invoiceDetail.init(row.approveObject.id, row.taskId, row.processId,row.taskName)
          })
        } else if (row.processName == "安全阀离线/切出申请流程") {
          this.offlinevalveDetailVisible = true
          this.$nextTick(() => {
            this.$refs.offlinevalveDetail.init(row.approveObject.id, row.taskId, row.processId, row.taskName,true)
          })
        } else if (row.processName == "组织保护措施状态变更申请流程") {
          this.safetychangeDetailVisible = true
          this.$nextTick(() => {
            this.$refs.safetychangeDetail.init(row.approveObject.id, row.taskId, row.processId, row.taskName,true)
          })
        } else if (row.processName == "SAI开项申请流程") {
          this.saiApplyVisible = true
          this.$nextTick(() => {
            this.$refs.saiApplyDetail.init(row.approveObject.saiApplyId, row.taskId, row.processId, row.taskName)
          })
        }else if (row.processName == "每周安全排查报告") {
          this.semsWeekcheckDetailVisible = true
          this.$nextTick(() => {
            this.$refs.semsWeekcheckDetail.init(row.approveObject.id, row.taskId, row.processId, row.taskName)
          })
        } else if (row.processName == "导师带徒经理审批") {
          this.dsdtTrainingDetailVisible = true
          this.$nextTick(() => {
            this.$refs.dsdtTrainingDetail.init(row.approveObject.id, row.taskId, row.processId, row.taskName)
          })
        }  else if (row.processName == "导师带徒学员成绩不合格审批") {
          this.dsdtNopassDetailVisible = true
          this.$nextTick(() => {
            this.$refs.dsdtNopassDetail.init(row.approveObject.id, row.taskId, row.processId, row.taskName)
          })
        } else if (row.processName.includes("PSSR装置经理审批")) {
          this.$router.push({name: 'mgrApprove', query: {aboveallId:row.approveObject.aboveallId, taskId:row.taskId, processId:row.processId, taskName:row.taskName}});
        } else if (row.processName.includes("PSSR") && !row.processName.includes("PSSR装置经理审批")) {
          this.pssrApproveDetailVisible = true
          this.$nextTick(() => {
            this.$refs.pssrApproveDetail.init(row.approveObject.subId, row.taskId, row.processId, row.taskName,row.approveObject.sn)
          })
        } else {
          this.approveInfo = row;
          this.infoTaskName = row.taskName;
          this.infoprocessName = row.processName;
          this.addOrUpdateVisible = true;
        }
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      // 表单提交
      dataFormSubmit (val) {
        //相关信息
        this.form = this.approveInfo
        this.form.submitType = val
        this.form.comment = this.comment
        // 审批确认后，申请人填写相关信息
        if (this.form.processName === (this.$t('事件') + this.$t('审批流程'))) {
          this.form.accidentLevel = this.recordInfo.accidentLevel;
          this.form.measures = this.recordInfo.measures;
          this.form.executorId = this.recordInfo.executorId;
          this.form.completeTime = this.recordInfo.completeTime;
          console.info("accidentLevel:" + this.form.accidentLevel)
          console.info("measures:" + this.form.measures)
          console.info("executorId:" + this.form.executorId)
          console.info("completeTime:" + this.form.completeTime)
        }
        //提交审批
        this.form.beforeUrl = this.beforeUrl
        this.form.beforeFilename = this.beforeFilename
        this.form.afterUrl = this.afterUrl
        this.form.afterFilename = this.afterFilename
        handleApprovedanger(this.form).then(response => {
          this.msgSuccess(this.$t('处理成功'));
          this.addOrUpdateVisible = false;
          this.getList();
        });
        this.$nextTick(function () {
          this.comment = ''
        })
      },
      // 文件上传中处理
      handleFileUploadProgressBefore(event, file, fileList) {
        this.uploadbefore.isUploading = true;
      },
      handleFileUploadProgressAfter(event, file, fileList) {
        this.uploadafter.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccessBefore(response, file, fileList) {
        this.uploadbefore.isUploading = false;
        //this.$refs.uploadbefore.clearFiles();
        this.beforeUrl = response.url;
        this.beforeFilename = response.fileName;
      },
      handleFileSuccessAfter(response, file, fileList) {
        this.uploadafter.isUploading = false;
        //this.$refs.uploadafter.clearFiles();
        this.afterUrl = response.url;
        this.afterFilename = response.fileName;
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.uploadbefore.submit()
        this.$refs.uploadafter.submit()
      },
      processImg (processId) {
        this.processImgVisible = true
        this.$nextTick(() => {
          this.$refs.processImg.init(processId)
        })
      },
    }
  };
</script>
