<template xmlns="http://www.w3.org/1999/html">
  <div class="checkTable">
    <el-dialog :visible.sync="dialog.dialogFormVisible" width="1800px" :close-on-click-modal="false"
               title='维修点清单'>
      <el-row>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
          <el-form-item label="装置" prop="plantId">
            <el-select v-model="queryParams.plantId" @change="handleQuery"
                       placeholder="请选择装置" clearable size="small" disabled>
              <el-option
                v-for="plant in plantOperation"
                :key="plant.plantId"
                :label="plant.plantName"
                :value="plant.plantId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="群组编码" prop="groupCode">
            <el-input
              v-model="queryParams.groupCode"
              placeholder="请输入群组编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="密封点类型" prop="pointType" label-width="90px">
            <el-select v-model="queryParams.pointType" @change="handleQuery" placeholder="请选择密封点类型" clearable
                       size="small">
              <el-option
                v-for="dict in pointOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['check:checkpoints:export']"
          >导出维修点清单
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-upload"
            size="mini"
            @click="handleImport"
            v-if="isEnter"
            v-hasPermi="['check:checkpoints:add']"
          >导入维修数据
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-position"
            size="mini"
            :disabled="leftMultiple"
            @click="handleToApprove"
            v-if="isEnter"
          >送审
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="info"
            plain
            icon="el-icon-s-check"
            size="mini"
            :disabled="leftMultiple"
            @click="handleApprove"
            v-if="isEnter"
            v-hasPermi="['task:repair:approve']"
          >审核
          </el-button>
        </el-col>
      </el-row>
      <el-row>
        <el-table :data="leftData" style="width: 100%;" height="500px" border
                  @selection-change="saveLeft"
                  ref="leftData" :cell-style="tableCellStyle">
          <el-table-column type="selection" align="center" fixed="left"></el-table-column>
          <el-table-column label="审核状态" fixed="left" align="center" prop="approveStatus" width="80"
                           :formatter="approveStatusFormat"/>
          <el-table-column label="净检测值" align="center" prop="netTestValue" width="80"/>
          <el-table-column label="泄漏程度" align="center" prop="leakageDegree" width="80" :formatter="leakageFormat"/>
          <el-table-column label="泄露部位" align="center" prop="leakagePosition" width="130"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="维修方法" align="center" prop="repairFunc" width="130" :show-overflow-tooltip="true"/>
          <el-table-column label="维修状态" align="center" prop="repairType" width="130" :show-overflow-tooltip="true"/>
          <el-table-column label="维修次数" align="center" prop="repairCount" width="130"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="装置名称" align="center" prop="plantName" width="130" :show-overflow-tooltip="true"/>
          <el-table-column label="区域名称" align="center" prop="regionName" width="130" :show-overflow-tooltip="true"/>
          <el-table-column label="平台(层)" align="center" prop="layer" width="130" :show-overflow-tooltip="true"/>
          <el-table-column label="设备/管线名称" align="center" prop="devName" width="130"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="设备/管线编号" align="center" prop="devCode" width="130"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="群组编码" align="center" prop="groupCode" width="130" :show-overflow-tooltip="true"/>
          <el-table-column label="密封点扩展号编码" align="center" prop="extendCode" width="150"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="密封点类型" align="center" prop="pointType" width="130"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="仪器编号" align="center" prop="instrumentCode" width="130"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="维修人员" align="center" prop="repairer" width="130" :show-overflow-tooltip="true"/>
          <el-table-column label="维修日期" align="center" prop="repairDate" width="180" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.repairDate, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="leftTotal>0"
          :total="leftTotal"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-row>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="cancel">返回</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog v-dialogDrag :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          {{ $t('将文件拖到此处，或') }}
          <em>{{ $t('点击上传') }}</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <!--<el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据-->
          <el-link type="info" style="font-size:12px" @click="importTemplate">{{ $t('下载模板') }}</el-link>
        </div>
        <form ref="downloadFileForm" :action="upload.downloadAction" target="FORMSUBMIT">
          <input name="type" :value="upload.type" hidden/>
        </form>
        <div class="el-upload__tip" style="color:red" slot="tip">{{ $t('提示：仅允许导入“xls”或“xlsx”格式文件！') }}</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm" v-loading.fullscreen.lock="fullscreenLoading">{{
            $t('确 定')
          }}
        </el-button>
        <el-button @click="upload.open = false">{{ $t('取 消') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getAllPlantName} from "@/api/base/plant";
import {getToken} from "@/utils/auth";
import {approveRepairpoints, listRepairpoints} from "@/api/check/repairpoints";

export default {
  name: "repairEnter",
  data() {
    return {
      leftTotal: 0,
      checkAgain: false,
      fullscreenLoading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pointId: null,
        repairId: null,
        netTestValue: null,
        repairFunc: null,
        repairType: null,
        repairCount: null,
        plantName: null,
        regionName: null,
        layer: null,
        devName: null,
        devCode: null,
        groupCode: null,
        extendCode: null,
        pointType: null,
        instrumentCode: null,
        leakagePosition: null,
        repairer: null,
        repairDate: null,
        leakageDegree: null,
        remarks: null,
        approveStatus: null,
        approveTime: null,
        deptId: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null,
        plantId: null,
        regionId: null,
        devId: null,
        instrumentId: null,
        plantType: null,
        mediumType: null,
        choose: null,
      },
      row: {},
      leftData: [],
      left: [],  //左边选中的数据
      leftMultiple: true,
      approveStatusOperation: [],
      dialog: {
        dialogFormVisible: false,
      },
      repairId: 0,
      pointOptions: [],
      plantOperation: [],
      isEnter: false,
      // 用户导入参数
      upload: {
        downloadAction: process.env.VUE_APP_BASE_API + '/common/template',
        //下载模板类型
        type: "repairPoint",
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/check/repairpoints/importData/",
      },
    }
  },
  methods: {
    tableCellStyle({row, column, rowIndex, columnIndex}) {
      if (columnIndex === 1 && row.approveStatus == 2) {
        return "color:#13C2C2;";
      }
      if (columnIndex === 1 && row.approveStatus == 1) {
        return "color:#0000FF;";
      }
      if (columnIndex === 1 && row.approveStatus == 0) {
        return "color:#ff0000;";
      }
      if (columnIndex === 3 && row.leakageDegree == 1) {
        return "color:#00ff00;font-size:200%";
      }
      if (columnIndex === 3 && row.leakageDegree == 2) {
        return "color:#FFDF00;font-size:200%";
      }
      if (columnIndex === 3 && row.leakageDegree == 3) {
        return "color:#ff0000;font-size:200%";
      }
    },
    leakageFormat(row, column) {
      return row.leakageDegree ? "●" : null;
    },
    approveStatusFormat(row, column) {
      return this.selectDictLabel(this.approveStatusOperation, row.approveStatus);
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
      this.fullscreenLoading = true;
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = this.$t('用户导入');
      this.upload.open = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('check/repairpoints/export', {
        ...this.queryParams
      }, `checkpoints_${new Date().getTime()}.xlsx`)
    },
    /** 下载模板操作 */
    importTemplate() {
      this.$refs['downloadFileForm'].submit()
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.fullscreenLoading = false;
      if (response.data === 0) {
        this.$alert(this.$t('导入失败！') + response.msg, this.$t('导入结果'), {dangerouslyUseHTMLString: true});
      } else if (response.data[0] != null) {
        this.$alert(this.$t('成功导入') + response.msg + this.$t('条数据') + "，" + this.$t('第') + response.data + this.$t('行数据出现错误导入失败') + "。", this.$t('导入结果'), {dangerouslyUseHTMLString: true});
      } else {
        this.$alert(this.$t('成功导入') + response.msg + this.$t('条数据'), this.$t('导入结果'), {dangerouslyUseHTMLString: true});
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    pointFormat(row, column) {
      return this.selectDictLabel(this.pointOptions, row.pointType);
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    cancel() {
      this.leftData = [];
      this.dialog.dialogFormVisible = false;
    },
    openDialog(row) {
      this.leftData = [];
      this.row = row;
      this.queryParams.plantId = row.plantId;
      this.queryParams.repairId = row.id;
      this.upload.url = process.env.VUE_APP_BASE_API + "/check/repairpoints/importData/" + row.id;;
      if (row.isEnter && row.status == 1) {
        this.isEnter = true;
      }
      this.dialog.dialogFormVisible = true;
      this.checkAgain = row.taskType == 2 ? true : false;
      this.getList();
      this.getDicts("point_type").then(response => {
        this.pointOptions = response.data;
      });
      getAllPlantName().then(response => {
        this.plantOperation = response.data;
      })

      this.getDicts("base_approve_status").then(response => {
        this.approveStatusOperation = response.data;
      });
    },
    /** 查询检测点列表 */
    getList() {
      listRepairpoints(this.queryParams).then(response => {
        this.leftData = response.rows;
        this.leftTotal = response.total;
      });
    },
    saveLeft(rows) {
      this.left = [];
      if (rows) {
        this.left = rows.map(row => row.checkId);
        this.leftMultiple = !rows.length;
      }
    },
    handleToApprove() {
      let data = {};
      data.checkIds = this.left;
      data.approveStatus = 1;
      this.$modal.confirm("是否确认送审？").then(() => {
          return approveRepairpoints(data).then(response => {
            this.$modal.msgSuccess("送审成功");
            this.getList();
          }).catch(() => {
              this.$modal.msgWarning("送审失败！请确认数据是否填写完整！");
            }
          )
      })
    },
    handleApprove() {
      let data = {};
      data.checkIds = this.left;
      data.approveStatus = 2;
      data.repairCount = 1;
      data.repairId = this.row.id;
      this.$modal.confirm("确认审核？").then(() => {
        return approveRepairpoints(data).then(response => {
          this.$modal.msgSuccess("审核成功");
          this.getList();
        }).catch(() => {
            this.$modal.msgWarning("审核失败！请确认数据是否填写完整！");
          }
        )
      })
    }
  }
}
</script>

<style scoped>
.checkTable .el-scrollbar {
  height: 100%;
}

.checkTable ::-webkit-scrollbar {
  /* 设置竖向滚动条的宽度 */
  width: 10px;
  /* 设置横向滚动条的高度 */
  height: 10px;
}

.checkTable ::-webkit-scrollbar-thumb {
  /*滚动条的背景色*/
  background-color: rgba(144, 147, 153, .3);
  border-radius: 35px;
}
</style>
