<template>
  <div class="purchase-gift-record-list-con">
    <Card>
      <div class="search-con">
        <Input
          placeholder="请输入用户编号"
          class="search-input"
          v-model="searchContent.uid"
          style="width: 200px"
          clearable
        />
        <ButtonGroup class="search-btngroup">
          <Button @click="handleSearch" type="primary">搜索</Button>
          <Button type="primary" @click="handleRefresh">
            <Icon type="md-refresh" />
          </Button>
        </ButtonGroup>
      </div>
      <div class="total-con">总计：{{ totalSize }} 条记录</div>
      <tables
        ref="tables"
        pagable
        v-model="tableData"
        :columns="columns"
        :currentPage="searchContent.pageIndex"
        :pageSize="searchContent.pageSize"
        :totalSize="totalSize"
        :loading="loading"
        @on-change="handleCurrentChange"
      />
    </Card>
  </div>
</template>

<script>
import Tables from '_c/tables'
import { formatTime } from '@/libs/tools'
export default {
  inject: ['reload'],
  components: {
    Tables
  },
  data () {
    const RecordState = {
      WAIT: 0,
      SUCCESS: 1,
      FAIL: 2
    }
    const RecordStateObj = {
      [RecordState.WAIT]: {
        label: '待发放',
        color: 'default'
      },
      [RecordState.SUCCESS]: {
        label: '已发放',
        color: 'success'
      },
      [RecordState.FAIL]: {
        label: '发放失败',
        color: 'warning'
      }
    }
    return {
      RecordState,
      RecordStateObj,

      searchContent: {
        activityId: '',
        uid: '',
        pageIndex: 1,
        pageSize: 10
      },
      columns: [
        { title: '编号', key: 'recordId', minWidth: 180 },
        { title: '用户编号', key: 'uid', minWidth: 180 },
        { title: '订单编号', key: 'oid', minWidth: 180 },
        { title: '通证编号', key: 'tokenId', minWidth: 180 },
        {
          title: '状态',
          key: 'status',
          width: 120,
          render: (h, params) => {
            let obj = RecordStateObj[params.row.status]
            return h(
              'Tag',
              {
                props: {
                  color: obj.color
                }
              },
              obj.label
            )
          }
        },
        {
          title: '记录时间',
          minWidth: 180,
          render: (h, params) => {
            return h('span', formatTime(params.row.createTime))
          }
        },
        {
          title: '更新时间',
          minWidth: 180,
          render: (h, params) => {
            return h('span', formatTime(params.row.updateTime))
          }
        }
      ],
      tableData: [],
      totalSize: 0,
      loading: true
    }
  },
  methods: {
    requestData () {
      this.loading = true
      this.searchContent.uid = this.searchContent.uid.trim()
      this.api({
        apiName: 'purchaseGiftEventRecordList',
        postdata: this.searchContent,
        complete: () => {
          this.loading = false
        },
        success: (res) => {
          this.tableData = res.data.records
          this.totalSize = res.data.total
        }
      })
    },
    handleSearch () {
      this.searchContent.pageIndex = 1
      this.requestData()
    },
    handleRefresh () {
      this.reload()
    },
    handleCurrentChange (pageNum) {
      this.searchContent.pageIndex = pageNum
      this.requestData()
    }
  },
  created () {
    this.searchContent.activityId = this.$route.query.activityId
    this.requestData()
  }
}
</script>

<style lang="less">
.purchase-gift-record-list-con {
  .search-con {
    padding: 10px 0;
    .search {
      &-col {
        display: inline-block;
        width: 150px;
        margin-right: 5px;
      }
      &-input {
        display: inline-block;
        width: 150px;
        margin-right: 5px;
      }
      &-date {
        margin-right: 5px;
      }
      &-btngroup {
        margin-right: 5px;
      }
    }
  }
  .total-con {
    margin-bottom: 10px;
  }
}
</style>
