<template>
  <div class="address-list">
    <div class="table">
      <el-table
        ref="orderTable"
        v-loading="listLoading"
        :data="dataList"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row stripe
      >
        <el-table-column align="center" label="券名称" prop="couponName"></el-table-column>
        <el-table-column align="center" label="面额" prop="couponValue"></el-table-column>
        <el-table-column align="center" label="券状态" prop="state">
          <template slot-scope="{ row }">
            {{ row.state ? '可用' : '不可用' }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="有效时间">
          <template slot-scope="{ row }">
            {{ formatDate(row) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="分享状态" prop="transferType">
          <template slot-scope="{ row }">
            {{ row.transferType ? '可分享' : '不可分享' }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="剩余数量" prop="leftShareTimes"></el-table-column>
        <el-table-column align="center" label="领取明细">
          <template slot-scope="{ row }">
            <el-button type="text" @click="openShareDetail(row.id)">查看明细</el-button>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" label="使用时间" prop="useTime"></el-table-column> -->

      </el-table>
    </div>
    <div class="pagination clearfix">
      <div class="fr">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal">
        </el-pagination>
      </div>
    </div>

    <!-- 分享详情 -->
    <el-dialog title="分享详情" :visible.sync="shareDialog" :show-close="false" width="70%" :close-on-click-modal="false">
      <div class="table" style="margin: 10px 0 20px;">
        <el-table 
          v-loading="shareTable_listLoading" 
          :data="shareTable_dataList" 
          element-loading-text="Loading" 
          tooltip-effect="dark" 
          style="width: 100%" 
          max-height="270">
          <el-table-column align="center" prop="userName" label="用户名称" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" prop="mobile" label="电话" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" prop="couponName" label="券名称" min-width="120"></el-table-column>
          <el-table-column align="center" prop="userPhone" label="数量" min-width="120">
            <template>
              1
            </template>
          </el-table-column>
          <el-table-column align="center" prop="receiveTime" label="领取时间" min-width="160"></el-table-column>
        </el-table>
      </div>
      <div class="pagination clearfix">
        <div class="fr">
          <el-pagination
            @current-change="shareTableCurrentChange"
            :current-page="shareTable_currentPage"
            :page-size="shareTable_pageSize"
            background
            layout="prev, pager, next"
            :total="shareTable_listTotal">
          </el-pagination>
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="shareDialog = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getMemberCouponList, getShareList} from "@/api/member";
import {dateFormat} from "@/utils/util"

export default {
  name: 'MemberCoupon',
  componentName: 'MemberCoupon',
  props: ['user'],
  data() {
    return {
      dataList: [], // 数据列表
      listLoading: true, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数

      detailId: '',
      shareDialog: false, // 分享详情 - 弹窗
      shareTable_dataList: null, // 分享详情 - 列表数据
      shareTable_listLoading: true, // 分享详情 - 列表加载loading
      shareTable_currentPage: 1, // 分享详情 - 当前页码
      shareTable_pageSize: 10, // 分享详情 - 每页数量
      shareTable_listTotal: 0, // 分享详情 - 列表总数
    }
  },
  created() {
    this.getCouponList()
  },
  methods: {
    getCouponList() {
      let params = {
        userId: this.user.userId,
        pageNum: this.currentPage,
        pageSize: this.pageSize
      }

      getMemberCouponList(params).then(res => {
        this.dataList = res.data.records
        this.listTotal = res.data.total
        this.listLoading = false
      })
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getCouponList()
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getCouponList()
    },
    formatDate(row) {
      return dateFormat('YYYY.mm.dd', new Date(row.activeStartTime)) + ' - ' + dateFormat('YYYY.mm.dd', new Date(row.activeEndTime))
    },

    // 分享详情 - 获取列表
    getShareList() {
      getShareList({
        pageNo: this.shareTable_currentPage,
        pageSize: this.shareTable_pageSize,
        userCouponId: this.detailId,
        // userId: this.user.userId,
      }).then(res => {
        this.shareTable_dataList = res.data.records;
        this.shareTable_listTotal = res.data.total;
        this.shareTable_listLoading = false;
      })
    },

    // 分享详情 - 打开弹窗
    openShareDetail(id) {
      this.detailId = id;
      this.shareDialog = true;
      this.shareTable_currentPage = 1;
      this.getShareList();
    },

    // 分享详情 - 更改列表当前页
    shareTableCurrentChange(val) {
      this.shareTable_currentPage = val;
      this.getShareList();
    },
  }
}
</script>

<style scoped>

</style>
