<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-tabs v-model="filterStatus" @tab-click="handleFilterStatusClick">
        <el-tab-pane label="全部" name="-1" :lazy="true"></el-tab-pane>
        <el-tab-pane label="待审核" name="0" :lazy="true"></el-tab-pane>
        <el-tab-pane label="审核通过" name="1" :lazy="true"></el-tab-pane>
        <el-tab-pane label="审核拒绝" name="3" :lazy="true"></el-tab-pane>
      </el-tabs>
      <div class="filter-box">
        <filter-user ref="filterUser" :filter-types="filterTypes" @select-value="handleFilterUser"></filter-user>
      </div>
      <el-table :data="tableData" stripe style="width: 100%;" @selection-change="handleSelectionChange" :height="tool.getTableHeight(true)">
        <el-table-column type="selection" width="55" v-if="tableHeaders"></el-table-column>
        <el-table-column v-for="(tableHeader, key) in tableHeaders" :key="key" :label="tableHeader.header" :width="tableHeader.other.width ? tableHeader.other.width : ''" :prop="tableHeader.other.prop ? tableHeader.other.prop : null">
          <template slot-scope="scope">
            <template v-if="scope.row[tableHeader.index].other.tag" >
              <el-tag :type="scope.row[tableHeader.index].other.tag.type ? scope.row[tableHeader.index].other.tag.type : null" :size="scope.row[tableHeader.index].other.tag.size ? scope.row[tableHeader.index].other.tag.size : null" :class="scope.row[tableHeader.index].other.tag.class ? scope.row[tableHeader.index].other.tag.class : null" >{{scope.row[tableHeader.index].value}}</el-tag>
            </template>
            <template v-else>
              <div v-html="scope.row[tableHeader.index].value"></div>
            </template>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180" v-if="tableHeaders">
          <template slot-scope="scope" v-if="scope.row.AUDIT_STATUS==='0' && (permission.hasPermission(`bonus/cf-lx-audit`)||permission.hasPermission(`bonus/cf-lx-audit-delete`))">
            <el-dropdown size="small" trigger="click">
              <el-button type="primary" size="small" @click.stop="">
                操作该数据<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="pass" @click.native="handleAudit(scope.row, 'true')" v-if="permission.hasPermission(`bonus/cf-lx-audit`)">审核通过</el-dropdown-item>
                <el-dropdown-item command="refuse" @click.native="handleAudit(scope.row, 'reject')" v-if="permission.hasPermission(`bonus/cf-lx-audit`)">审核拒绝
                </el-dropdown-item>
                <el-dropdown-item command="del" @click.native="handleDel(scope.row.ID)"
                                  v-show="permission.hasPermission(`bonus/cf-lx-audit-delete`)">删除数据
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-dropdown size="small" trigger="click" @command="handleMuli" v-if="(status===0) && (permission.hasPermission(`bonus/cf-lx-audit`)||permission.hasPermission(`bonus/cf-lx-audit-delete`))">
          <el-button type="primary" size="small">
            所选数据<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="pass" v-if="permission.hasPermission(`bonus/cf-lx-audit`)">批量审核</el-dropdown-item>
            <el-dropdown-item command="reject" v-if="permission.hasPermission(`bonus/cf-lx-audit`)">批量拒审</el-dropdown-item>
            <el-dropdown-item command="del" @click.native="handleDel()" v-if="permission.hasPermission(`bonus/cf-lx-audit-delete`)">批量删除</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <el-button type="success" size="small" @click="handleExport" v-show="permission.hasPermission(`bonus/cf-lx-audit-export`)">导出Excel</el-button>
        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"></pagination>
      </div>
    </div>
  </div>
</template>

