<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="" prop="key">
        <el-input
          v-model="queryParams.key"
          placeholder="输入主题名称关键字搜索"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="红包过期时间" prop="queryParams">
        <el-date-picker
          v-model="queryParams.redEnvelopeExpire"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="redChange"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="推送时间" prop="push">
        <el-date-picker
          v-model="queryParams.push"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="pushChange"
        >
        </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>
    <div style="margin: 10px 0px">
      <el-button type="primary" size="mini" @click="hanleAdd">添加</el-button>
    </div>

    <el-table
      v-loading="loading"
      :data="configList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="主题名称" align="center" prop="topicName" />
      <el-table-column label="红包个数" align="center" prop="redEnvelopeNum" />
      <el-table-column label="红包单个金额(元)" align="center" prop="singleAmount" />
      <el-table-column label="红包总金额(元)" align="center">
        <template slot-scope="scope">
          {{ Number(scope.row.singleAmount) * Number(scope.row.redEnvelopeNum) }}
        </template>
      </el-table-column>
      <el-table-column label="红包过期时间" align="center" prop="redEnvelopeExpireTime">
        <template slot-scope="scope">
          {{
            scope.row.redEnvelopeExpireTime
              ? $moment(Number(scope.row.redEnvelopeExpireTime)).format(
                  "yyyy-MM-DD HH:mm:ss"
                )
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="已领取数量(个)" align="center" prop="receiveNum" />
      <el-table-column label="已领取金额(元)" align="center" prop="receivedAmount" />
      <el-table-column label="返还金额(元)" align="center" prop="expireAmount" />
      <el-table-column label="推送消息内容" align="center" prop="pushMessage" />
      <!-- <el-table-column label="用户数量(个)" align="center" prop="" /> -->
      <el-table-column label="推送时间" align="center" prop="pushTime">
        <template slot-scope="scope">
          {{
            scope.row.pushTime
              ? $moment(Number(scope.row.pushTime)).format("yyyy-MM-DD HH:mm:ss")
              : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleLookOrder(scope.row)"
            >查看</el-button
          >
        </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 * as markApi from "@/api/marketing/index";
export default {
  name: "Config",
  data() {
    return {
      loading: true,
      total: 0,
      configList: [],
      title: "",
      open: false,
      queryParams: {
        key: "",
        push: [],
        redEnvelopeExpire: [],
        pageNo: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      drawer: false,
      userid: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    redChange(val) {
      this.queryParams.redEnvelopeExpireStartTime = val[0];
      this.queryParams.redEnvelopeExpireEndTime = val[1];
    },
    pushChange(val) {
      this.queryParams.pushStartTime = val[0];
      this.queryParams.pushEndTime = val[1];
    },
    hanleAdd() {
      this.$router.push({
        path: "/marketing/marketing/index",
      });
    },

    handleLookOrder(record) {
      this.userid = record.userId;
      this.drawer = true;
      this.$router.push({
        path: "/marketing/marketing/setting",
        query: {
          id: record.id,
        },
      });
    },

    /** 查询参数列表 */
    getList() {
      console.log();
      this.loading = true;
      markApi.queryPage(this.queryParams).then((response) => {
        this.configList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;

      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.redEnvelopeExpireStartTime = "";
      this.queryParams.redEnvelopeExpireEndTime = "";
      this.queryParams.pushStartTime = "";
      this.queryParams.pushEndTime = "";
      this.queryParams.push = [];
      this.queryParams.redEnvelopeExpire = [];

      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style scoped lang="scss"></style>
