<template>
  <div class="app-container">
    <div style="text-align: right; margin-bottom: 10px">
      <el-date-picker
        v-model="dateRange"
        type="datetimerange"
        range-separator="-"
        value-format="yyyy-MM-dd HH:mm:ss"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        @change="tjdatePicker"
      >
      </el-date-picker>
    </div>
    <div class="header" style="width: 100%">
      <el-row class="row" :gutter="12">
        <el-col :span="3">
          <div class="item">
            <div class="count">
              {{ orderStatistics ? orderStatistics.amount : 0 }} 个
            </div>
            <div class="title">
              <span>总发放数量</span>
            </div>
          </div>
        </el-col>
        <el-col :span="3">
          <div class="item">
            <div class="count">
              {{ orderStatistics ? orderStatistics.receiveNum : 0 }}
            </div>
            <div class="title">
              <span>总发放金额(元)</span>
            </div>
          </div>
        </el-col>
        <el-col :span="3">
          <div class="item">
            <div class="count">
              {{ orderStatistics ? orderStatistics.sendAmount : 0 }}个
            </div>
            <div class="title">
              <span>全部领取人数</span>
            </div>
          </div>
        </el-col>
        <el-col :span="3">
          <div class="item">
            <div class="count">
              {{ orderStatistics ? orderStatistics.sendNum : 0 }}个
            </div>
            <div class="title">
              <span>全部领取金额(元)</span>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-form
      style="margin-top: 20px"
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="80px"
    >
      <el-form-item label="" prop="key">
        <el-input
          v-model="queryParams.key"
          placeholder="请输入代金券ID/代金券名称/用户Id/用户昵称/用户手机号搜索"
          clearable
          style="width: 300px"
        />
      </el-form-item>
      <el-form-item label="推广方式" prop="showType">
        <el-select
          v-model="queryParams.showType"
          placeholder="请选择推广方式"
          style="width: 180px"
        >
          <el-option label="全部" value=""> </el-option>
          <el-option label="全网自动推出" value="0"> </el-option>
          <el-option label="定向渠道推广" value="1"> </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择券类型"
          style="width: 180px"
        >
          <el-option label="全部" value=""> </el-option>
          <el-option label="独立券" value="1"> </el-option>
          <el-option label="连锁券" value="2"> </el-option>
          <el-option label="联合券" value="3"> </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="领取时间" prop="dateRange">
        <el-date-picker
          v-model="queryParams.dateRange"
          type="datetimerange"
          range-separator="-"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="datePicker"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="spuList"
      :border="true"
      row-key="id"
    >
      <el-table-column type="selection" align="center" />
      <el-table-column label="领取人ID" align="center" prop="userId" />
      <el-table-column label="领取人昵称" align="center" prop="nick" />
      <el-table-column label="领取人手机号" align="center" prop="phone" />
      <el-table-column label="代金券ID" align="center" prop="voucherId" />
      <el-table-column label="代金券名称" align="center" prop="voucherName" />

      <el-table-column label="类型" align="center" width="120">
        <template slot-scope="scope">
          {{
            scope.row.type == 0
              ? "通用券"
              : scope.row.type == 1
              ? "独立券"
              : scope.row.type == 2
              ? "连锁券"
              : scope.row.type == 3
              ? "联合券"
              : ""
          }}
        </template>
      </el-table-column>

      <el-table-column label="推广方式" align="center">
        <template slot-scope="scope">
          {{
            scope.row.showType == 0
              ? "全网自动推出"
              : scope.row.showType == 1
              ? "定向渠道推广"
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="面额(元)" align="center" prop="sendAmount" />
      <el-table-column label="领取时间" align="center">
        <template slot-scope="scope">
          {{
            scope.row.reviewTime
              ? $moment(scope.row.reviewTime).format("YYYY-MM-DD HH:mm:ss")
              : ""
          }}
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import moment from "moment";
import { voucherStatistics, recordPage } from "@/api/marketing/voucher";
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      total: 0,
      // 参数表格数据
      spuList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      dateRange: [],
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        key: "",
        showType: "",
        startTime: "",
        endTime: "",
        type: "",
        dateRange: [],
      },
      orderStatistics: {},
      queryStatistics: {
        startTime: "",
        endTime: "",
      },
    };
  },
  mounted() {
    this.getList();
    this.getstatisticsOrder();
  },
  methods: {
    tjdatePicker(val) {
      if (val) {
        this.queryStatistics.startTime = moment(val[0]).valueOf();
        this.queryStatistics.endTime = moment(val[1]).valueOf();
      } else {
        this.queryStatistics.startTime = "";
        this.queryStatistics.endTime = "";
      }
      this.getstatisticsOrder();
    },

    datePicker(val) {
      this.queryParams.startTime = moment(val[0]).valueOf();
      this.queryParams.endTime = moment(val[1]).valueOf();
    },
    getstatisticsOrder() {
      voucherStatistics(this.queryStatistics).then((res) => {
        if (res.code == 200) {
          this.orderStatistics = res.data;
        } else {
          this.$modal.msgError(res.msg);
        }
      });
    },

    /** 查询参数列表 */
    getList() {
      this.loading = true;
      recordPage(this.queryParams).then((response) => {
        if (response.code == 200) {
          this.spuList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      });
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.endTime = "";
      this.queryParams.startTime = "";
      this.resetForm("queryForm");
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
  },
};
</script>
<style scoped lang="scss">
.header {
  .item {
    height: 100px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    line-height: 40px;
  }
  .row {
    background: #f4f4f4;
    padding: 10px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    .title {
      text-align: center;
    }
    .count {
      text-align: center;
      font-size: 20px;
      font-weight: bold;
      color: rgb(64, 158, 255);
    }
    .zz {
      font-size: 14px;
      color: red;
      padding: 0px 10px;
    }
  }
}
.radio {
  margin: 20px 0px;
}
</style>
<style>
.el-radio-button__inner {
  width: 150px;
}
</style>
