<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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceInformation" @selection-change="handleSelectionChange">
      <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="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="200"  align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.intoStatus == '2' || scope.row.intoStatus == '3'"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-else-if="scope.row.intoStatus == '1'"
          >撤回
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row)"
            v-else-if="scope.row.intoStatus == '4' || scope.row.intoStatus == '5'"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleAudit(scope.row)"
          >审核记录
          </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 { listEntranceOfPersonnel, submitMulApproval, updatePersonOutReason
} from '@/api/pro/admissionRequest'
import { dictTableOption, selectFile } from '@/api/tool/select'

import { retractApproval } from '@/api/terminalEquipment/deviceInformation'
import bnsConsPersonDetailsTabs from '@/components/pro-tabs/bns-cons-person-details-tabs.vue'
import { getConstructionPersonnelDetails } from '@/api/pro/outsourcingPro'
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 {
  name: "Post",
  components: { bnsKkFilePreview, BnsFileListTabs, bnsConsPersonDetailsTabs },
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      deviceInformation: [],
      // 日期范围
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      personItems: {},
      personOpen: false,
      // 对齐方式
      labelPosition: 'top',
      // 角色选项
      deviceOptions: [],
      statusDict: [],
      // 控制显示或隐藏的变量
      showDeviceName: false,
      document:false,
      documentPreview: false,
      fileList: [],
      filePreview: {
        filePreviewUrl: '',
        fileName: '',
        showDownloadButton: false
      },
      // 默认为新增模式，可编辑
      isEditing: false,
      // 地图是否显示弹出层
      innerVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceName: undefined,
        createUser: undefined,
        deviceUser: undefined,
        deviceType: undefined
      },
      // 表单参数
      form: {
        reason: ''
      },
      // 表单验证规则
      rules: {
        reason: [
          { required: true, message: '请输入出场原因', trigger: 'blur' }
        ]
      },
      uuid:'',
      taskId:'',
      auditStatusDict:{
        "1" : "待审批",
        "2" : "审批中",
        "3" : "已通过",
        "4" : "已驳回",
        "5" : "已撤销"
      },
      updateStatus(status){
        return this.auditStatusDict[status]
      },
      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
          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;
      listEntranceOfPersonnel(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.postId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push("/system/admission-request-auth/addPersonnel/" + encryptCBC(JSON.stringify({
        id: '0'
      })));
    },
    /** 修改按钮操作 */
    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;
      });
    },


    /** 撤回按钮操作 */
    handleDelete(row) {
      const params = {
        uuid: row.uuid,
        taskId: row.taskId,
        procInstId: row.procInsId,
        taskType: this.$store.state.user.userType === '02' ? '6' : '11',
      }
      let text = '是否撤回当前出场申请的数据项？'
      this.$modal.confirm(text).then(function() {
        return retractApproval(params)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('撤回成功')
      }).catch(() => {
      })
    },
    handleEdit(row){
      this.open = true
      this.uuid = row.uuid
      this.taskId = row.taskId
      this.form.reason = row.reason
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 提交按钮 */
    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))
      })
    },
    handleAudit(row) {
      this.$router.push('/flow/auditRecord/' + encryptCBC(JSON.stringify({
        taskId: row.taskId
      })))
    },
  }
};
</script>
