<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="titleZh">
        <el-input v-model="queryParams.titleZh" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <!-- <el-form-item label="标题" prop="titleEn">
        <el-input v-model="queryParams.titleEn" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item label="类型" prop="type">
        <!-- 1:优惠卷 2:金额-满减 3:方数-满减 4 折扣 5优惠 6 特价 7区间价-->
        <!-- <el-select v-model="queryParams.type" placeholder="请选择类型 1:优惠卷 2:金额-满减 3:方数-满减 4 折扣 5优惠 6 特价 7区间价" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select> -->
        <dict-selector :type='DICT_TYPE.ECW_COUPON_TYPE' v-model="queryParams.type" />
      </el-form-item>
      <!-- <el-form-item label="使用条件(优惠卷专用)" prop="cashCondition">
        <el-input v-model="queryParams.cashCondition" placeholder="请输入使用条件(优惠卷专用)" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="减免金额(优惠卷专用)" prop="reduceAmount">
        <el-input v-model="queryParams.reduceAmount" placeholder="请输入减免金额(优惠卷专用)" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="条件货币ID" prop="conditionCurrencyId">
        <el-input v-model="queryParams.conditionCurrencyId" placeholder="请输入条件货币ID" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item label="开始时间">
        <el-date-picker v-model="dateRangeStartTime" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <!-- <el-form-item label="减免货币ID" prop="reduceCurrencyId">
        <el-input v-model="queryParams.reduceCurrencyId" placeholder="请输入减免货币ID" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item label="结束时间">
        <el-date-picker v-model="dateRangeEndTime" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <!-- <el-form-item label="领券后X天起生效" prop="afterReceiveDays">
        <el-input v-model="queryParams.afterReceiveDays" placeholder="请输入领券后X天起生效" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="有效天数" prop="validDays">
        <el-input v-model="queryParams.validDays" placeholder="请输入有效天数" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item label="费用类型" prop="costType">
        <dict-selector :type="DICT_TYPE.ECW_COUPON_COST_TYPE" v-model="queryParams.costType"/>
      </el-form-item>
      <!-- <el-form-item label="优惠清单" prop="discountDetailed">
        <el-input v-model="queryParams.discountDetailed" placeholder="请输入优惠清单" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="同类优惠劵是否可以叠加 0否  1是" prop="isSimilarSuperposition">
        <el-select v-model="queryParams.isSimilarSuperposition" placeholder="请选择同类优惠劵是否可以叠加 0否  1是" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="可叠加优惠劵列表" prop="couponIds">
        <el-input v-model="queryParams.couponIds" placeholder="请输入可叠加优惠劵列表" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="适用用户类型 0全部用户 1部分用户 2指定用户不参与" prop="suitableUserType">
        <el-select v-model="queryParams.suitableUserType" placeholder="请选择适用用户类型 0全部用户 1部分用户 2指定用户不参与" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="适用线路类型 0全部线路参与 1指定线路参与 2指定线路不参与" prop="suitableLineType">
        <el-select v-model="queryParams.suitableLineType" placeholder="请选择适用线路类型 0全部线路参与 1指定线路参与 2指定线路不参与" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="适用产品类型 0全部产品参与 1指定产品参与 2指定产品不参与" prop="suitableProdType">
        <el-select v-model="queryParams.suitableProdType" placeholder="请选择适用产品类型 0全部产品参与 1指定产品参与 2指定产品不参与" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="每个用户领券上限，如不填则默认为1" prop="limitNum">
        <el-input v-model="queryParams.limitNum" placeholder="请输入每个用户领券上限，如不填则默认为1" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item label="过期状态" prop="overdueStatus">
        <dict-selector :type="DICT_TYPE.ECW_COUPON_OVERDUE_STATUS" v-model="queryParams.overdueStatus"/>
      </el-form-item>
      <el-form-item label="投放状态" prop="putonStatus">
        <dict-selector :type="DICT_TYPE.ECW_COUPON_PUTON_STATUS" v-model="queryParams.putonStatus"/>
      </el-form-item>
      <!-- <el-form-item label="获取方式 0=客户领取 1=平台发放" prop="getWay">
        <el-input v-model="queryParams.getWay" placeholder="请输入获取方式 0=客户领取 1=平台发放" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="订单属性(可多选) 1 普货 2重货 3泡货" prop="orderAttr">
        <el-input v-model="queryParams.orderAttr" placeholder="请输入订单属性(可多选) 1 普货 2重货 3泡货" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否发送站内信 0否  1是" prop="isSiteContent">
        <el-select v-model="queryParams.isSiteContent" placeholder="请选择是否发送站内信 0否  1是" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否控货(可多选) 0否 1是" prop="isCargoControl">
        <el-input v-model="queryParams.isCargoControl" placeholder="请输入是否控货(可多选) 0否 1是" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="单证报关(可多选) 1 我司全代 2自单代报 3混合报关" prop="documentDeclaration">
        <el-input v-model="queryParams.documentDeclaration" placeholder="请输入单证报关(可多选) 1 我司全代 2自单代报 3混合报关" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item> -->
      <el-form-item label="状态" prop="status">
        <dict-selector :type="DICT_TYPE.ECW_COUPON_STATUS" v-model="queryParams.status"/>
      </el-form-item>
      <!-- <el-form-item label="创建时间">
        <el-date-picker v-model="dateRangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @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="['ecw:coupon:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['ecw:coupon:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list" border>
      <el-table-column label="优惠券ID" align="center" prop="couponId" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="{row}">
          <dict-tag :type="DICT_TYPE.ECW_COUPON_TYPE" :value="row.type" />
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="titleZh" />
      <el-table-column label="发布人/发布时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <div>{{ scope.row.creatorName }}</div>
          <div>{{ parseTime(scope.row.createTime) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="createTime" width="180">
        <template slot-scope="{row}">
          <dict-tag :type="DICT_TYPE.ECW_IS_DRAFT" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row, true)"
                     v-hasPermi="['ecw:coupon:update']">查看</el-button>
          <el-button v-if="scope.row.status != 1" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['ecw:coupon:update']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleCopy(scope.row)"
                     v-hasPermi="['ecw:coupon:create']">复制</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['ecw:coupon:delete']">删除</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 { createCoupon, updateCoupon, deleteCoupon, getCoupon, getCouponPage, exportCouponExcel } from "@/api/ecw/coupon";

export default {
  name: "Coupon",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 优惠券信息列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeStartTime: [],
      dateRangeEndTime: [],
      dateRangeCreateTime: null,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        titleZh: null,
        titleEn: null,
        type: null,
        cashCondition: null,
        reduceAmount: null,
        conditionCurrencyId: null,
        reduceCurrencyId: null,
        afterReceiveDays: null,
        validDays: null,
        costType: null,
        discountDetailed: null,
        isSimilarSuperposition: null,
        couponIds: null,
        suitableUserType: null,
        suitableLineType: null,
        suitableProdType: null,
        limitNum: null,
        overdueStatus: null,
        putonStatus: null,
        getWay: null,
        orderAttr: null,
        isSiteContent: null,
        isCargoControl: null,
        documentDeclaration: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeStartTime, 'startTime');
      this.addBeginAndEndTime(params, this.dateRangeEndTime, 'endTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getCouponPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeStartTime = [];
      this.dateRangeEndTime = [];
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      return this.$router.push('edit')
    },
    /** 修改按钮操作 */
    handleUpdate(row, readonly) {
      const couponId = row.couponId;
      let url = 'edit?id=' + couponId
      if(readonly){
        url += '&readonly=1'
      }
      return this.$router.push(url)
    },
    handleCopy(row){
      this.$router.push('edit?action=copy&id=' + row.couponId)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const couponId = row.couponId;
      this.$modal.confirm('是否确认删除优惠券信息编号为"' + couponId + '"的数据项?').then(function() {
          return deleteCoupon(couponId);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeStartTime, 'startTime');
      this.addBeginAndEndTime(params, this.dateRangeEndTime, 'endTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm('是否确认导出所有优惠券信息数据项?').then(() => {
          this.exportLoading = true;
          return exportCouponExcel(params);
        }).then(response => {
          this.$download.excel(response, '${table.classComment}.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>