<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.badgeId')" prop="badgeConfigId" 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.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 type="text" @click.native="handleRetrieveBadge(scope.row.badgeConfigId)">{{ $t('pages.badgeManagement.common.retrieve') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import { listUserNotAchieveBadge, retrieveBadge } from '@/api/badge'
export default {
  name: 'BadgeGive',
  data() {
    return {
      list: [],
      listQuery: {
        userId: ''
      },
      formVisible: false,
      textOptTitle: '',
      submitLoading: false
    }
  },
  methods: {
    renderData() {
      const that = this
      that.listLoading = true
      that.$message({
        message: that.$t('pages.badgeManagement.message.querying'),
        type: 'warning'
      })
      listUserNotAchieveBadge(that.listQuery.userId).then(res => {
        that.listLoading = false
        const { body } = res
        that.list = body
      }).catch(er => {
        that.listLoading = false
      })
    },
    renderDataSuccess() {
      this.$message({
        message: this.$t('pages.badgeManagement.message.retrieveSuccess'),
        type: 'success'
      })
      this.renderData()
    },
    handleSearch() {
      this.renderData()
    },
    handleRetrieveBadge(badgeId) {
      this.$confirm(this.$t('pages.badgeManagement.confirm.retrieveOwned'), 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>
