<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.status"
        :placeholder="$t('pages.cpApplicationLogs.placeholder.status')"
        style="width: 120px"
        class="filter-item"
        @change="handleSearch"
      >
        <el-option
          v-for="(item, index) in status"
          :key="index"
          :label="$t(`pages.cpApplicationLogs.status.${item.value}`)"
          :value="item.value"
        />
      </el-select>
      <div class="filter-item">
        <account-input v-model="listQuery.sendApplyUserId" :placeholder="$t('pages.cpApplicationLogs.placeholder.senderId')" />
      </div>
      <div class="filter-item">
        <account-input v-model="listQuery.acceptApplyUserId" :placeholder="$t('pages.cpApplicationLogs.placeholder.receiverId')" />
      </div>
      <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="$t('pages.cpApplicationLogs.word.loading')"
      fit
      highlight-current-row
    >
      <el-table-column :label="$t('pages.cpApplicationLogs.table.sender')" align="center" min-width="200">
        <template slot-scope="scope">
          <user-table-exhibit :user-profile="scope.row.sendApplyUser" :query-details="true" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.cpApplicationLogs.table.receiver')" align="center" min-width="200">
        <template slot-scope="scope">
          <user-table-exhibit :user-profile="scope.row.acceptApplyUser" :query-details="true" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.cpApplicationLogs.table.amount')" prop="applyConsumeGold" align="center" min-width="100" />
      <el-table-column :label="$t('pages.cpApplicationLogs.table.status')" prop="ip" align="center" min-width="100">
        <template slot-scope="scope">
          {{ statusMap[scope.row.status] ? $t(`pages.cpApplicationLogs.status.${scope.row.status}`) : '?' }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('pages.cpApplicationLogs.table.time')" width="200" align="center">
        <template slot-scope="scope">
          <div>{{ $t('pages.cpApplicationLogs.word.created') }}: {{ scope.row.createTime | dateFormat }}</div>
          <div>{{ $t('pages.cpApplicationLogs.word.updated') }}: {{ scope.row.updateTime | dateFormat }}</div>
        </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 { pageCpApply } from '@/api/table'
import Pagination from '@/components/Pagination'
import { toMap } from '@/utils'
export default {
  name: 'CandyPurchasing',
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      rangeDate: [],
      listQuery: {
        cursor: 1,
        limit: 20,
        sendApplyUserId: '',
        acceptApplyUserId: '',
        status: ''
      },
      listLoading: true,
      status: [
        { value: 'WAIT' },
        { value: 'AGREE' },
        { value: 'DISMISS' },
        { value: 'REFUSE' },
        { value: 'REIMBURSE' }
      ],
      statusMap: {}
    }
  },
  created() {
    this.statusMap = toMap(this.status, 'value')
    this.renderData()
  },
  methods: {
    renderData(isClean) {
      const that = this
      if (isClean === true) {
        that.list = []
        that.listQuery.cursor = 1
      }
      that.listLoading = true
      pageCpApply(that.listQuery).then(res => {
        const { body } = res
        that.total = body.total || 0
        that.list = body.records
        that.listLoading = false
      })
    },
    handleSearch() {
      this.renderData(true)
    },
    renderDataSuccess() {
      this.$message({
        message: 'Successful',
        type: 'success'
      })
      this.renderData()
    }
  }
}
</script>
