<template>
  <div v-loading="loading">
    <div class="white-box">
      <div class="filter-box">
        <filter-user :filter-types="filterTypes" @select-value="handleFilterUser"></filter-user>
      </div>
      <el-table :data="tableData" stripe style="width: 100%;" :height="tool.getTableHeight()">
        <el-table-column v-for="(tableHeader, key) in tableHeaders" :key="key" :label="tableHeader.header" :width="tableHeader.other.width ? tableHeader.other.width : ''">
          <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-if="scope.row[tableHeader.index].other.progress" >
              <el-progress type="circle" :percentage="Number.parseInt(percentList['MOVE_PERCENT'][scope.row.ID])"
                           :width="50"
                           :stroke-width="3"></el-progress>
            </template>
            <template v-else>
              <div v-html="scope.row[tableHeader.index].value"></div>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150" v-if="tableHeaders">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click.native="handleChangeUserName(scope.row.USER_NAME.value)" v-show="permission.hasPermission(`reconsume/change-audit-add`)">调整复销池余额</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-button type="primary" size="small" @click="handleChange" icon="el-icon-plus" v-show="permission.hasPermission(`reconsume/change-audit-add`)">申请调整复销池余额</el-button>
        <el-button type="primary" size="small" @click="handleDeduct" icon="el-icon-plus" v-show="permission.hasPermission(`reconsume/deduct-audit-add`)">手动扣除月复销</el-button>
        <el-button type="success" size="small" @click="handleExport"
                   v-show="permission.hasPermission(`reconsume/pool-list-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 Pagination from '@/components/Pagination'
import permission from '@/utils/permission'
import filterHelper from '../../utils/filterHelper'

export default {
  name: 'reconsume_pool-list',
  components: {FilterUser,Pagination},
  mounted () {
    this.getData()
  },
  data () {
    return {
      tableHeaders: null,
      allData: null,
      tableData: null,
      loading: true,
      multipleSelection: [],
      currentPage: 1,
      totalPages: 1,
      totalCount: 1,
      pageSize: 20,
      tool: tool,
      permission: permission,
      filterTypes: {
        'USER_NAME': {isUserTable: true, name: '会员编号'},
        'REAL_NAME': {isUserTable: true, name: '会员姓名'},
        'STATUS': {isUserTable: true, name: '会员状态', other: 'status'},
        'DEC_LV': {isUserTable: true, name: '实时会员级别', other: 'decLevel'},
        'LAST_DEC_LV': {isUserTable: true, name: '结算时会员级别', other: 'decLevel'},
        'EMP_LV': {isUserTable: true, name: '实时聘级', other: 'empLevel'},
        'UNUSED_PV': {isUserTable: false, name: '复销池余额'},
        'UNUSED_MONTH': {isUserTable: false, name: '赠送优惠期剩余月数'},
        'CREATED_AT': {isUserTable: true, name: '加入时间', other: 'date'},
        'PERIOD_AT': {isUserTable: true, name: '加入期数'},
      },
      filterModel: {},
      baseInfo: baseInfo,
    }
  },
  methods: {
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleCurrentChange (page) {
      this.getData(page, this.pageSize)
    },
    handleSizeChange (pageSize) {
      this.getData(this.currentPage, pageSize)
    },
    handlePass(row){
      this.$confirm('确定要为该会员扣除复销并设为合格吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return network.postData(`reconsume/deduct`, {
          userName: row.USER_NAME.value,
          isPass: true,
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.getData(this.currentPage, this.pageSize)
      }).catch(response => {

      })
    },
    handleNotPass(row){
      this.$confirm('确定要为该会员扣除复销并设为不合格吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return network.postData(`reconsume/deduct`, {
          userName: row.USER_NAME.value,
          isPass: false,
        })
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
        this.getData(this.currentPage, this.pageSize)
      }).catch(response => {

      })
    },
    handleDeduct() {
      this.$router.push(`/reconsume/deduct-audit-add`)
    },
    handleChangeUserName(userName) {
      this.$router.push({path:`/reconsume/change-audit-add/${userName}`})
    },
    handleChange() {
      this.$router.push({path:`/reconsume/change-audit-add`})
    },
    handleFilterUser(filterData) {
      filterHelper.handleFilterUser(this, filterData)
    },
    handleFilter() {
      this.getData()
    },
    getData(page, pageSize) {
      let filterData = this.filterModel
      let vueObj=this
      network.getPageData(this, 'reconsume/pool-list', page, pageSize, filterData, function (response) {
        vueObj.allData = response
        vueObj.filterTypes = response.filterTypes
      })
    },
    handleExport(){
      this.$confirm(`确定要导出当前数据吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return network.getData(`reconsume/pool-list-export`, this.filterModel)
      }).then(response => {
        this.$message({
          message: response,
          type: 'success'
        })
      }).catch(response => {

      })
    },
  }
}

</script>

<style scoped>

</style>
