<template>
  <div>
    <div class="filter-container">
      <account-input v-model="listQuery.userId" :placeholder="$t('pages.badgeManagement.filter.userId')" class="filter-item" />
      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
      >
        {{ $t('common.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.badgeManagement.common.badgeRuleId')" prop="id" align="center" />
      <el-table-column :label="$t('pages.badgeManagement.common.badgeIcon')" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="scope.row.selectUrl"
            :preview-src-list="[scope.row.selectUrl]"
          />
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.badgeManagement.common.badgeName')" prop="badgeName" align="center" />
      <el-table-column :label="$t('pages.badgeManagement.common.badgeType')" prop="typeName" align="center" />
      <el-table-column :label="$t('pages.badgeManagement.common.badgeKey')" prop="badgeKey" align="center" />
      <el-table-column :label="$t('pages.badgeManagement.common.level')" prop="badgeLevel" align="center" />
      <el-table-column :label="$t('pages.badgeManagement.common.milestone')" prop="milestone" align="center" />
      <el-table-column :label="$t('pages.badgeManagement.common.createdAt')" prop="createTime" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime | dateFormat }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.badgeManagement.common.actions')" fixed="right" align="center" width="200">
        <template slot-scope="scope">
          <el-button v-if="scope.row.presented" type="text" @click.native="handleRetrieveBadge(scope.row.id)">{{ $t('pages.badgeManagement.common.retrieve') }}</el-button>
          <el-button v-else-if="!scope.row.presented" type="text" @click.native="handleGiveBadge(scope.row.id)">{{ $t('pages.badgeManagement.common.give') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.cursor"
      :limit.sync="listQuery.limit"
      @pagination="renderData"
    />
  </div>
</template>

<script>
import { giveBadgeTable, giveBadge, retrieveBadge } from '@/api/badge'
import Pagination from '@/components/Pagination'
export default {
  name: 'BadgeGive',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listQuery: {
        cursor: 1,
        limit: 20,
        userId: ''
      },
      formVisible: false,
      textOptTitle: '',
      submitLoading: false
    }
  },
  methods: {
    renderData() {
      const that = this
      if (!that.listQuery.userId) {
        that.$message({
          message: that.$t('pages.badgeManagement.message.userIdRequired'),
          type: 'warning'
        })
        return
      }
      that.listLoading = true
      that.$message({
        message: that.$t('pages.badgeManagement.message.querying'),
        type: 'warning'
      })
      giveBadgeTable(that.listQuery).then(res => {
        that.listLoading = false
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
      }).catch(er => {
        that.listLoading = false
      })
    },
    renderDataSuccess() {
      this.$message({
        message: this.$t('pages.badgeManagement.message.retrieveSuccess'),
        type: 'success'
      })
      this.renderData()
    },
    handleSearch() {
      this.renderData()
    },
    handleGiveBadge(badgeId) {
      this.$confirm(this.$t('pages.badgeManagement.confirm.give'), this.$t('common.tip'), {
        type: 'warning'
      }).then(() => {
        this.$message({
          message: this.$t('pages.badgeManagement.message.giving'),
          type: 'warning'
        })
        this.listLoading = true
        giveBadge(badgeId, this.listQuery.userId).then((res) => {
          this.listLoading = false
          this.$message({
            message: this.$t('pages.badgeManagement.message.giveSuccess'),
            type: 'success'
          })
          this.renderData()
        })
      }).catch(() => {

      })
    },
    handleRetrieveBadge(badgeId) {
      this.$confirm(this.$t('pages.badgeManagement.confirm.retrieve'), this.$t('common.tip'), {
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        this.$message({
          message: this.$t('pages.badgeManagement.message.retrieving'),
          type: 'warning'
        })
        retrieveBadge(badgeId, this.listQuery.userId).then((res) => {
          this.listLoading = false
          this.$message({
            message: this.$t('pages.badgeManagement.message.retrieveSuccess'),
            type: 'success'
          })
          this.renderData()
        })
      }).catch(() => {

      })
    }
  }
}
</script>
