<template>
  <div class="app-container">
    <el-form :model="personQueryParams" ref="personQueryForm" size="small" :inline="true" label-width="auto">
      <el-form-item label="施工人员姓名" label-width="120px" prop="name">
        <el-input
          v-model="personQueryParams.name"
          placeholder="请输入施工人员姓名"
          v-no-whitespace
          maxlength="20"
          show-word-limit
          clearable
          @keyup.enter.native="handleQueryPerson"
        />
      </el-form-item>
      <el-form-item label="工程：" prop="proName">
        <el-select v-model="personQueryParams.proName" placeholder="请选择工程名称" clearable>
          <el-option v-for="item in proOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="承包商：" prop="consName">
        <el-select v-model="personQueryParams.consName" placeholder="请选择承包商" clearable>
          <el-option v-for="item in consOptions" :key="item.valueUuid" :label="item.label" :value="item.valueUuid" />
        </el-select>
      </el-form-item>
      <el-form-item label="审批状态" prop="status">
        <el-select v-model="personQueryParams.status" placeholder="请选择审批状态" clearable>
          <el-option v-for="dict in statusDict" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item style="display: flex; justify-content: flex-end">
        <el-button type="primary" icon="el-icon-edit" size="mini" @click="onHandleBatchAuditing" plain>
          批量审核
        </el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQueryPerson">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQueryPerson">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="personList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="indexContinuous(personQueryParams.pageNum, personQueryParams.pageSize)"
      />
      <el-table-column label="工程名称" width="170" align="center" prop="proName" />
      <el-table-column label="承包商名称" width="170" align="center" prop="consName" />
      <el-table-column label="姓名" width="70" align="center" prop="name" />
      <el-table-column label="岗位" width="100" align="center" prop="postName" />
      <el-table-column label="特殊工种种类" width="100" align="center" prop="specialJobType" />
      <el-table-column label="体检文件" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleShow(scope.row, 1)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="保险文件" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleShow(scope.row, 2)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="特殊工种文件" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleShow(scope.row, 3)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="其他文件" width="100" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleShow(scope.row, 4)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column label="性别" width="50" align="center" prop="sex" />
      <el-table-column label="年龄" width="50" align="center" prop="age" />
      <el-table-column label="联系方式" width="120" align="center">
        <template slot-scope="scope">
          <!-- 检查是否存在身份证号 -->
          <span v-if="scope.row.phone">{{ hideSensitiveInfo(scope.row.phone) }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="籍贯" width="70" align="center" prop="nativePlace" />
      <el-table-column label="身份证号码" width="160" align="center">
        <template slot-scope="scope">
          <!-- 检查是否存在身份证号 -->
          <span v-if="scope.row.idCard">{{ hideSensitiveInfo(scope.row.idCard) }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="民族" width="70" align="center" prop="nation" />
      <el-table-column label="家庭地址" width="200" align="center" prop="homeAddress" />
      <el-table-column label="现地址" width="200" align="center" prop="currentAddress" />
      <el-table-column label="人脸照片" align="center" prop="facePath">
        <template slot-scope="scope">
          <el-popover placement="right" title="" trigger="hover">
            <img
              :src="`${scope.row.facePath ? lookFaceFile + scope.row.facePath : ''}`"
              :min-width="300"
              :height="300"
              alt="照片"
            />
            <img
              slot="reference"
              :src="`${scope.row.facePath ? lookFaceFile + scope.row.facePath : ''}`"
              style="max-height: 50px; max-width: 50px"
              :alt="`${scope.row.facePath ? lookFaceFile + scope.row.facePath : ''}`"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" align="center" prop="intoStatus">
        <template slot-scope="scope">
          <!-- 检查是否存在身份证号 -->
          <span v-if="scope.row.intoStatus">{{ updateStatus(scope.row.intoStatus) }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === '1'"
            size="mini"
            type="text"
            v-hasPermi="['system:subPersonApply:list']"
            @click="handleApprove(scope.row)"
          >
            审批
          </el-button>
          <el-button size="mini" type="text" @click="handleView(scope.row)" v-else>查看</el-button>
          <el-button size="mini" type="text" @click="handleAuditRecord(scope.row)">审核记录</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="personTotal > 0"
      :total="personTotal"
      :page.sync="personQueryParams.pageNum"
      :limit.sync="personQueryParams.pageSize"
      @pagination="getList"
    />
    <bns-person-approval-tabs
      :items="personQueryParams"
      v-on:pass="approval"
      v-on:turnDown="turnDown"
      v-on:finalInstance="finalInstance"
    />
    <el-dialog title="文件查看" :visible.sync="document" width="80%" top="5vh" append-to-body>
      <div style="width: 100%; height: 720px">
        <bns-file-list-tabs :items="fileList" />
      </div>
    </el-dialog>
    <el-dialog title="文件查看" :visible.sync="documentPreview" width="80%" top="5vh" append-to-body>
      <div style="width: 100%; height: 720px">
        <bns-kkFile-preview :items="kkFilePreview"></bns-kkFile-preview>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { hideSensitiveInfo, indexContinuous, lookFaceFile } from '@/utils/bonus'
import bnsConsPersonDetailsTabs from '@/components/pro-tabs/bns-cons-person-details-tabs.vue'
import bnsGenerateWorkCardTabs from '@/components/pro-tabs/bns-generate-work-card-tabs.vue'
import {
  getProOptions,
  getConsOptions,
  getSubOptions,
  getList,
  batchAuditingApi,
} from '@/api/pro/subManagement/approval/personApproval'
import BnsPersonApprovalTabs from '@/components/pro-tabs/bns-person-approval-tabs.vue'
import { dictTableOption, selectFile } from '@/api/tool/select'

import { encryptCBC } from '@/utils/aescbc'
import bnsFileListTabs from '@/components/pro-tabs/bns-file-list-tabs.vue'
import bnsKkFilePreview from '@/components/pro-tabs/bns-kkFile-preview.vue'
export default {
  components: {
    bnsKkFilePreview,
    bnsFileListTabs,
    BnsPersonApprovalTabs,
    bnsConsPersonDetailsTabs,
    bnsGenerateWorkCardTabs,
  },
  data() {
    return {
      personQueryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        status: '1',
        proName: undefined,
        consName: undefined,
      },
      personList: [],
      proOptions: [], //工程下拉选项
      consOptions: [], //承包商下拉选项
      //审核类型下拉选
      statusDict: [],
      personTotal: 0,
      loading: true,
      constructionPersonnelOpen: false,
      document: false,
      documentPreview: false,
      fileList: [],
      kkFilePreview: {
        filePreviewUrl: '',
        fileName: '',
        showDownloadButton: false,
      },
      auditStatusDict: {
        1: '待审批',
        2: '审批中',
        3: '已通过',
        4: '已驳回',
        5: '已撤销',
      },
      lookFaceFile: '',

      // 批量审核选择的数据源
      selectList: [],
    }
  },
  created() {
    this.lookFaceFile = lookFaceFile()
    this.getProOption()
    this.getConsOption()
    this.getStatus()
    this.getList()
  },
  methods: {
    indexContinuous,
    updateStatus(status) {
      return this.auditStatusDict[status]
    },
    hideSensitiveInfo,
    /** 文件查看按钮操作 */
    handleShow(row, informationType) {
      this.loading = true
      const arr = {
        1: '体检文件',
        2: '保险文件',
        3: '特殊工种文件',
        4: '其他文件',
      }
      selectFile({
        classification: 9,
        uuid: row.uuid,
        informationType: informationType,
        fromType: 2,
      }).then(response => {
        console.log(response)
        if (response.data.length === 0) {
          this.fileList = []
          this.$message.warning(arr[informationType] + '暂无')
        } else if (response.data.length === 1) {
          this.kkFilePreview = {
            filePreviewUrl: response.data[0].filePath,
            fileName: response.data[0].fileName,
            showDownloadButton: false,
          }
          this.documentPreview = true
        } else {
          this.fileList = response.data
          this.document = true
        }
        this.loading = false
      })
    },
    finalInstance(e) {},
    turnDown(e) {},
    approval(e) {
      console.log('============================')
      // console.log("tongg")
      // console.log(JSON.stringify(e))
      // console.log(JSON.stringify(this.paramsData))
      // const param = {
      //   reason:e,
      //   ...this.paramsData
      // }
      // submitPersonApproval(param).then(response => {
      //   if (response.code === 200) {
      //     this.$message({
      //       message: '新建成功',
      //       type: 'success'
      //     })
      //   } else {
      //     this.$message({
      //       message: response.msg,
      //       type: 'error'
      //     })
      //   }
      // })
    },
    //获取工程下拉选项
    getProOption() {
      const params = {
        id: '',
      }
      getProOptions(params).then(res => {
        this.proOptions = res.data
      })
    },
    //获取承包商下拉选项
    getConsOption() {
      const params = {
        id: '',
      }
      getConsOptions(params).then(res => {
        this.consOptions = res.data
      })
    },
    //获取审核类型下拉选
    getStatus() {
      const params = {
        dictType: 'sys_approval_state',
        dictValue: '',
      }
      dictTableOption(params).then(response => {
        this.statusDict = response.data
      })
    },
    /** 人员入场查询 */
    getList() {
      const proId = this.$store.state.user.thisIds.proId
      const consUuid = this.$store.state.user.thisIds.consUuid
      const supUuid = this.$store.state.user.thisIds.supUuid
      let userType = this.$store.state.user.userType
      if (userType === '' || userType === 'null' || userType === undefined) {
        userType = '00'
      }
      const params = {
        proId: proId,
        consUuid: consUuid,
        supUuid: supUuid,
        userType: userType,
        type: '入场',
        ...this.personQueryParams,
      }
      console.log('人员入场查询==' + JSON.stringify(params))
      getList(params).then(res => {
        console.log('人员入场查询==' + res)
        this.personList = res.rows
        this.personTotal = res.total
        this.loading = false
      })
    },
    //查询按钮
    handleQueryPerson() {
      this.loading = true
      this.personQueryParams.pageNum = 1
      this.getList()
    },
    //重置按钮
    resetQueryPerson() {
      this.loading = true
      this.resetForm('personQueryForm')
      this.handleQueryPerson()
    },
    //审核
    handleApprove(row) {
      console.log('审核++' + JSON.stringify(row))
      this.$router.push(
        '/system/subManagement-personApproval-auth/personApproval/' +
          encryptCBC(
            JSON.stringify({
              uuid: row.uuid,
              taskId: row.taskId,
              procInstId: row.procInsId,
              contUuid: row.contUuid,
              showType: 2,
              isExamine: true,
              btnShow: true,
              finalCheck: row.finalCheck,
            })
          )
      )
    },
    //查看
    handleView(row) {
      this.$router.push(
        '/system/subManagement-personApproval-auth/personApproval/' +
          encryptCBC(
            JSON.stringify({
              uuid: row.uuid,
              taskId: row.taskId,
              procInstId: row.procInsId,
              contUuid: row.contUuid,
              showType: 1,
              isExamine: false,
              btnShow: false,
              finalCheck: row.finalCheck,
            })
          )
      )
    },
    //审核记录
    handleAuditRecord(row) {
      this.$router.push(
        '/flow/auditRecord/' +
          encryptCBC(
            JSON.stringify({
              taskId: row.taskId,
              checkType: '2',
            })
          )
      )
    },

    // 列表复选框选择事件
    handleSelectionChange(list) {
      this.selectList = list
    },

    // 批量审核按钮
    async onHandleBatchAuditing() {
      if (this.selectList.length < 1) {
        this.$modal.msgError('请勾选需要审核的数据！')
        return
      }
      console.log(this.selectList, 'this.selectList')
      // const res = batchAuditingApi()
    },
  },
}
</script>
<style scoped lang="scss"></style>
