<template>
  <div v-loading="loading">
    <div class="white-box">
      <div class="leo-filter" v-loading="loading">
        <div :class="`filter-item filter-date-range-picker`">
          <el-input v-model="filterModel.userName" placeholder="请输入会员编号">
            <template slot="append">【{{realName}}】</template>
          </el-input>
        </div>
        <el-button class="filter-item filter-filter-btn" type="primary" @click="handleChkUser">确认</el-button>
        <el-button class="filter-item filter-filter-btn" type="primary" @click="handleFilter">筛选</el-button>
      </div>
      <el-table class="table-box" ref="multipleTable" :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>
              <div v-html="scope.row[tableHeader.index].value"></div>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-button type="success" size="small" @click="handleExport" v-show="permission.hasPermission(`user/group-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 Pagination from '@/components/Pagination'
  import permission from '@/utils/permission'

  export default {
    name: 'user_group',
    components: {Pagination},
    mounted() {
      this.loading = false
    },
    data() {
      return {
        tableHeaders: null,
        tableData: null,
        loading: true,
        multipleSelection: [],
        currentPage: 1,
        totalPages: 1,
        totalCount: 1,
        pageSize: 20,
        tool: tool,
        permission: permission,
        filterData: null,
        filterModel: {
          userName: '',
        },
        realName: '',
      }
    },
    methods: {
      handleChkUser() {
        if (this.filterModel.userName) {
          this.loading = true
          network.getData('user/full-info', {userName: this.filterModel.userName}).then(response => {
            this.realName = response.REAL_NAME
            this.loading = false
          }).catch(response => {
            this.loading = false
          })
        }
      },
      handleCurrentChange(page) {
        this.getData(page, this.pageSize)
      },
      handleSizeChange(pageSize) {
        this.getData(this.currentPage, pageSize)
      },
      handleFilter() {
        if (this.filterModel.userName === '') {
          this.$message({
            message: '请填写会员编号',
            type: 'warning'
          })
          return false
        }
        this.filterData = this.filterModel
        this.getData(1, this.pageSize)
      },
      getData(page, pageSize) {
        let vueObj = this
        network.getPageData(this, 'user/group', page, pageSize, this.filterData, function (response) {
          vueObj.tableData = response.list
        })
      },
      handleExport(){
        this.$confirm(`确定要导出当前数据吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return network.getData(`user/group-export`, this.filterModel)
        }).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
        }).catch(response => {

        })
      },
    }
  }

</script>

<style scoped>
  .leo-filter {
    padding: 0 0 15px 0;
  }

  .leo-filter .filter-hidden {
    display: none;
  }

  .filter-item {
    margin-right: 10px;
  }

  @media (min-width: 768px) {
    .leo-filter {
    }

    .leo-filter .filter-item {
      display: inline-block;
      max-width: 200px;
    }

    .leo-filter .filter-item.filter-date-month-picker {
      display: inline-block;
      max-width: 220px;
    }

    .leo-filter .filter-item.filter-date-range-picker {
      display: inline-block;
      max-width: 380px;
    }

    .leo-filter .filter-item.filter-filter-btn {
      height: 38px;
      width: 80px;
    }

    .leo-filter .filter-hidden {
      display: none;
    }
  }

  .table-box .el-form-item__label {
    width: 100px;
    color: #99a9bf;
  }

  .table-box .el-form-item {
    width: 30%;
    margin-right: 0;
    margin-bottom: 0;
  }
</style>
