<template>
  <div class="join-shop-list-container">
    <!-- 查询结果 -->
    <el-table v-loading="listLoading" element-loading-text="正在查询中..." :data="list" v-bind="{ stripe: true, size: 'small', border: true, highlightCurrentRow: true }">
      <el-table-column prop="allianceCardId" label="联盟卡ID" width="90" align="center" />
      <el-table-column prop="buyerUserId" label="购买者会员ID" width="100" align="center" />
      <el-table-column prop="buyerUserIdPromoters" label="推广者会员ID" width="120" align="center" />
      <el-table-column prop="buyerUserName" label="会员名称" width="120" align="center" />
      <el-table-column prop="buyerUserLogo" label="会员头像" width="120" align="center">
        <template slot-scope="scope">
          <el-image :preview-src-list="[scope.row.buyerUserLogo]" v-if="scope.row.buyerUserLogo" :src="scope.row.buyerUserLogo" fit="fill" style="width: 40px; height: 40px"></el-image>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column prop="state" label="支付状态" width="120" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 1" size="mini" type="info">未支付</el-tag>
          <el-tag v-if="scope.row.state === 2" size="mini" type="success">已支付</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="购买时间" align="center" />
      <el-table-column align="center" label="操作" width="120" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <el-button size="mini" @click="$refs.consumptionDialogRef && $refs.consumptionDialogRef.handleOpen(row.allianceCardId, row.buyerUserId)">查看消费记录</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="margin-top: 10px">
      <el-pagination
        :current-page="listQuery.page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, -> ,jumper"
        :total="total"
        @size-change="(val) => (listQuery.pageSize = val) && getList()"
        @current-change="(val) => (listQuery.page = val) && getList()"
      />
    </div>

    <!-- 查看消费记录 -->
    <ConsumptionDialog ref="consumptionDialogRef"></ConsumptionDialog>
  </div>
</template>

<script>
import { getBuyerPurchaseByByIdApi } from '@/api/business'
import ConsumptionDialog from './ConsumptionDialog.vue'

export default {
  components: { ConsumptionDialog },
  props: {
    cardId: { type: [Number, String], required: true }
  },

  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      listQuery: {
        allianceCardId: undefined,
        page: 1,
        pageSize: 20
      }
    }
  },

  watch: {
    cardId: {
      handler(value) {
        this.listQuery.allianceCardId = value
        value && this.getList()
      },
      immediate: true
    }
  },

  methods: {
    // 获取商家列表
    async getList() {
      try {
        this.listLoading = true
        const res = await getBuyerPurchaseByByIdApi(this.listQuery)
        this.list = res.data.list
        this.total = res.data.total
        this.listLoading = false
      } catch (error) {
        this.listLoading = false
      }
    }
  }
}
</script>
