<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="medium" class="ry_form">
      <el-form-item label="批次号" prop="stockId">
        <el-input
          v-model="queryParams.stockId"
          placeholder="请输入批次号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次名称	" prop="stockName">
        <el-input
          v-model="queryParams.stockName"
          placeholder="请输入商家券批次名称	"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次状态" prop="stockState">
        <el-input
          v-model="queryParams.stockState"
          placeholder="请输入批次状态"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次类型" prop="stockType">
        <el-select v-model="queryParams.stockType" placeholder="请选择批次类型" clearable size="small">
            <el-option label="微信满减券" value="NORMAL" />
            <el-option label="微信折扣券" value="DISCOUNT" />
            <el-option label="微信换购券" value="EXCHANGE" />
            <el-option label="平台满减券" value="PNORMAL" />
            <el-option label="平台折扣券" value="PDISCOUNT" />
            <el-option label="平台体验券" value="PTRAIL" />
        </el-select>
      </el-form-item>
      <el-form-item label="发送类型" prop="sendType">
        <el-select v-model="queryParams.sendType" placeholder="发送类型" clearable size="small">
            <el-option label="自动发送" value="AUTO" />
            <el-option label="手动发送" value="MANUAL" />
            <el-option label="新用户领取" value="newUser" />
        </el-select>
      </el-form-item>

        <el-form-item label="自动触发事件" prop="eventType">
          <el-select v-model="queryParams.eventType" placeholder="请选自动触发事件" clearable size="small">
            <el-option label="用户注册成功" value="registered_successfully" />
            <el-option label="用户下单成功" value="checkout_success " />
          </el-select>
        </el-form-item>
        <el-form-item label="折扣类型" prop="discountType">
          <el-select v-model="queryParams.discountType" placeholder="请选择折扣类型" clearable size="small">
            <el-option label="按订单折扣" value="BYORDER" />
            <el-option label="按服务天数" value="BYSERVICEDAY" />
          </el-select>
        </el-form-item>

      <el-form-item label="开始时间" prop="availableBeginTime">
        <el-date-picker
            clearable
            size="small"
            v-model="queryParams.availableBeginTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="availableEndTime">
        <el-date-picker
            clearable
            size="small"
            v-model="queryParams.availableEndTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item class="flex_one tr">
        <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="['mall:wechatStock: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="['mall:wechatStock: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="['mall:wechatStock:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['mall:wechatStock:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wechatStockList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商家券批次名称 " align="center" prop="stockName" v-if="columns[0].visible"/>
      <el-table-column label="批次号" align="center" prop="stockId" v-if="columns[16].visible"/>
      <el-table-column label="批次状态" align="center" prop="stockState" v-if="columns[15].visible"/>
      <el-table-column label="批次备注" align="center" prop="comment" v-if="columns[1].visible"/>
      <el-table-column label="适用商品范围" align="center" prop="goodsName" v-if="columns[2].visible"/>
      <el-table-column label="批次类型" align="center" prop="stockType" v-if="columns[3].visible"/>
      <el-table-column label="开始时间" align="center" prop="availableBeginTime" width="180" v-if="columns[4].visible">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.availableBeginTime, '')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="availableEndTime" width="180" v-if="columns[5].visible">
        <template slot-scope="scope">
            <span>{{ parseTime(scope.row.availableEndTime, '')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="领取后N天开始生效" align="center" prop="availableDayAfterReceive" v-if="columns[6].visible"/>
      <el-table-column label="优惠金额" align="center" prop="discountAmount" v-if="columns[7].visible"/>
      <el-table-column label="消费门槛" align="center" prop="transactionMinimum" v-if="columns[8].visible"/>
      <el-table-column label="折扣比例" align="center" prop="discountPercent" v-if="columns[9].visible"/>
      <el-table-column label="核销方式" align="center" prop="useMethod" v-if="columns[10].visible"/>
      <el-table-column label="商户请求单号" align="center" prop="outRequestNo" v-if="columns[11].visible"/>
      <el-table-column label="批次最大发放个数" align="center" prop="maxCoupons" v-if="columns[12].visible"/>
      <el-table-column label="用户最大可领个数" align="center" prop="maxCouponsPerUser" v-if="columns[13].visible"/>
      <el-table-column label="批次已发放个数" align="center" prop="giveNum" v-if="columns[13].visible"/>
      <el-table-column label="券code模式" align="center" prop="couponCodeMode" v-if="columns[14].visible"/>

        <el-table-column label="发送类型" align="center" prop="sendType">
            <template slot-scope="scope">
                <span v-if="scope.row.sendType == 'AUTO'">自动发送</span>
                <span v-if="scope.row.sendType == 'MANUAL'">手动发送</span>
                <span v-if="scope.row.sendType == 'newUser'">新用户领取</span>
            </template>
        </el-table-column>
      <el-table-column label="触发事件" align="center" prop="eventType" />
<!--      <el-table-column label="折扣类型" align="center" prop="discountType" />-->

      <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="['mall:wechatStock:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:wechatStock: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="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="108px" inline class="dialog-form-two">
        <el-form-item label="批次名称	" prop="stockName">
          <el-input v-model="form.stockName" placeholder="请输入商家券批次名称	" />
        </el-form-item>
        <el-form-item label="批次备注" prop="comment">
          <el-input v-model="form.comment" placeholder="请输入批次备注" />
        </el-form-item>
        <el-form-item label="适用商品范围" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入适用商品范围" />
        </el-form-item>
        <el-form-item label="批次类型" prop="stockType">
          <el-select v-model="form.stockType" placeholder="请选择批次类型">
<!--            <el-option label="微信满减券" value="NORMAL" />-->
<!--            <el-option label="微信折扣券" value="DISCOUNT" />-->
<!--            <el-option label="微信换购券" value="EXCHANGE" />-->
            <el-option label="平台满减券" value="PNORMAL" />
            <el-option label="平台折扣券" value="PDISCOUNT" />
            <el-option label="平台体验券" value="PTRAIL" />
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="availableBeginTime">
          <el-date-picker clearable size="small"
                        v-model="form.availableBeginTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="availableEndTime">
          <el-date-picker clearable size="small"
                        v-model="form.availableEndTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领取后N天开始生效" prop="availableDayAfterReceive">
          <el-input v-model="form.availableDayAfterReceive" placeholder="请输入领取后N天开始生效" />
        </el-form-item>
        <el-form-item label="优惠金额" prop="discountAmount">
          <el-input v-model="form.discountAmount" placeholder="请输入固定面额满减券-优惠金额" />
        </el-form-item>
        <el-form-item label="消费门槛" prop="transactionMinimum">
          <el-input v-model="form.transactionMinimum" placeholder="请输入消费门槛" />
        </el-form-item>
        <el-form-item label="折扣比例" prop="discountPercent">
          <el-input v-model="form.discountPercent" placeholder="请输入折扣比例" />
        </el-form-item>
        <el-form-item label="批次最大发放个数" prop="maxCoupons">
          <el-input v-model="form.maxCoupons" placeholder="请输入批次最大发放个数" />
        </el-form-item>
        <el-form-item label="用户最大可领个数" prop="maxCouponsPerUser">
          <el-input v-model="form.maxCouponsPerUser" placeholder="请输入用户最大可领个数" />
        </el-form-item>

        <el-form-item label="发放类型" prop="sendType">
          <el-select v-model="form.sendType" placeholder="请选择发放类型">
            <el-option label="自动发送" value="AUTO" />
            <el-option label="手动发送" value="MANUAL" />
            <el-option label="新用户领取" value="newUser" />
          </el-select>
        </el-form-item>
        <el-form-item label="自动触发事件" prop="eventType">
          <el-select v-model="form.eventType" placeholder="请选自动触发事件">
            <el-option label="用户注册成功" value="registered_successfully" />
            <el-option label="用户下单成功" value="checkout_success " />
          </el-select>
        </el-form-item>

          <el-form-item label="海报" prop="couponPoster">
            <oss-image-upload v-model="form.couponPoster" :limit="1"></oss-image-upload>
          </el-form-item>

        <el-form-item label="折扣类型" prop="discountType">
          <el-select v-model="form.discountType" placeholder="请选择折扣类型">
            <el-option label="按订单折扣" value="BYORDER" />
            <el-option label="按服务天数" value="BYSERVICEDAY" />
          </el-select>
        </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 { listWechatStock, getWechatStock, delWechatStock, addWechatStock, updateWechatStock, exportWechatStock } from "@/api/marketing/wechatStock";

export default {
  name: "WechatStock",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微信商家券表格数据
      wechatStockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderByColumn: "create_time",
        isAsc: "desc",
        stockName: null,
        comment: null,
        goodsName: null,
        stockType: null,
        availableBeginTime: null,
        availableEndTime: null,
        availableDayAfterReceive: null,
        discountAmount: null,
        transactionMinimum: null,
        discountPercent: null,
        useMethod: null,
        outRequestNo: null,
        maxCoupons: null,
        maxCouponsPerUser: null,
        couponCodeMode: null,
        stockState: null,
        stockId: null,
        sendType: null,
        discountType: null,
        eventType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        stockName: [
          { required: true, message: "批次名称	不能为空", trigger: "blur" }
        ],
        goodsName: [
          { required: true, message: "适用商品范围不能为空", trigger: "blur" }
        ],
        stockType: [
          { required: true, message: "批次类型不能为空", trigger: "change" }
        ],
        availableBeginTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        availableEndTime: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        maxCoupons: [
          { required: true, message: "批次最大发放个数不能为空", trigger: "blur" }
        ],
        maxCouponsPerUser: [
          { required: true, message: "用户最大可领个数不能为空", trigger: "blur" }
        ],
        availableDayAfterReceive: [
            { required: true, message: "领取后N天开始生效不能为空", trigger: "blur" }
        ],
      },
      columns: [
            { key: 1, label: "批次名称	", visible:  true  },
            { key: 2, label: "批次备注", visible:  true  },
            { key: 3, label: "适用商品范围", visible:  true  },
            { key: 4, label: "批次类型", visible:  true  },
            { key: 5, label: "开始时间", visible:  true  },
            { key: 6, label: "结束时间", visible:  true  },
            { key: 7, label: "领取后N天开始生效", visible:  true  },
            { key: 8, label: "优惠金额", visible:  true  },
            { key: 9, label: "消费门槛", visible:  true  },
            { key: 10, label: "折扣比例", visible:  true  },
            { key: 11, label: "核销方式", visible:  false  },
            { key: 12, label: "商户请求单号", visible:  false  },
            { key: 13, label: "批次最大发放个数", visible:  true  },
            { key: 14, label: "用户最大可领个数", visible:  true  },
            { key: 15, label: "券code模式。", visible:  false  },
            { key: 16, label: "批次状态", visible:  true  },
            { key: 17, label: "批次号", visible:  true  },
                         ],
      showMoreCondition: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询微信商家券列表 */
    getList() {
      this.loading = true;
      listWechatStock(this.queryParams).then(response => {
        this.wechatStockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stockName: null,
        comment: null,
        goodsName: null,
        stockType: null,
        availableBeginTime: null,
        availableEndTime: null,
        availableDayAfterReceive: null,
        discountAmount: null,
        transactionMinimum: null,
        discountPercent: null,
        useMethod: null,
        outRequestNo: null,
        maxCoupons: null,
        maxCouponsPerUser: null,
        couponCodeMode: null,
        stockState: null,
        stockId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        sendType: null,
        discountType: null,
        eventType: null,
        updateTime: null,
        sendType: null,
        discountType: null,
        eventType: null,
        couponPoster : 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
      getWechatStock(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) {
            updateWechatStock(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWechatStock(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 delWechatStock(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有微信商家券数据项？').then(() => {
        this.exportLoading = true;
        return exportWechatStock(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
