<template>
  <div class="app-container">
    <el-form
      :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/代金券名称搜索"
          clearable
          style="width: 180px"
        />
      </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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="voucherAdd"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="title" />
      <el-table-column label="类型" align="center">
        <template slot-scope="scope">
          {{
            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="inviteStoreNum"
      />
      <el-table-column
        label="已同意加入店铺数量"
        align="center"
        prop="agreeStoreNum"
      />
      <el-table-column label="面额(元)" align="center" prop="sendAmount" />
      <el-table-column label="总发放数量" align="center" prop="sendNum" />
      <el-table-column label="已领取数量" align="center" prop="receiveNum" />

      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            style="color: #00b529"
            @click="handleLook(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            style="color: red"
            @click="handleRemove(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 moment from "moment";
import { queryPage, voucherRemove } from "@/api/marketing/voucher";
export default {
  data() {
    return {
      loading: true,
      total: 0,
      title: "",
      list: [],
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        dateRange: [],
        type: "",
        key: "",
        showType: "",
      },
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleChoose(record) {
      this.$emit("voucherChoose", record);
    },

    handleRemove(record) {
      this.$confirm("是否确认删除该代金券?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          voucherRemove(record.id).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleLook(record) {
      this.$router.push({
        path: "/marketing/marketing/voucher/add",
        query: { id: record.id, type: "look" },
      });
    },

    handleEdit(record) {
      this.$router.push({
        path: "/marketing/marketing/voucher/add",
        query: { id: record.id },
      });
    },
    voucherAdd() {
      // this.$store.dispatch("tagsView/delView", this.$route); //关闭当前页
      this.$router.push({ path: "/marketing/marketing/voucher/add" });
    },

    datePicker(val) {
      this.queryParams.startTime = moment(val[0]).valueOf();
      this.queryParams.endTime = moment(val[1]).valueOf();
    },
    getList() {
      this.loading = true;
      queryPage(this.queryParams).then((res) => {
        this.list = res.data.records;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.endTime = "";
      this.queryParams.startTime = "";
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
</style>
<style scoped lang="scss">
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>
