<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.username" clearable class="filter-item" style="width: 200px;" placeholder="请输入用户名" />
      <el-input v-model="listQuery.mobile" clearable class="filter-item" style="width: 200px;" placeholder="请输入手机号" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" width="100px" label="用户ID" prop="customerNo" sortable />

      <el-table-column align="center" label="用户名" prop="customerName" />
      <el-table-column align="center" label="微信昵称" prop="nickname" />

      <el-table-column align="center" label="手机号码" prop="tel" />

      <el-table-column align="center" label="是否为合伙人" :formatter="formatBoolean" prop="isDis" />
      <el-table-column align="center" label="是否实名制" :formatter="formatNid" prop="customerIdcard" />

      <el-table-column align="center" label="性别" prop="gender">
        <template slot-scope="scope">
          <el-tag>{{ genderDic[scope.row.gender] }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column align="center" label="数据渠道" prop="channelId">
        <template slot-scope="scope">
          <el-tag type="primary">{{ scope.row.channelId | channelsFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="数据来源" prop="source" />

      <el-table-column align="center" label="状态" prop="state">
        <template slot-scope="scope">
          <el-tag>{{ statusDic[scope.row.state] }}</el-tag>
        </template>
      </el-table-column>

      <!--<el-table-column align="center" label="操作" width="200" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="success" v-if="!scope.row.isDis" v-permission="['POST /admin/user/star']"  @click="handleStarConfirm(scope.row)">设置为行星</el-button>
           </template>
      </el-table-column>-->

    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

  </div>
</template>

<script>
import { fetchList } from '@/api/user'
import { allChannel } from '@/api/channel'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
let vm

export default {
  name: 'User',
  components: { Pagination },
  filters: {
    channelsFilter(value) {
      const rs = vm.channels.filter((item) => {
        return item.id === value
      })

      if (rs != null && rs.length > 0) { return rs[0].name }
    }
  },
  data() {
    return {
      channels: [],
      list: null,
      total: 0,
      citys:[],
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        username: undefined,
        mobile: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      downloadLoading: false,
      genderDic: ['未知', '男', '女'],
      statusDic: ['可用', '禁用', '注销'],
      disType: ['否', '是']
    }
  },
  beforeCreate() {
    vm = this
  },
  created() {
    allChannel().then(res => {
      this.channels = res.data.data
    })
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['用户名', '手机号码', '性别', '生日', '状态']
        const filterVal = ['username', 'mobile', 'gender', 'birthday', 'status']
        var exportList = []
        this.list.map(v => {
          var obj = JSON.parse(JSON.stringify(v))
          obj.gender = this.$data.genderDic[obj.gender]
          obj.status = this.$data.statusDic[obj.status]
          exportList.push(obj)
        })
        excel.export_json_to_excel2(tHeader, exportList, filterVal, '用户信息')
        this.downloadLoading = false
      })
    },
    handleStarConfirm(row) {
      this.$confirm('确定将会员设置为合伙人?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleStar(row)
      })
    },
    handleStar(row) {

    },
    formatBoolean: function(row, column, cellValue) {
      var ret = ''
      if (cellValue) {
        ret = '是'
      } else {
        ret = '否'
      }
      return ret
    },
    formatNid: function(row, column, cellValue) {
      var ret = ''
      if (cellValue != undefined && cellValue != null && cellValue != '') {
        ret = '是'
      } else {
        ret = '否'
      }
      return ret
    }

  }
}
</script>

<style>
.el-table th.gutter {
  display: table-cell !important;
}
</style>
