<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="请输入关键字"
          v-no-whitespace
          maxlength="60"
          show-word-limit
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="承包商：" prop="contName" v-if="hideOrNot">
        <el-select v-model="queryParams.contName" placeholder="请选择承包商" clearable>
          <el-option v-for="item in consOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="分包商：" prop="subName" v-if="hideOrNot">
        <el-select v-model="queryParams.subName" placeholder="请选择分包商" clearable>
          <el-option v-for="item in subOptions" :key="item.value" :label="item.label" :value="item.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 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-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="subList" @selection-change="handleSelectionChange">
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="indexContinuous(queryParams.pageNum, queryParams.pageSize)"
      />
      <el-table-column label="工程名称" align="center" prop="proName" />
      <el-table-column label="所属承包商" align="center" prop="contName" />
      <el-table-column label="分包商名称" align="center" prop="subName" />
      <el-table-column label="社会统一征信代码" align="center" prop="subCode" />
      <el-table-column label="法人姓名" align="center" prop="legalName" />
      <el-table-column label="法人联系方式" width="120" align="center">
        <template slot-scope="scope">
          <!-- 检查是否存在身份证号 -->
          <span v-if="scope.row.legalPhone">{{ hideSensitiveInfo(scope.row.legalPhone) }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="分包商负责人" align="center" prop="subUserName" />
      <el-table-column label="分包商负责人联系方式" width="120" align="center">
        <template slot-scope="scope">
          <!-- 检查是否存在身份证号 -->
          <span v-if="scope.row.subUserPhone">{{ hideSensitiveInfo(scope.row.subUserPhone) }}</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="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleApprove(scope.row)" v-if="scope.row.status == '1'">
            审批
          </el-button>
          <el-button size="mini" type="text" @click="handleView(scope.row)" v-else>查看</el-button>
          <el-button size="mini" type="text" v-hasPermi="['system:audit:query']" @click="handleAuditRecord(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"
    />
  </div>
</template>

<script>
import { dictTableOption } from '@/api/tool/select'
import { listSubEntry } from '@/api/pro/sub/subList'
import { getConsOptions, getSubOptions } from '@/api/pro/subManagement/approval/personApproval'
import { hideSensitiveInfo, indexContinuous } from '../../../../utils/bonus'
import { encryptCBC } from '@/utils/aescbc'

export default {
  name: 'Post',
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工程表格数据
      subList: [],
      //审核类型下拉选
      statusDict: [],
      //承包商下拉选项
      consOptions: [],
      //分包商拉选项
      subOptions: [],
      // 是否显示弹出层
      subOpen: false,
      // 是否隐藏承包商
      hideOrNot: false,
      createTime: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyWord: undefined,
        status: '1',
        contName: undefined,
        subName: undefined,
      },
      form: {},
      rules: {},
      auditStatusDict: {
        1: '待审批',
        2: '审批中',
        3: '已通过',
        4: '已驳回',
        5: '已撤销',
      },
    }
  },
  created() {
    const userType = this.$store.state.user.userType
    if (userType !== '02') {
      this.hideOrNot = true
      this.getConsOption()
      this.getSubOption()
    }
    this.getList()
    this.getStatus()
  },
  methods: {
    indexContinuous,
    hideSensitiveInfo,
    updateStatus(status) {
      return this.auditStatusDict[status]
    },
    /** 查询工程列表 */
    getList() {
      this.loading = true
      this.queryParams.proId = this.$store.state.user.thisIds.proId
      this.queryParams.contUuid = this.$store.state.user.thisIds.consUuid
      this.queryParams.supUuid = this.$store.state.user.thisIds.supUuid
      this.queryParams.subUuid = this.$store.state.user.thisIds.subUuid
      this.queryParams.userType = this.$store.state.user.userType
      listSubEntry(this.queryParams).then(response => {
        this.subList = response.rows
        this.total = response.total
        this.loading = false
        console.log(this.subList)
      })
    },
    //获取审核类型下拉选
    getStatus() {
      const params = {
        dictType: 'sys_approval_state',
        dictValue: '',
      }
      dictTableOption(params).then(response => {
        this.statusDict = response.data
      })
    },
    //获取承包商下拉选项
    getConsOption() {
      const params = {
        id: '',
      }
      getConsOptions(params).then(res => {
        this.consOptions = res.data
      })
    },
    //获取分包商下拉选项
    getSubOption() {
      const params = {
        id: '',
      }
      getSubOptions(params).then(res => {
        this.subOptions = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.keyWord = undefined
      this.queryParams.status = undefined
      this.queryParams.subName = undefined
      this.queryParams.contName = undefined
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.proId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.$router.push(
        '/pro/approval/subApproval/' +
          encryptCBC(
            JSON.stringify({
              proId: row.proId,
              consUuid: row.consUuid,
              supUuid: row.supUuid,
              subUuid: row.subUuid,
              uuid: row.uuid,
              taskId: row.taskId,
              procInstId: row.procInsId,
              finalCheck: row.finalCheck,
              isExamine: false,
              btnShow: false,
              showType: 1,
            })
          )
      )
    },
    /** 审批按钮操作 */
    handleApprove(row) {
      this.$router.push(
        '/pro/approval/subApproval/' +
          encryptCBC(
            JSON.stringify({
              proId: row.proId,
              consUuid: row.consUuid,
              supUuid: row.supUuid,
              subUuid: row.subUuid,
              uuid: row.uuid,
              taskId: row.taskId,
              procInstId: row.procInsId,
              finalCheck: row.finalCheck,
              isExamine: true,
              btnShow: true,
              showType: 2,
            })
          )
      )
    },
    /** 审核记录按钮操作 */
    handleAuditRecord(row) {
      this.$router.push(
        '/flow/auditRecord/' +
          encryptCBC(
            JSON.stringify({
              taskId: row.taskId,
              checkType: '2',
              taskType: '存在',
            })
          )
      )
    },
  },
}
</script>
