<template>
  <div class="app-container live-page">
        <div class="live-page__header">
      <h2 class="live-page__title">优惠券用户</h2>
    </div>

<div class="live-panel" v-show="showSearch">
<el-form class="live-search-form" :model="queryParams" ref="queryForm" :inline="true" label-width="100px">

      <el-form-item label="优惠券名称" prop="couponTitle">
        <el-input
          v-model="queryParams.couponTitle"
          placeholder="请输入优惠券名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
       <el-form-item label="状态" prop="status">
         <el-select   v-model="queryParams.status" placeholder="请选择状态" clearable size="small" >
         <el-option
                v-for="item in statusOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
        </el-select>
      </el-form-item>
      <el-form-item label="领取时间">
            <el-date-picker v-model="dateRange" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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>
    <div class="live-panel live-panel--table">
<el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['company:companySmsLogs:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['store:storeCouponUser:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table   height="500" border   v-loading="loading" :data="storeCouponUserList" @selection-change="handleSelectionChange" class="live-table">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="会员昵称" align="center" prop="nickname" />
      <el-table-column label="会员手机号" align="center" prop="phone" />
      <el-table-column label="优惠券名称" align="center" prop="couponTitle" />
      <el-table-column label="优惠券的面值" align="center" prop="couponPrice" />
      <el-table-column label="最低消费" align="center" prop="useMinPrice" />
      <el-table-column label="优惠券结束时间" align="center" prop="limitTime" width="180">
      </el-table-column>
      <el-table-column label="领取时间" align="center" prop="createTime" width="180"></el-table-column>
      <el-table-column label="使用时间" align="center" prop="useTime" width="180">
      </el-table-column>
      <!-- <el-table-column label="获取方式" align="center" prop="type" /> -->
      <el-table-column label="状态" align="center" prop="status" >
          <template slot-scope="scope">
              <el-tag prop="status" v-for="(item, index) in statusOptions"    v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
          </template>
      </el-table-column>
    </el-table>
    </div>

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

  </div>
</template>

<script>
import { listStoreCouponUser, getStoreCouponUser, delStoreCouponUser, addStoreCouponUser, updateStoreCouponUser, exportStoreCouponUser } from "@/api/live/liveCouponUser";

export default {
  name: "StoreCouponUser",
  data() {
    return {
      statusOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      dateRange:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券发放记录表格数据
      storeCouponUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        couponId: null,
        userId: null,
        couponTitle: null,
        couponPrice: null,
        useMinPrice: null,
        endTime: null,
        useTime: null,
        type: null,
        status: null,
        isFail: null,
        isDel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        couponId: [
          { required: true, message: "兑换的项目id不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "优惠券所属用户不能为空", trigger: "blur" }
        ],
        couponTitle: [
          { required: true, message: "优惠券名称不能为空", trigger: "blur" }
        ],
        couponPrice: [
          { required: true, message: "优惠券的面值不能为空", trigger: "blur" }
        ],
        useMinPrice: [
          { required: true, message: "最低消费多少金额可用优惠券不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "优惠券创建时间不能为空", trigger: "blur" }
        ],
        endTime: [
          { required: true, message: "优惠券结束时间不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "获取方式不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        isFail: [
          { required: true, message: "是否有效不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDicts("store_coupon_user_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询优惠券发放记录列表 */
    getList() {
      this.loading = true;
      listStoreCouponUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.storeCouponUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        couponId: null,
        userId: null,
        couponTitle: null,
        couponPrice: null,
        useMinPrice: null,
        createTime: null,
        updateTime: null,
        endTime: null,
        useTime: null,
        type: null,
        status: 0,
        isFail: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加优惠券发放记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStoreCouponUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改优惠券发放记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStoreCouponUser(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addStoreCouponUser(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除优惠券发放记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStoreCouponUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有优惠券发放记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStoreCouponUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
