<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="关键字" prop="keyWord">
        <el-input
          v-model="queryParams.keyWord"
          placeholder="请输入关键字"
          maxlength="60"
          show-word-limit
          clearable
          v-no-whitespace
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工程" prop="proName" v-if="hideOrNot">
        <el-select v-model="queryParams.proName" placeholder="请选择工程" clearable>
          <el-option v-for="dict in proNameDict" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="审批状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审批状态" clearable>
          <el-option v-for="dict in approvalStatus" :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>
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :style="{ float: 'right' }"></right-toolbar>

    <el-table
      v-loading="loading"
      :data="proList"
      @selection-change="handleSelectionChange"
      :index="indexContinuous(queryParams.pageNum, queryParams.pageSize)"
    >
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="工程名称" align="center" prop="proName" />
      <el-table-column label="承包商名称" align="center" prop="consName" />
      <el-table-column label="社会统一信用代码" align="center" prop="socialUnifiedCreditCode" />
      <el-table-column label="法人姓名" align="center" prop="corporateName" />
      <el-table-column label="法人联系方式" align="center" prop="corporatePhone">
        <template slot-scope="scope">
          <!-- 检查是否存在身份证号 -->
          <span v-if="scope.row.corporatePhone">{{ hideSensitiveInfo(scope.row.corporatePhone) }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="承包商负责人" align="center" prop="contractorPrincipal" />
      <el-table-column label="承包商负责人联系方式" align="center">
        <template slot-scope="scope">
          <!-- 检查是否存在身份证号 -->
          <span v-if="scope.row.contractorPrincipalPhone">
            {{ hideSensitiveInfo(scope.row.contractorPrincipalPhone) }}
          </span>
          <span v-else>-</span>
        </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="操作" width="300px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="examine(scope.row, 1)"
            v-if="scope.row.status === '1'"
            v-hasPermi="['system:consApproval:approval']"
          >
            审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="examine(scope.row, 2)"
            v-hasPermi="['system:consApproval:approval']"
            v-else
          >
            查看
          </el-button>
          <el-button size="mini" type="text" @click="auditRecord(scope.row)" v-hasPermi="['system:audit:query']">
            审核记录
          </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"
    />
  </div>
</template>

<script>
import { dictTableOption } from '@/api/tool/select'
import { getProOptions } from '@/api/pro/subManagement/approval/personApproval'
import { getConsEntranceList } from '@/api/pro/approval_cons'
import { hideSensitiveInfo, indexContinuous } from '@/utils/bonus'
import { encryptCBC } from '@/utils/aescbc'

export default {
  name: 'Post',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 承包商表格数据
      proList: [],
      //工程选择下拉选
      proNameDict: [],
      //审批状态下拉选
      approvalStatus: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      hideOrNot: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined,
        proName: undefined,
        start: undefined,
        status: '1',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      statusDict: {
        1: '待审批',
        2: '审批中',
        3: '已通过',
        4: '已驳回',
        5: '已撤销',
      },
    }
  },
  created() {
    const userType = this.$store.state.user.userType
    if (userType !== '00') {
      this.hideOrNot = false
      this.getProOption()
    }
    this.getList()
    this.getApprovalStatus()
  },
  methods: {
    updateStatus(status) {
      return this.statusDict[status]
    },
    indexContinuous,
    hideSensitiveInfo,
    /** 获取审批状态下拉选 */
    getApprovalStatus() {
      const params = {
        dictType: 'sys_approval_state',
        dictValue: '',
      }
      dictTableOption(params).then(response => {
        this.approvalStatus = response.data
      })
    },
    /** 获取工程下拉选 */
    getProOption() {
      const params = {
        id: '',
      }
      getProOptions(params).then(res => {
        this.proNameDict = res.data
      })
    },

    /** 查询承包商入场资质列表 */
    getList() {
      this.loading = false
       this.queryParams.proId = this.$store.state.user.thisIds.proId;
      this.queryParams.supUuid = this.$store.state.user.thisIds.supUuid
      this.queryParams.userType = this.$store.state.user.userType
      getConsEntranceList(this.queryParams).then(response => {
        this.proList = response.rows
        this.total = response.total
        this.loading = false
        console.log('this.proList', this.proList)
        console.log('this.proList', JSON.stringify(this.proList))
      })
    },
    /** 打开审核页面 */
    examine(row, type) {
      if (type === 1) {
        this.$router.push(
          '/system/cons-approval-auth/consEntranceApproval/' +
            encryptCBC(
              JSON.stringify({
                proId: row.proId,
                taskId: row.taskId,
                procInstId: row.procInsId,
                finalCheck: row.finalCheck,
                consUuid: row.consUuid,
                isExamine: true,
                btnShow: true,
                showType: 2,
              })
            )
        )
      } else {
        this.$router.push(
          '/system/cons-approval-auth/consEntranceApproval/' +
            encryptCBC(
              JSON.stringify({
                proId: row.proId,
                taskId: row.taskId,
                procInstId: row.procInsId,
                finalCheck: row.finalCheck,
                consUuid: row.consUuid,
                isExamine: false,
                btnShow: false,
                showType: 1,
              })
            )
        )
      }
    },
    /** 打开审核记录页面 */
    auditRecord(row) {
      this.$router.push(
        '/flow/auditRecord/' +
          encryptCBC(
            JSON.stringify({
              taskId: row.taskId,
              checkType: '2',
              taskType: '存在',
            })
          )
      )
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.proId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
  },
}
</script>
