<template>
  <section class="static-detial" v-loading="loading">
    <dm-sub-title title-align="space-between">
      奖品数据详情
    </dm-sub-title>
    <div class="tips mt5 mb20">数据实时更新</div>
    <div class="pb20 clearfix">
      <el-input v-model="form.prizeName" class="w260" placeholder="请输入奖品名称" clearable @change="refresh"><i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
      <el-input v-model="form.search" class="w270" placeholder="请输入姓名/昵称/手机号/会员卡号" clearable @change="refresh"><i slot="prefix" class="el-input__icon el-icon-search"></i></el-input>
      <el-date-picker class="w256" v-model="form.dateTime" value-format="yyyy-MM-dd" @change="refresh" type="daterange" range-separator="~" start-placeholder="开始日期" end-placeholder="结束日期"> </el-date-picker>
      <el-select class="w160" v-model="form.prizeType" placeholder="所有奖品类型" @change="refresh" clearable>
        <el-option v-for="(v, i) in prizeTypeOptions" :key="i" :label="v.label" :value="v.value"></el-option>
      </el-select>
      <el-button icon="iconfont fz14 icon-cp-xiazai" type="primary" class="fr" @click="handleExport" size="small" :disabled="!total"> 查询结果导出</el-button>
    </div>
    <el-table tooltipEffect="light" :data="tableList" style="width:100%">
      <el-table-column label="奖品名称" min-width="204" prop="prizeName" fixed="left" show-overflow-tooltip :formatter="(row, col, val) => val || '--'"></el-table-column>
      <el-table-column label="奖品类型" min-width="183" prop="prizeType" :formatter="(row, col, val) => prizeTypeObj[val]"></el-table-column>
      <el-table-column label="中奖会员" min-width="214">
        <div slot-scope="{ row }" class="member-info">
          <img class="member-logo" v-if="row.memberImage" :src="row.memberImage" alt="" />
          <svg aria-hidden="true" v-else class="member-logo">
            <use xlink:href="#icon-user-default"></use>
          </svg>
          <div>
            <p class="member-name">
              <template v-if="row.memberNick">
                {{ row.memberNick || '--' }}
              </template>
              <span class="member-subname" v-if="row.memberName">
                {{ row.memberNick ? `(${row.memberName})` : row.memberName }}
              </span>
            </p>
            <p class="member-name" v-if="row.memberMobile">{{ row.memberMobile }}</p>
          </div>
        </div>
      </el-table-column>
      <el-table-column label="会员卡号" min-width="184" prop="memberCard" :formatter="(row, col, val) => val || '--'"></el-table-column>
      <el-table-column label="兑奖方式" min-width="184" prop="exchangeTypeName" :formatter="(row, col, val) => val || '--'"></el-table-column>
      <el-table-column label="中奖时间" min-width="183" prop="prizeTime" fixed="right">
        <template slot-scope="{ row }">
          {{ formatDateTimeByType(row.prizeTime, 'yyyy-MM-dd') || '--' }}
          <br />
          {{ formatDateTimeByType(row.prizeTime, 'HH-mm-ss') || '--' }}
        </template>
      </el-table-column>
    </el-table>
    <dm-pagination v-show="tableList.length" background class="dm-pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="form.pageNum" :page-sizes="[20, 40, 60, 80]" :page-size="form.pageSize" layout="total, sizes, prev, pager, next" :total="total"></dm-pagination>
    <vue-gic-export-excel :dialogVisible.sync="dialogVisible" :dataArr="tableList" :type="2" :excelUrl="exportGamePrizeDetail" :params="params" projectName="marketing"></vue-gic-export-excel>
  </section>
</template>

<script>
import { formatDateTimeByType } from '@/utils/index.js';
import { getPrizeDetail, exportGamePrizeDetail } from '@/service/api/gameApi.js';
export default {
  data() {
    return {
      tableList: [],
      form: {
        prizeName: '',
        pageNum: 1,
        pageSize: 20,
        gameId: this.$route.params.id,
        dateTime: [],
        search: '',
        prizeType: ''
      },
      prizeTypeOptions: [
        {
          label: '积分',
          value: 1
        },
        {
          label: '卡券',
          value: 2
        }
      ],
      prizeTypeObj: {
        1: '积分',
        2: '卡券'
      },
      formatDateTimeByType,
      total: 0,
      dialogVisible: false,
      exportGamePrizeDetail,
      params: {},
      loading: false
    };
  },
  mounted() {
    this.getTableList();
  },
  methods: {
    handleExport() {
      this.dialogVisible = true;
      const { search, prizeType, prizeName } = this.form;
      this.params = {
        gameId: this.$route.params.id,
        search,
        prizeType,
        prizeName
      };
      if (this.form.dateTime && this.form.dateTime.length) {
        this.params.startTime = this.form.dateTime[0];
        this.params.endTime = this.form.dateTime[1];
      }
    },
    refresh() {
      this.form.pageNum = 1;
      this.getTableList();
    },
    handleSizeChange(val) {
      this.form.pageSize = val;
      this.getTableList();
    },
    handleCurrentChange(val) {
      this.form.pageNum = val;
      this.getTableList();
    },
    getTableList() {
      const params = { ...this.form };
      this.loading = true;
      if (params.dateTime && params.dateTime.length) {
        params.startTime = params.dateTime[0];
        params.endTime = params.dateTime[1];
      }
      delete params.dateTime;
      getPrizeDetail(params)
        .then(res => {
          const { result } = res;
          this.tableList = result.result;
          this.total = result.totalCount;
        })
        .finally(() => {
          this.loading = false;
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.static-detial {
  padding: 16px 20px;
}
.tips {
  font-weight: 400;
  color: #606266;
  line-height: 17px;
}
.member-info {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  .member-logo {
    flex-shrink: 0;
    margin-right: 10px;
    width: 40px;
    height: 40px;
    border-radius: 40px;
    overflow: hidden;
  }
  .member-name {
    font-size: 14px;
    font-weight: 400;
    color: #303133;
    line-height: 20px;
    .member-subname {
      color: #909399;
    }
  }
}
</style>
