<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <el-select v-model="searchParam._mineIndex" placeholder="所属矿山" clearable size="small" class="filter-item" style="width: 200px;">
          <el-option v-for="mine in mineList" :key="mine.index" :label="mine.name" :value="mine.index" />
        </el-select>
        <el-input v-model="searchParam.nameLike" clearable size="small" placeholder="危险作业名称" style="width: 200px;" class="filter-item" />
        <!-- <el-select v-model="searchParam.auditState" clearable size="small" placeholder="审核状态" style="width: 200px;" class="filter-item">
          <el-option label="未审核" :value="1" />
          <el-option label="审核通过" :value="2" />
          <el-option label="审核不通过" :value="3" />
        </el-select> -->
        <el-button class="filter-item" size="mini" type="success" icon="el-icon-search" @click="searchSubmit">搜索</el-button>
        <el-button class="filter-item" size="mini" type="warning" icon="el-icon-refresh-left" @click="searchParamReset">重置</el-button>
      </div>
      <el-button class="filter-item" size="mini" type="primary" @click="doAuditAll">一键审核通过</el-button>
    </div>
    <!--弹出框1-->
    <el-dialog append-to-body :close-on-click-modal="true" :visible.sync="dialog1Show" :title="'确认审核'" width="580px">
      <div>
        <span>确认要</span>
        <span v-if="dialog1Data.auditState==2" style="color: #67C23A;">审核通过</span>
        <span v-if="dialog1Data.auditState==3" style="color: #F56C6C;">审核不通过</span>
        <span>危险作业：</span><span style="font-weight: bold;">{{ dialog1Data.name }}</span><span>吗？</span>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="dialog1Show=false">取消</el-button>
        <el-button :loading="dialog1Saving" type="primary" @click="auditSubmit">确认</el-button>
      </div>
    </el-dialog>
    <!--弹出框2-->
    <el-dialog append-to-body :close-on-click-modal="true" :visible.sync="dialog2Show" :title="'危险作业备案变更清单'" width="960px">
      <el-form label-width="140px">
        <el-form-item label="排序方式：">
          <el-radio-group v-model="dialog2Reverse" size="mini">
            <el-radio-button :label="true">最新优先</el-radio-button>
            <el-radio-button :label="false">时间顺序</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <el-timeline :reverse="dialog2Reverse">
        <el-timeline-item v-for="(dialog2DataItem,i) in dialog2Data" :key="dialog2DataItem.id" :color="dialog2Data.length==i+1?'#67C23A':null" :timestamp="(i==0?'初次提交：':dialog2Data.length==i+1?'最新变更：':'变更：')+dialog2DataItem.createDatetime" placement="top">
          <el-card>
            <el-form :model="dialog2DataItem" size="mini" label-width="120px" style="margin-right: 80px;">
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form-item label="危险作业名称：" prop="name">
                    <span>{{ dialog2DataItem.name }}</span>
                  </el-form-item>
                </el-col>
                <!-- <el-col :span="12">
                  <el-form-item label="审核状态：" prop="auditState">
                    <span v-if="dialog2DataItem.auditState==1">未审核</span>
                    <span v-if="dialog2DataItem.auditState==2">审核通过</span>
                    <span v-if="dialog2DataItem.auditState==3">审核不通过</span>
                  </el-form-item>
                </el-col> -->
                <el-col :span="12">
                  <el-form-item label="作业时间：" prop="workDate">
                    <span>{{ dialog2DataItem.workDate }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="状态：" prop="workState">
                    <span v-if="dialog2DataItem.workState==1">正常</span>
                    <span v-if="dialog2DataItem.workState==2">异常</span>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="备注：" prop="note">
                    <span>{{ dialog2DataItem.note }}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="文件：" prop="filesPath">
                    <div v-for="file in dialog2DataItem._fileList" :key="file.url">
                      <el-link type="primary" @click="openFile(file)">{{ file.name }}</el-link>
                    </div>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="提交时间：" prop="createDatetime">
                    <span>{{ dialog2DataItem.createDatetime }}</span>
                  </el-form-item>
                </el-col>
                <!-- <el-col :span="12">
                  <el-form-item label="审核时间：" prop="auditDatetime">
                    <span>{{ dialog2DataItem.auditDatetime }}</span>
                  </el-form-item>
                </el-col> -->
                <el-col v-if="dialog2DataItem.complaint" :span="24">
                  <el-form-item label="申诉说明：" prop="complaint">
                    <span>{{ dialog2DataItem.complaint }}</span>
                  </el-form-item>
                </el-col>
                <el-col v-if="dialog2DataItem.complaint" :span="24">
                  <el-form-item label="申诉文件：" prop="complaintFilesPath">
                    <div v-for="file in dialog2DataItem._complaintFileList" :key="file.url">
                      <el-link type="primary" @click="openFile(file)">{{ file.name }}</el-link>
                    </div>
                  </el-form-item>
                </el-col>
                <el-col v-if="dialog2DataItem.complaint" :span="24">
                  <el-form-item label="申诉时间：" prop="complaintDatetime">
                    <span>{{ dialog2DataItem.complaintDatetime }}</span>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </el-card>
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
    <!--弹出框3-->
    <el-dialog append-to-body :close-on-click-modal="true" :visible.sync="dialog3Show" :title="'确认审核'" width="580px">
      <div>
        <span>确认要将</span>
        <span style="font-weight: bold;">全部矿山 未审核</span>
        <span>的危险作业</span>
        <span style="font-weight: bold;color: #67C23A;">一键审核通过</span>
        <span>吗？</span>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="dialog3Show=false">取消</el-button>
        <el-button :loading="dialog1Saving" type="primary" @click="submitAuditAll">确认</el-button>
      </div>
    </el-dialog>
    <!--弹出框4-->
    <el-dialog append-to-body :close-on-click-modal="true" :visible.sync="dialog4Show" :title="'申诉详情'" width="720px">
      <el-form :model="dialog4Data" size="mini" label-width="180px" style="margin-right: 80px;">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="危险作业名称：" prop="name">
              <span>{{ dialog4Data.name }}</span>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="审核状态：" prop="workDate">
              <span v-if="dialog4Data.auditState==1">未审核</span>
              <span v-if="dialog4Data.auditState==2">审核通过</span>
              <span v-if="dialog4Data.auditState==3">审核不通过</span>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="作业时间：" prop="workDate">
              <span>{{ dialog4Data.workDate }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态：" prop="workDate">
              <span v-if="dialog4Data.workState==1">正常</span>
              <span v-if="dialog4Data.workState==2">异常</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注：" prop="note">
              <span>{{ dialog4Data.note }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文件：" prop="filesPath">
              <div v-for="file in dialog4Data._fileList" :key="file.url">
                <el-link type="primary" @click="openFile(file)">{{ file.name }}</el-link>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-divider content-position="left">申诉内容：</el-divider>
          </el-col>
          <el-col v-if="dialog4Data.complaint" :span="24">
            <el-form-item label="申诉说明：" prop="complaint">
              <span>{{ dialog4Data.complaint }}</span>
            </el-form-item>
          </el-col>
          <el-col v-if="dialog4Data.complaint" :span="24">
            <el-form-item label="申诉文件：" prop="complaintFilesPath">
              <div v-for="file in dialog4Data._complaintFileList" :key="file.url">
                <el-link type="primary" @click="openFile(file)">{{ file.name }}</el-link>
              </div>
            </el-form-item>
          </el-col>
          <el-col v-if="dialog4Data.complaint" :span="24">
            <el-form-item label="申诉时间：" prop="complaintDatetime">
              <span>{{ dialog4Data.complaintDatetime }}</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!--表格-->
    <el-table ref="table" v-loading="searching" :data="tableData" row-key="id">
      <el-table-column :show-overflow-tooltip="true" prop="id" label="编号" width="100px" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="mineName" label="所属矿山" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="name" label="危险作业名称" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="workDate" label="作业时间" align="center" />
      <el-table-column :show-overflow-tooltip="true" prop="workState" label="作业状态" align="center">
        <template slot-scope="scope">
          <el-switch v-if="scope.row.auditState==2" v-model="scope.row._workStateSwitch" active-text="正常" inactive-text="异常" :active-value="1" :inactive-value="2" active-color="#13ce66" inactive-color="#ff4949" @change="changeWorkState(scope.row)" />
        </template>
      </el-table-column>
      <!-- <el-table-column :show-overflow-tooltip="true" prop="auditState" label="审批状态" align="center">
        <template slot-scope="scope">
          <span v-show="scope.row.auditState==1">审核中</span>
          <span v-show="scope.row.auditState==2">已通过</span>
          <span v-show="scope.row.auditState==3">未通过</span>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" width="300px" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" round @click="doLook(scope.row.workUuid)">查看</el-button>
          <el-button v-if="scope.row.complaint" size="mini" round @click="doLookComplaint(scope.row)">企业申诉</el-button>
          <!-- <el-button v-if="scope.row.auditState==1" size="mini" round @click="doAudit(scope.row.id,2)">审核通过</el-button>
          <el-button v-if="scope.row.auditState==1" size="mini" round @click="doAudit(scope.row.id,3)">审核不通过</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-sizes="[10, 20, 50, 100]"
      :current-page.sync="searchParam.page"
      :page-size.sync="searchParam.count"
      :total="searchResult.total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="searchSubmit"
      @current-change="searchSubmit"
    />
  </div>
</template>

<script>
import dangerousWorkApi from '@/api/kd/dangerousWork'
import { getToken } from '@/utils/auth'
import { mapGetters } from 'vuex'
import mineApi from '@/api/kd/mine'
const searchParam = {// 默认搜索条件
  page: 1,
  count: 10,
  searchCount: true
}
export default {
  name: 'CDangerousWork', // 本页面名
  components: {},
  data() {
    return {
      mineList: [],
      httpHeaders: {
        'Authorization': getToken()
      },
      searching: false,
      searchParam: {},
      searchResult: {
        total: 0,
        results: [] // 搜索结果
      },
      // =====审核窗口数据=====
      dialog1Show: false,
      dialog1Data: {},
      dialog1Saving: false,
      // =====查看窗口数据=====
      dialog2Show: false,
      dialog2Data: {},
      dialog2Reverse: true,
      // =====一键审核全部=====
      dialog3Show: false,
      // =====查看企业申诉=====
      dialog4Show: false,
      dialog4Data: {}
    }
  },
  computed: {
    ...mapGetters([
      'user',
      'baseApi'
    ]),
    tableData() {
      // 此处对返回值进行算法重组
      return this.searchResult.results
    }
  },
  mounted() {
    // 矿山选项卡
    mineApi.getEnableMine().then(response => {
      let i = 1
      let mineList = response.results
      mineList = mineList.filter(x => x.mineType === 1) // 只保留地下矿山
      mineList.forEach(x => { x.index = i++ })
      this.mineList = mineList
    })
    this.searchParamReset()
    this.searchSubmit()
  },
  methods: {
    searchParamReset() {
      this.searchParam = JSON.parse(JSON.stringify(searchParam))
    },
    searchSubmit() {
      this.searching = true
      if (this.searchParam._mineIndex) {
        const mine = this.mineList[this.searchParam._mineIndex - 1]
        this.searchParam.mineType = mine.mineType
        this.searchParam.mineId = mine.id
      } else {
        this.searchParam.mineType = null
        this.searchParam.mineId = null
      }
      this.searchParam.hasEnable = true
      dangerousWorkApi.getPage(this.searchParam).then(response => {
        this.searching = false
        response.results.forEach(r => {
          r._workStateSwitch = r.workState
        })
        this.searchResult = response
      }).catch(() => {
        this.searching = false
      })
    },
    changeWorkState(row) {
      const param = {
        id: row.id,
        workState: row._workStateSwitch
      }
      dangerousWorkApi.setWorkState(param).then(() => {
        this.$message.success('修改状态成功')
        row.workState = row._workStateSwitch
      }).catch(() => {
        row._workStateSwitch = row._workStateSwitch === 1 ? 2 : 1
      })
    },
    doLook(workUuid) {
      dangerousWorkApi.getByWorkUuid(workUuid).then(response => {
        this.dialog2Data = response.results
        this.dialog2Data.forEach(dw => {
          if (dw.filesPath) {
            dw._fileList = JSON.parse(dw.filesPath)
          }
          if (dw.complaintFilesPath) {
            dw._complaintFileList = JSON.parse(dw.complaintFilesPath)
          }
        })
        this.dialog2Show = true
      }).catch(() => {
        this.searchSubmit()
      })
    },
    doLookComplaint(dangerousWork) {
      this.dialog4Data = dangerousWork
      this.dialog4Show = true
    },
    auditSubmit() {
      this.dialog1Saving = true
      dangerousWorkApi.audit(this.dialog1Data).then(() => {
        this.$message.success('审核成功')
        this.dialog1Show = false
        this.dialog1Saving = false
        this.dialog1Data = {}
        this.searchSubmit()
      }).catch(() => {
        this.dialog1Saving = false
      })
    },
    doAudit(id, auditState) {
      dangerousWorkApi.getById(id).then(response => {
        this.dialog1Data = response
        this.dialog1Data.auditState = auditState
        this.dialog1Show = true
      }).catch(() => {
        this.searchSubmit()
      })
    },
    doAuditAll() {
      this.dialog3Show = true
    },
    submitAuditAll() {
      dangerousWorkApi.auditAll({ auditState: 2 }).then(() => {
        this.$message.success('一键审核通过成功')
        this.dialog3Show = false
        this.searchSubmit()
      })
    },
    elementuiUpListFormat(fileList) {
      const filePathArray = []
      fileList.forEach(f => {
        if (f.response && f.response.result) {
          filePathArray.push({
            name: f.name,
            url: f.response.result
          })
        } else if (f.name && f.url) {
          filePathArray.push({
            name: f.name,
            url: f.url
          })
        }
      })
      return filePathArray
    },
    openFile(file) {
      if (file.url) {
        window.open(this.baseApi + '/file' + file.url)
      } else if (file.response && file.response.result) {
        window.open(this.baseApi + '/file' + file.response.result)
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
  ::v-deep .vue-treeselect__control,::v-deep .vue-treeselect__placeholder,::v-deep .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
  }
</style>
