<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.sysOrigin"
        :placeholder="$t('pages.userDiamondRunWater.filter.system')"
        style="width: 120px"
        class="filter-item"
        @change="changeSysOrigin"
      >
        <el-option
          v-for="item in permissionsSysOriginPlatforms"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
          <span style="float: left;"> <sys-origin-icon :icon="item.value" :desc="item.value" /></span>
          <span style="float: left;margin-left:10px">{{ item.label }}</span>
        </el-option>
      </el-select>
      <div class="filter-item">
        <div><account-input v-model="listQuery.userId" :placeholder="$t('pages.userDiamondRunWater.filter.userId')" type="USER" :sys-origin="listQuery.sysOrigin" /></div>
      </div>
      <el-select
        v-model="listQuery.type"
        :placeholder="$t('pages.userDiamondRunWater.filter.type')"
        style="width: 120px"
        class="filter-item"
        clearable
        @change="handleSearch"
      >
        <el-option :label="$t('pages.userDiamondRunWater.type.income')" :value="false" />
        <el-option :label="$t('pages.userDiamondRunWater.type.expense')" :value="true" />
      </el-select>
      <div class="filter-item">
        <el-autocomplete
          v-model="listQuery.origin"
          popper-class="my-autocomplete"
          :fetch-suggestions="querySearch"
          :placeholder="$t('pages.userDiamondRunWater.filter.origin')"
          clearable
          @select="handleSelect"
          @clear="handleSelect"
        >
          <i
            slot="suffix"
            class="el-icon-edit el-input__icon"
          />
          <template slot-scope="{ item }">
            <span :label="getDiamondOriginName(item)">{{ getDiamondOriginName(item) }}</span>
            <div :key="item.value" :value="item.value" style="font-size:12px; color:#d8d0d0">{{ item.value }}</div>
          </template>
        </el-autocomplete>
      </div>
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
      >
        {{ $t('pages.userDiamondRunWater.action.search') }}
      </el-button>

    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      fit
      highlight-current-row
    >
      <el-table-column :label="$t('pages.userDiamondRunWater.table.user')" align="center" min-width="100">
        <template slot-scope="scope">
          <user-table-exhibit :user-profile="scope.row.userProfile" :query-details="true" />
        </template>
      </el-table-column>
      <el-table-column prop="balance" :label="$t('pages.userDiamondRunWater.table.diamondBalance')" align="center" min-width="120" />
      <el-table-column prop="quantity" :label="$t('pages.userDiamondRunWater.table.diamond')" align="center" min-width="100" />
      <el-table-column prop="origin" :label="$t('pages.userDiamondRunWater.table.source')" align="center" min-width="120" />
      <el-table-column prop="originDesc" :label="$t('pages.userDiamondRunWater.table.sourceDescription')" align="center" min-width="150" />
      <el-table-column prop="typeDesc" :label="$t('pages.userDiamondRunWater.table.type')" align="center" min-width="100" />
      <el-table-column prop="remarks" :label="$t('pages.userDiamondRunWater.table.remarks')" align="center" min-width="120" />
      <el-table-column
        prop="createTime"
        :label="$t('pages.userDiamondRunWater.table.createdAt')"
        align="center"
        width="200"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />
    <user-deatils-drawer
      v-if="userDeatilsDrawer"
      :user-id="thatSelectedUserId"
      @close="userDeatilsDrawer=false"
    />
  </div>
</template>

<script>
import { pageUserDiamondRunWater } from '@/api/app-user'
import { mapGetters } from 'vuex'
import Pagination from '@/components/Pagination'
import { pickerOptions } from '@/constant/el-const'
import { diamondOrigins } from '@/constant/type'
export default {
  name: 'UserSongCount',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      pickerOptions,
      diamondOrigins,
      regions: [],
      restaurants: [],
      userDeatilsDrawer: false,
      thatSelectedUserId: '',
      listQuery: {
        cursor: 1,
        limit: 20,
        sysOrigin: 'HALAR',
        userId: '',
        origin: '',
        type: ''
      },
      listLoading: true
    }
  },
  computed: {
    ...mapGetters(['permissionsSysOriginPlatforms'])
  },
  created() {
    const that = this
    const querySystem = this.permissionsSysOriginPlatforms[0]
    if (!querySystem) {
      return
    }
    that.listQuery.sysOrigin = querySystem.value
    that.renderData(true)
  },
  mounted() {
    this.restaurants = this.diamondOrigins
  },
  methods: {
    renderData(isReset) {
      const that = this
      if (isReset === true) {
        that.listQuery.cursor = 1
      }
      that.listLoading = true
      pageUserDiamondRunWater(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      })
    },
    changeSysOrigin() {
      this.handleSearch()
    },
    handleSelect(item) {
      this.renderData(true)
    },
    handleSearch() {
      if (this.listQuery.origin !== '') {
        const origins = this.diamondOrigins.filter(this.createFilter(this.listQuery.origin))
        this.listQuery.origin = origins.length > 0 ? origins[0].value : this.listQuery.origin
      }
      this.renderData(true)
    },
    queryUserDetails(userId) {
      this.userDeatilsDrawer = true
      this.thatSelectedUserId = userId
    },
    querySearch(queryString, cb) {
      var restaurants = diamondOrigins
      var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants
      cb(results)
    },
    getDiamondOriginName(origin) {
      const value = origin && origin.value ? origin.value : origin
      const fallback = origin && origin.name ? origin.name : value
      const key = `pages.userDiamondRunWater.origin.${value}`
      const label = this.$t(key)
      return label === key ? fallback : label
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) >= 0) || (restaurant.name.toLowerCase().indexOf(queryString.toLowerCase()) >= 0)
      }
    }
  }
}
</script>