<script>
  import network from '@/utils/network'
  import tool from '@/utils/tool'
  import FilterUser from '@/components/FilterUser'
  import baseInfo from '@/utils/baseInfo'
  import permission from '@/utils/permission'
  import Pagination from '@/components/Pagination'
  import filterHelper from '@/utils/filterHelper'

  export default {
    name: 'bonus_cf-lx-audit-list',
    components: {FilterUser,Pagination},
    mounted() {
      this.getData()
    },
    props: {
      status: {
        type: Number,
        default: 0,
      }
    },
    data() {
      return {
        tableHeaders: null,
        allData: null,
        tableData: null,
        loading: true,
        multipleSelection: [],
        currentPage: 1,
        totalPages: 1,
        totalCount: 1,
        pageSize: 20,
        tool: tool,
        permission: permission,
        filterTypes: null,
        filterModel: {},
        baseInfo: baseInfo,
        auditId: null,
        form: {
          id: null,
          baseInfo: {USER_NAME: null},
          balance: {
            unusedPV: 0,
            unusedMonth: 0,
          },
          type: null,
          changeAmount: null,
          changePv: null,
          changeMonth: null,
          remark: null,
          isShow: null,
          auditStatus: null
        },
        filterStatus: '0',
      }
    },
    methods: {
      handleMuli(command) {
        if (this.multipleSelection.length < 1) {
          this.$message({
            message: '请选择要操作的记录',
            type: 'warning'
          })
          return;
        }
        if (command === 'pass') {
          this.handleAudit(null, 'true')
        } else if (command === 'reject') {
          this.handleAudit(null, 'reject')
        }
      },
      handleChangeType() {
        if (this.form.type == 0) {
          this.form.changeAmount = this.form.changePv
        } else if (this.form.type == 1) {
          this.form.changeAmount = this.form.changeMonth
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleCurrentChange(page) {
        this.getData(page, this.pageSize)
      },
      handleSizeChange(pageSize) {
        this.getData(this.currentPage, pageSize)
      },
      handleAudit(row = null, status) {
        let obj = this
        let title = '确定要通过审核？请输入备注'
        if (status === 'reject') {
          title = '确定要拒绝审核？请输入备注'
        }
        this.$prompt(title, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({value}) => {
          let selectedIds = []
          if (row === null) {
            for (let val of obj.multipleSelection) {
              selectedIds.push(val.ID)
            }
          } else {
            selectedIds.push(row.ID)
          }
          this.$message({
            message: '正在审核请稍后',
            type: 'info'
          })
          return network.postData(`bonus/cf-lx-audit`, {
            selectedIds: selectedIds,
            remark: value,
            auditStatus: status
          })
        }).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          this.getData(this.currentPage, this.pageSize)
        }).catch(response => {

        })
      },
      handleDel(id = null) {
        let obj = this
        this.$confirm('确定删除选定的数据？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let selectedIds = []
          if (id === null) {
            for (let val of obj.multipleSelection) {
              selectedIds.push(val.ID)
            }
          } else {
            selectedIds.push(id)
          }
          return network.postData(`bonus/cf-lx-audit-delete`, {
            selected: selectedIds
          })
        }).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          obj.getData(obj.currentPage, obj.pageSize)
        }).catch(response => {

        })
      },
      handleFilterStatusClick(tab, event) {
        filterHelper.clearFilterOption(this)
        this.getData()
      },
      handleFilterUser(filterData) {
        filterHelper.handleFilterUser(this, filterData)
      },
      handleFilter() {
        this.getData()
      },
      getData(page, pageSize) {
        let filterData = this.filterModel
        filterData.filterStatus = this.filterStatus != '-1' ? `=,${this.filterStatus}` : ''
        let vueObj = this
        network.getPageData(this, 'bonus/cf-lx-audit-list', page, pageSize, filterData, function (response) {
          vueObj.filterTypes = response.filterTypes
          vueObj.allData = response
        })
      },
      handleExport(){
        this.$confirm(`确定要导出当前数据吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return network.getData(`bonus/cf-lx-audit-export`, this.filterModel)
        }).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
        }).catch(response => {

        })
      },
    },
  }
</script>

<style scoped>

</style>
