<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="施工人员" prop="consName">
        <el-input
          v-model="queryParams.consName"
          placeholder="请输入施工人员姓名"
          v-no-whitespace
          maxlength="20"
          show-word-limit
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审批状态" prop="status">
        <el-select v-model="queryParams.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>
        <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 :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleOut(null,1)"
          v-hasPermi="['system_personOut_btn']"
        >批量出场
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceInformation" @selection-change="handleSelectionChange" style="overflow-x: auto">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="indexContinuous(queryParams.pageNum, queryParams.pageSize)"
      />
      <el-table-column label="姓名" show-overflow-tooltip width="150"  align="center" prop="consName" />
      <el-table-column label="岗位" width="150"  align="center" prop="post" />
      <el-table-column label="特殊工种种类" width="150" align="center" prop="workType" />
      <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="60"  align="center" prop="sex" />
      <el-table-column label="年龄" width="60"  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="籍贯" show-overflow-tooltip width="150" align="center" prop="natives" />
      <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="60"  align="center" prop="nation" />
      <el-table-column label="家庭住址" show-overflow-tooltip width="150"  align="center" prop="homeAddress" />
      <el-table-column label="现住址" show-overflow-tooltip width="150"  align="center" prop="address" />
      <el-table-column label="人脸照片" align="center" prop="faceUrl">
        <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="审批状态" width="150"  align="center" prop="status" />
      <el-table-column label="操作" align="center" fixed="right" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:listPersonnelInformation:list']"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleOut(scope.row,2)"
            v-hasPermi="['system_personOut_btn']"
          >出场
          </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"
    />
    <!--施工人员基本信息查看-->
    <el-dialog title="查看详情" :visible.sync="personOpen" width="60%">
      <bns-cons-person-details-tabs
        :items="personItems"></bns-cons-person-details-tabs>
    </el-dialog>

    <!--人员出场信息查看-->
    <el-dialog :title="'查看详情'" :visible.sync="open" width="20%">
      <el-form :label-position="labelPosition" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="请填写出场原因：" prop="desc">
          <el-input type="textarea" v-model="form.reason" v-no-whitespace maxlength="80" show-word-limit :autosize="{ minRows: 10}"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-preventReClick="5000">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <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="filePreview"></bns-kkFile-preview>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { hideSensitiveInfo, indexContinuous, lookFaceFile } from '@/utils/bonus'
import {
  dictTableOption, selectFile
} from '@/api/tool/select'
import { listPersonnelInformation, submitMulApproval, updatePersonOutReason } from '@/api/pro/admissionRequest'

import bnsConsPersonDetailsTabs from '@/components/pro-tabs/bns-cons-person-details-tabs.vue'
import { getConstructionPersonnelDetails } from '@/api/pro/outsourcingPro'
import BnsFileListTabs from '@/components/pro-tabs/bns-file-list-tabs.vue'
import bnsKkFilePreview from '@/components/pro-tabs/bns-kkFile-preview.vue'

export default {
  name: 'Post',
  components: { bnsKkFilePreview, BnsFileListTabs, bnsConsPersonDetailsTabs },
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // procInsId
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      deviceInformation: [],
      // 日期范围
      dateRange: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      document:false,
      documentPreview: false,
      fileList: [],
      filePreview: {
        filePreviewUrl: '',
        fileName: '',
        showDownloadButton: false
      },
      personItems: {},
      personOpen: false,
      // 角色选项
      deviceOptions: [],
      statusDict: [],
      // 控制显示或隐藏的变量
      showDeviceName: false,
      // 默认为新增模式，可编辑
      isEditing: false,
      // 地图是否显示弹出层
      innerVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        consName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      //出场数组
      uuid: [],
      taskId:[],
      // 对齐方式
      labelPosition: 'top',
      // 表单验证规则
      rules: {
        reason: [
          { required: true, message: '请输入出场原因', trigger: 'blur' }
        ]
      },
      row: {},
      type: 0,
      lookFaceFile:''
    }
  },
  created() {
    this.lookFaceFile = lookFaceFile()
    this.getStatus()
    this.getList()
  },
  methods: {
    lookFaceFile,
    indexContinuous,
    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.filePreview = {
            filePreviewUrl: response.data[0].filePath,
            fileName: response.data[0].fileName,
            showDownloadButton: false
          }
          this.documentPreview = true
        }else{
          this.fileList = response.data
          console.log('🚀 ~ selectFile ~ this.fileList:', this.fileList)
          this.document = true
        }
        this.loading = false
      })
    },
    /** 查询设备列表 */
    getList() {
      this.loading = true
      this.queryParams.proId = this.$store.state.user.thisIds.proId
      this.queryParams.contUuid = this.$store.state.user.thisIds.consUuid
      this.queryParams.subUuid = this.$store.state.user.thisIds.subUuid
      this.queryParams.userType = this.$store.state.user.userType
      listPersonnelInformation(this.queryParams).then(response => {
        this.deviceInformation = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    //获取审核类型下拉选
    getStatus() {
      const params = {
        dictType: 'sys_approval_state',
        dictValue: ''
      }
      dictTableOption(params).then(response => {
        this.statusDict = response.data
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uuid)
      this.taskId = selection.map(item => item.taskId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.personOpen = true;
      const params = {
        uuid: row.uuid
      }
      getConstructionPersonnelDetails(params).then(response => {
        console.log(response)
        this.personItems = response.data;
        this.personItems.phone = hideSensitiveInfo(response.data.phone)
        this.personItems.idCard = hideSensitiveInfo(response.data.idCard)
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },

    // 表单重置
    reset() {
      this.form = {
        desc: undefined
      }
      this.resetForm('form')
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.uuid = this.uuid
          updatePersonOutReason(this.form).then(response => {
            this.submitApply()
          })
        }
      })
    },
    submitApply() {
      const param = {
        proId: this.$store.state.user.thisIds.proId,
        taskType: this.$store.state.user.userType === '02' ? '6' : '11',
        uuid: this.uuid,
        taskId: this.taskId,
        userType: this.$store.state.user.userType,
        parentUuid: this.$store.state.user.thisIds.parentUuid,
      }
      submitMulApproval(param).then(response => {
        if (response.code === 200) {
          // this.$message.success(response.msg)
          if (response.msg === '此审核类型未配置相对应的审核流，请联系管理员配置后再次提交'){
            this.$modal.msgError(response.msg)
          }else{
            this.$modal.msgSuccess('提交出场成功')
          }
          this.open = false
          this.getList()
        } else {
          this.$message.error(response.msg)
        }
      }).catch((error) => {
        console.log('error=' + JSON.stringify(error))
      })
    },
    /** 出场或批量出场按钮操作 */
    handleOut(row,type) {
      this.type = type
      this.row = row
      if (type === 1) {
        if (this.ids.length === 0) {
          this.$message.error('请选择需要出场的人员')
          return
        }

        this.uuid = this.ids.join(',')
        this.taskId = this.taskId.join(',')
      } else {
        if (!row) {
          this.$message.error('请选择需要出场的人员')
          return
        }else{
          this.uuid = row.uuid
          this.taskId = row.taskId ? row.taskId : ''
        }
      }
      this.reset()
      this.open = true
      this.title = '出场原因'
    }

  }
}
</script>
