<template>
  <div class="app-container">
    <el-row :gutter="24">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
          <el-form-item label="审核状态" prop="approveStatus">
            <el-select
              v-model="queryParams.approveStatus"
              placeholder="请选择审核状态"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in approveStatusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="审核结果" prop="approveResult">
            <el-select
              v-model="queryParams.approveResult"
              placeholder="请选择审核结果"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="item in approveResultOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          </el-form-item>
        </el-form>
        <div style="padding-top: 10px">
          <vxe-table
            ref="xTable"
            align="center"
            :loading="loading"
            class="mytable-style"
            auto-resize
            border
            resizable
            export-config
            highlight-current-row
            show-overflow
            max-height="700"
            :data="approveList"
            :radio-config="{trigger: 'row'}"
          >
            <vxe-table-column field="moduleName" title="模块名称"/>
            <vxe-table-column field="operation" title="执行操作" :formatter="operationFormat"/>
            <vxe-table-column field="approveStatus" title="审核状态" :formatter="approveStatusFormat"/>
            <vxe-table-column field="createTime" title="提交时间"/>
            <vxe-table-column field="parameterContent" title="内容"/>
            <vxe-table-column field="approveResult" title="审核结果" :formatter="approveResultFormat"/>
            <vxe-table-column title="操作" width="320">
              <template slot-scope="scope" v-if="scope.row.approveStatus == 0">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-check"
                  @click="handleApprove(scope.row,0)"
                  v-hasPermi="['approveManager:approve:submitApprove']"
                >通过
                </el-button>

                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-close"
                  @click="handleApprove(scope.row,1)"
                  v-hasPermi="['approveManager:approve:submitApprove']"
                >未通过
                </el-button>
              </template>
            </vxe-table-column>
          </vxe-table>
          <vxe-pager
            v-show="showTable"
            perfect
            :current-page.sync="currentPage"
            :page-size.sync="pageSize"
            :total="total"
            :page-sizes="[10,50,100]"
            :layouts="['PrevJump', 'PrevPage','JumpNumber', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
            @page-change="handlePageChange"
          >
          </vxe-pager>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {debounce} from 'lodash'

export default {
  name: "User",
  data() {
    return {
      approveStatusOptions: [
        {value: '0', label: '待审核'},
        {value: '1', label: '已审核'}
      ],
      approveResultOptions: [
        {value: '0', label: '通过'},
        {value: '1', label: '未通过'}
      ],
      operationOptions: [
        {value: '0', label: '新增'},
        {value: '1', label: '修改'},
        {value: '2', label: '注销'},
        {value: '3', label: '授权'}
      ],
      // 遮罩层
      loading: true,
      showTable: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      currentPage: 1,
      pageSize: 10,
      // 总条数
      total: 0,
      // 表格数据
      approveList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        approveStatus: undefined,
        approveResult: undefined
      },
      edit: false
    };
  },
  watch: {},
  created() {
    this.getList()
  },
  methods: {
    handlePageChange({currentPage, pageSize}) {
      this.currentPage = currentPage
      this.pageSize = pageSize
      this.getList()
    },
    // 列表状态格式化
    operationFormat({cellValue}) {
      let belongTo = '未知的类型'
      for (let i = 0; i < this.operationOptions.length; i++) {
        if (cellValue == "0") {
          belongTo = "新增"
        } else if (cellValue == "1") {
          belongTo = "修改"
        } else if (cellValue == "2") {
          belongTo = "注销"
        } else if (cellValue == "3") {
          belongTo = "授权"
        }
      }
      return belongTo
    },
    approveStatusFormat({cellValue}) {
      let belongTo = '未知的类型'
      for (let i = 0; i < this.approveStatusOptions.length; i++) {
        if (cellValue == "0") {
          belongTo = "待审批"
        } else if (cellValue == "1") {
          belongTo = "已审批"
        }
      }
      return belongTo
    },
    approveResultFormat({cellValue}) {
      let belongTo = ''
      for (let i = 0; i < this.approveResultOptions.length; i++) {
        if (cellValue == "0") {
          belongTo = "通过"
        } else if (cellValue == "1") {
          belongTo = "未通过"
        }
      }
      return belongTo
    },
    /** 查询用户列表 */
    async getList() {
      let sysTime
      let lk
      await this.$axios.get('/sysPolicyController/getLicenseKey').then((res) => {
        sysTime = res.data.sysTime
        lk = res.data.lk
      }).catch((error) => {
      })

      this.loading = true;
      var searchParams = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        approveStatus: this.queryParams.approveStatus,
        approveResult: this.queryParams.approveResult,
        sysTime: sysTime,
        lk: lk
      }
      await this.$axios.get('/sysApproveController/getAll',
        {params: searchParams}).then((res) => {
        this.approveList = res.data.records
        this.total = res.data.total

        if (res.data.records == '') {
          this.showTable = false
        } else {
          this.showTable = true
        }
        this.loading = false
      }).catch((error) => {
        this.loading = false;
        // this.$message.error(error)
      })
    },
    /** 搜索按钮操作 */
    handleQuery: debounce(function () {
      this.currentPage = 1
      this.pageSize = 10
      this.getList()
    }, 1000),
    handleApprove(row, val) {
      let tips = ''
      if (val == 0) {
        tips = '【通过】'
      } else {
        tips = '【未通过】'
      }
      this.$confirm('确认审核结果' + tips + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.approveResult = val
        this.submitApprove(row)
      }).catch(() => {
      });
    },
    /** 提交按钮 */
    submitApprove: debounce(async function (row) {
      let sysTime
      let lk
      await this.$axios.get('/sysPolicyController/getLicenseKey').then((res) => {
        sysTime = res.data.sysTime
        lk = res.data.lk
      }).catch((error) => {
      })

      row.sysTime = sysTime
      row.lk = lk
      await this.$axios.post('/sysApproveController/submitApprove', row).then((res) => {
        if (res.code == 0) {
          this.$message.success('审核成功')
          this.getList();
        }
        if (res.code == 1) {
          this.$message.error(res.data)
          this.getList();
        }
      }).catch((error) => {
        // this.$message.error(error)
        this.getList();
      })
    }, 1000)
  }
};
</script>
