<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="有效期" prop="limitTime">
        <el-date-picker v-model="limitTime" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="change"></el-date-picker>
      </el-form-item>
      <el-form-item label="面额" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入面额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="couponList">
      <el-table-column label="优惠券名称" align="center" prop="title" width="120px"/>
      <el-table-column label="面额" align="center" prop="price" />
      <el-table-column label="折扣" align="center" prop="rate" />
      <el-table-column label="数量" align="center" prop="number" />
      <el-table-column label="卷类型 " align="center" prop="couponType" width="120px">
        <template slot-scope="scope">
          <dict-tag :options="couponTypeOptions" :value="scope.row.couponType"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="最低消费金额可用" align="center" prop="minPrice" />
      <el-table-column label="剩余数量" align="center" prop="remainNumber" />
      <el-table-column label="有效期" align="center" prop="limitTime" width="180"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="更改时间" align="center" prop="updateTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="selectCoupon(scope.row)"
          >选择
          </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"
    />
  </div>
</template>

<script>
import { listCoupon } from "@/api/his/coupon";
export default {
  name: "CouponComponent",
  data() {
    return {
      limitTime:[],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券表格数据
      couponList: [],
      statusOptions: [],
      // 卷类型 1代金券 字典
      couponTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        limitTime: null,
        price: null,
        number: null,
        couponType: 7,
        minPrice: null,
        remainNumber: null,
        sTime:null,
        eTime:null
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_coupon_type").then(response => {
      this.couponTypeOptions = response.data;
    });
    this.getDicts("sys_company_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    selectCoupon(row) {
      this.$emit("select-coupon", {"id": row.couponId, "name": row.title });
    },
    /** 查询优惠券列表 */
    getList() {
      this.loading = true;
      listCoupon(this.queryParams).then(response => {
        this.couponList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        couponId: null,
        title: null,
        createTime: null,
        limitTime: null,
        price: null,
        number: null,
        couponType: null,
        minPrice: null,
        remainNumber: null,
        status:"1",
        rate:0,
        limitDay:1,
        limitCount:1,
        limitType:null,
        cateIds:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.limitTime=null;
      this.queryParams.sTime=null;
      this.queryParams.eTime=null;
      this.handleQuery();
    },
    change(){
      if(this.limitTime!=null){
        this.queryParams.sTime=this.limitTime[0];
        this.queryParams.eTime=this.limitTime[1];
      }else{
        this.queryParams.sTime=null;
        this.queryParams.eTime=null;
      }
    },
  }
};
</script>
<style scoped>
::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  height: 70vh;
}
</style>
