<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠券id" prop="couponId">
        <el-input
          v-model="queryParams.couponId"
          placeholder="请输入优惠券id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="couponTitle">
        <el-input
          v-model="queryParams.couponTitle"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="面值" prop="couponPrice">
        <el-input
          v-model="queryParams.couponPrice"
          placeholder="请输入面值"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最低消费" prop="minPrice">
        <el-input
          v-model="queryParams.minPrice"
          placeholder="请输入最低消费"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="领取时间" prop="addTime">
        <el-input
          v-model="queryParams.addTime"
          placeholder="请输入领取时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="失效时间" prop="loseTime">
        <el-input
          v-model="queryParams.loseTime"
          placeholder="请输入失效时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用时间" prop="useTime">
        <el-input
          v-model="queryParams.useTime"
          placeholder="请输入使用时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指定分类" prop="cateId">
        <el-input
          v-model="queryParams.cateId"
          placeholder="请输入指定分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指定商品" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入指定商品"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="${comment}" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择${comment}">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="${comment}" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择${comment}">
        </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
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:CouponUser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:CouponUser:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:CouponUser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:CouponUser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="CouponUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="用户" align="center" prop="uid" />
      <el-table-column label="优惠券id" align="center" prop="couponId" />
      <el-table-column label="名称" align="center" prop="couponTitle" />
      <el-table-column label="面值" align="center" prop="couponPrice" />
      <el-table-column label="最低消费" align="center" prop="minPrice" />
      <el-table-column label="领取时间" align="center" prop="addTime" />
      <el-table-column label="失效时间" align="center" prop="loseTime" />
      <el-table-column label="使用时间" align="center" prop="useTime" />
      <el-table-column label="指定分类" align="center" prop="cateId" />
      <el-table-column label="指定商品" align="center" prop="productId" />
      <el-table-column label="1:通用券 2：品类券 3：指定商品" align="center" prop="receiveType" />
      <el-table-column label="1：未使用 2：已使用 3:已失效" align="center" prop="status" />
      <el-table-column label="${comment}" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="${comment}" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </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"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:CouponUser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:CouponUser:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改优惠券领取记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户" />
        </el-form-item>
        <el-form-item label="优惠券id" prop="couponId">
          <el-input v-model="form.couponId" placeholder="请输入优惠券id" />
        </el-form-item>
        <el-form-item label="名称" prop="couponTitle">
          <el-input v-model="form.couponTitle" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="面值" prop="couponPrice">
          <el-input v-model="form.couponPrice" placeholder="请输入面值" />
        </el-form-item>
        <el-form-item label="最低消费" prop="minPrice">
          <el-input v-model="form.minPrice" placeholder="请输入最低消费" />
        </el-form-item>
        <el-form-item label="领取时间" prop="addTime">
          <el-input v-model="form.addTime" placeholder="请输入领取时间" />
        </el-form-item>
        <el-form-item label="失效时间" prop="loseTime">
          <el-input v-model="form.loseTime" placeholder="请输入失效时间" />
        </el-form-item>
        <el-form-item label="使用时间" prop="useTime">
          <el-input v-model="form.useTime" placeholder="请输入使用时间" />
        </el-form-item>
        <el-form-item label="指定分类" prop="cateId">
          <el-input v-model="form.cateId" placeholder="请输入指定分类" />
        </el-form-item>
        <el-form-item label="指定商品" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入指定商品" />
        </el-form-item>
        <el-form-item label="${comment}" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="${comment}" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择${comment}">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCouponUser, getCouponUser, delCouponUser, addCouponUser, updateCouponUser } from "@/api/system/CouponUser"

export default {
  name: "CouponUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券领取记录表格数据
      CouponUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        uid: null,
        couponId: null,
        couponTitle: null,
        couponPrice: null,
        minPrice: null,
        addTime: null,
        loseTime: null,
        useTime: null,
        cateId: null,
        productId: null,
        receiveType: null,
        status: null,
        createdAt: null,
        updatedAt: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        uid: [
          { required: true, message: "用户不能为空", trigger: "blur" }
        ],
        couponId: [
          { required: true, message: "优惠券id不能为空", trigger: "blur" }
        ],
        couponTitle: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        couponPrice: [
          { required: true, message: "面值不能为空", trigger: "blur" }
        ],
        minPrice: [
          { required: true, message: "最低消费不能为空", trigger: "blur" }
        ],
        addTime: [
          { required: true, message: "领取时间不能为空", trigger: "blur" }
        ],
        receiveType: [
          { required: true, message: "1:通用券 2：品类券 3：指定商品不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "1：未使用 2：已使用 3:已失效不能为空", trigger: "change" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询优惠券领取记录列表 */
    getList() {
      this.loading = true
      listCouponUser(this.queryParams).then(response => {
        this.CouponUserList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        uid: null,
        couponId: null,
        couponTitle: null,
        couponPrice: null,
        minPrice: null,
        addTime: null,
        loseTime: null,
        useTime: null,
        cateId: null,
        productId: null,
        receiveType: null,
        status: null,
        createdAt: null,
        updatedAt: 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
      getCouponUser(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) {
            updateCouponUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addCouponUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除优惠券领取记录编号为"' + ids + '"的数据项？').then(function() {
        return delCouponUser(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/CouponUser/export', {
        ...this.queryParams
      }, `CouponUser_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
