<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div class="screen-container">
      <el-form ref="screenForm" :model="screenForm" label-width="86px" size="small" label-position="left">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="兑换码" prop="code">
              <el-input v-model="screenForm.code" placeholder="请输入兑换码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="昵称" prop="name">
              <el-input v-model="screenForm.name" placeholder="请输入昵称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="电话" prop="phone">
              <el-input v-model="screenForm.phone" placeholder="请输入电话"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="兑换时间" prop="exchangeDate">
              <el-date-picker
                v-model="screenForm.exchangeDate"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                clearable>
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="订单号" prop="orderId">
              <el-input v-model="screenForm.orderId" placeholder="请输入订单号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="订单日期" prop="orderDate">
              <el-date-picker
                v-model="screenForm.orderDate"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                clearable>
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="优惠券名称" prop="coupon">
              <el-input v-model="screenForm.coupon" placeholder="请输入优惠券名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="12" :lg="6">
            <el-form-item label="状态" prop="status">
              <el-select v-model="screenForm.status" placeholder="请选择状态">
                <el-option label="全部" value=""></el-option>
                <el-option label="未兑换" :value="0"></el-option>
                <el-option label="已兑换未使用" :value="1"></el-option>
                <el-option label="已使用" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :lg="24" class="tr">
            <el-form-item label="">
              <el-button size="small" @click="resetScreenForm">清空</el-button>
              <el-button size="small" type="primary" @click="submitScreenForm">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="mymain-container">
      <div class="btn-group clearfix">
        <div class="fr">
          <el-button size="small" type="primary" icon="el-icon-download" @click="handleExport">导出</el-button>
        </div>
      </div>

      <div class="table">
        <el-table v-loading="listLoading" :data="dataList" element-loading-text="Loading" border fit highlight-current-row stripe>
          <el-table-column align="center" label="活动名称" prop="name" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="活动有效期" prop="aaa" min-width="200">
            <template slot-scope="scope">
              {{ scope.row.startTime | dateToDayFilter }} 至 {{ scope.row.endTime | dateToDayFilter }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="兑换有效期" prop="aaa" min-width="200">
            <template slot-scope="scope">
              {{ scope.row.exchangeStartTime | dateToDayFilter }} 至 {{ scope.row.exchangeEndTime | dateToDayFilter }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="使用有效期" prop="aaa" min-width="200">
            <template slot-scope="scope">
              {{ scope.row.couponStartTime | dateToDayFilter }} 至 {{ scope.row.couponEndTime | dateToDayFilter }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="优惠券名称" prop="couponName" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="优惠券类型" prop="couponType" min-width="100">
            <template slot-scope="scope">
              {{ scope.row.couponType | couponTypeFilter }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="优惠券面值" prop="discountAmount" min-width="100"></el-table-column>
          <el-table-column align="center" label="兑换码" prop="code" min-width="120"></el-table-column>
          <el-table-column align="center" label="状态" prop="status2" min-width="120">
            <template slot-scope="scope">
              {{ scope.row.status2 | statusFilter }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="昵称" prop="userNickName" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="电话" prop="userMobile" min-width="120"></el-table-column>
          <el-table-column align="center" label="兑换时间" prop="userExchangeTime" min-width="160"></el-table-column>
          <el-table-column align="center" label="使用时间" prop="userUseTime" min-width="160"></el-table-column>
          <el-table-column align="center" label="订单号" prop="orderId" min-width="180" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="订单用户" prop="userName" min-width="120" show-overflow-tooltip></el-table-column>
          <el-table-column align="center" label="订单状态" prop="orderStatus" min-width="120">
            <template slot-scope="scope">
              {{ scope.row.orderStatus | orderStatusFilter }}
            </template>
          </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>
    </div>


    <div class="page-footer">
      <div class="footer">
        <el-button @click="goBack">返 回</el-button>
      </div>
    </div>


  </div>
</template>

<script>
import { getRecords } from "@/api/draw";
import { downloadFiles } from '@/utils/util'

export default {
  filters: {
    couponTypeFilter(val) {
      const MAP = {
        SATISFY: '满减券',
        GOODS: '商品券',
      }
      return MAP[val]
    },
    statusFilter(val) {
      const MAP = {
        0: '未兑换',
        1: '已兑换未使用',
        2: '已使用',
      }
      return MAP[val]
    },
    orderStatusFilter(val) {
      const MAP = {
        NOPAY: '待付款',
        DFH: '待发货',
        YFH: '已发货',
        OVER: '已完成',
        CLOSE: '已关闭',
      }
      return MAP[val]
    }
  },
  data() {
    return {
      promotionLuckDrawId: null,
      promotionLuckDrawCouponId: null,
      dataList: null, // 列表数据
      listLoading: true, // 列表加载loading
      screenForm: { // 筛选表单数据
        code: '',
        name: '',
        phone: '',
        exchangeDate: [],
        orderId: '',
        orderDate: [],
        coupon: '',
        status: '',
      },
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      
    }
  },

  computed: {
    
  },

  created() {
    const { promotionLuckDrawId, promotionLuckDrawCouponId } = this.$route.query;
    this.promotionLuckDrawId = promotionLuckDrawId;
    this.promotionLuckDrawCouponId = promotionLuckDrawCouponId;
    this.getList();
  },

  methods: {

    getList() {
      this.listLoading = true;

      let params = {
        promotionLuckDrawId: this.promotionLuckDrawId,
        promotionLuckDrawCouponId: this.promotionLuckDrawCouponId,
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        code: this.screenForm.code,
        name: this.screenForm.name,
        mobile: this.screenForm.phone,
        exchangeStartTime: this.screenForm.exchangeDate.length ? this.screenForm.exchangeDate[0] + ' 00:00:00' : '',
        exchangeEndTime: this.screenForm.exchangeDate.length ? this.screenForm.exchangeDate[1] + ' 23:59:59' : '',
        orderId: this.screenForm.orderId,
        useStartTime: this.screenForm.orderDate.length ? this.screenForm.orderDate[0] + ' 00:00:00' : '',
        useEndTime: this.screenForm.orderDate.length ? this.screenForm.orderDate[1] + ' 23:59:59' : '',
        couponName: this.screenForm.coupon,
        status2: this.screenForm.status,
      }

      getRecords(params).then(res => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      })
    },

    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1;
      this.getList();
    },

    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields();
      this.currentPage = 1;
      this.getList();
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getList();
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },

    // 导出
    handleExport() {
      let screenData = {
        promotionLuckDrawId: this.promotionLuckDrawId || '',
        promotionLuckDrawCouponId: this.promotionLuckDrawCouponId || '',
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        code: this.screenForm.code,
        name: this.screenForm.name,
        mobile: this.screenForm.phone,
        exchangeStartTime: this.screenForm.exchangeDate.length ? this.screenForm.exchangeDate[0] + ' 00:00:00' : '',
        exchangeEndTime: this.screenForm.exchangeDate.length ? this.screenForm.exchangeDate[1] + ' 23:59:59' : '',
        orderId: this.screenForm.orderId,
        useStartTime: this.screenForm.orderDate.length ? this.screenForm.orderDate[0] + ' 00:00:00' : '',
        useEndTime: this.screenForm.orderDate.length ? this.screenForm.orderDate[1] + ' 23:59:59' : '',
        couponName: this.screenForm.coupon,
        status2: this.screenForm.status,
      };
      screenData = this.$deleteEmptyObj(screenData);
      downloadFiles('promotion/luck/draw/exchange/export', screenData);
    },

    goBack() {
      this.$router.go(-1);
      // this.$store.dispatch("tagsView/delView", this.$route);
    },
  }
}
</script>

<style scoped lang="scss">

</style>
