<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="所属连锁店id" prop="chainStoreId">
        <el-input
          v-model="queryParams.chainStoreId"
          placeholder="请输入所属连锁店id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属店铺id" prop="storeId">
        <el-input
          v-model="queryParams.storeId"
          placeholder="请输入所属店铺id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动开始时间" prop="activeStartTime">
        <el-date-picker clearable
                        v-model="queryParams.activeStartTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择活动开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="活动结束时间" prop="activeEndTime">
        <el-date-picker clearable
                        v-model="queryParams.activeEndTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择活动结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="有礼奖励 0：优惠券 1：兑换券 2：成长值 3：积分" prop="courtesyReward">
        <el-input
          v-model="queryParams.courtesyReward"
          placeholder="请输入有礼奖励 0：优惠券 1：兑换券 2：成长值 3：积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否在线 0：在线 1： 下线" prop="isonline">
        <el-input
          v-model="queryParams.isonline"
          placeholder="请输入是否在线 0：在线 1： 下线"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动剩余天数" prop="remainderTotal">
        <el-input
          v-model="queryParams.remainderTotal"
          placeholder="请输入活动剩余天数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="赠送积分" prop="points">
        <el-input
          v-model="queryParams.points"
          placeholder="请输入赠送积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="赠送成长值" prop="growthValue">
        <el-input
          v-model="queryParams.growthValue"
          placeholder="请输入赠送成长值"
          clearable
          @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">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business/marketingActivity:activeNewlyweds: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="['business/marketingActivity:activeNewlyweds: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="['business/marketingActivity:activeNewlyweds: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="['business/marketingActivity:activeNewlyweds:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activeNewlywedsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="所属连锁店id" align="center" prop="chainStoreId" />
      <el-table-column label="所属店铺id" align="center" prop="storeId" />
      <el-table-column label="活动名称" align="center" prop="name" />
      <el-table-column label="活动开始时间" align="center" prop="activeStartTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activeStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动结束时间" align="center" prop="activeEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activeEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有礼奖励 0：优惠券 1：兑换券 2：成长值 3：积分" align="center" prop="courtesyReward" />
      <el-table-column label="是否在线 0：在线 1： 下线" align="center" prop="isonline" />
      <el-table-column label="活动状态 0：进行中 1： 待生效 2：已结束" align="center" prop="status" />
      <el-table-column label="活动剩余天数" align="center" prop="remainderTotal" />
      <el-table-column label="赠送积分" align="center" prop="points" />
      <el-table-column label="赠送成长值" align="center" prop="growthValue" />
      <el-table-column label="自定义规则" align="center" prop="remark" />
      <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="['business/marketingActivity:activeNewlyweds:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business/marketingActivity:activeNewlyweds: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="所属连锁店id" prop="chainStoreId">
          <el-input v-model="form.chainStoreId" placeholder="请输入所属连锁店id" />
        </el-form-item>
        <el-form-item label="所属店铺id" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入所属店铺id" />
        </el-form-item>
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="活动开始时间" prop="activeStartTime">
          <el-date-picker clearable
                          v-model="form.activeStartTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择活动开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动结束时间" prop="activeEndTime">
          <el-date-picker clearable
                          v-model="form.activeEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择活动结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有礼奖励 0：优惠券 1：兑换券 2：成长值 3：积分" prop="courtesyReward">
          <el-input v-model="form.courtesyReward" placeholder="请输入有礼奖励 0：优惠券 1：兑换券 2：成长值 3：积分" />
        </el-form-item>
        <el-form-item label="是否在线 0：在线 1： 下线" prop="isonline">
          <el-input v-model="form.isonline" placeholder="请输入是否在线 0：在线 1： 下线" />
        </el-form-item>
        <el-form-item label="活动剩余天数" prop="remainderTotal">
          <el-input v-model="form.remainderTotal" placeholder="请输入活动剩余天数" />
        </el-form-item>
        <el-form-item label="赠送积分" prop="points">
          <el-input v-model="form.points" placeholder="请输入赠送积分" />
        </el-form-item>
        <el-form-item label="赠送成长值" prop="growthValue">
          <el-input v-model="form.growthValue" placeholder="请输入赠送成长值" />
        </el-form-item>
        <el-form-item label="自定义规则" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入自定义规则" />
        </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 { listActiveNewlyweds, getActiveNewlyweds, delActiveNewlyweds, addActiveNewlyweds, updateActiveNewlyweds } from "@/api/marketingActivity/activeNewlyweds";

export default {
  name: "ActiveNewlyweds",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新人有礼活动表格数据
      activeNewlywedsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        chainStoreId: null,
        storeId: null,
        name: null,
        activeStartTime: null,
        activeEndTime: null,
        courtesyReward: null,
        isonline: null,
        status: null,
        remainderTotal: null,
        points: null,
        growthValue: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询新人有礼活动列表 */
    getList() {
      this.loading = true;
      listActiveNewlyweds(this.queryParams).then(response => {
        this.activeNewlywedsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        chainStoreId: null,
        storeId: null,
        name: null,
        activeStartTime: null,
        activeEndTime: null,
        courtesyReward: null,
        isonline: null,
        status: null,
        remainderTotal: null,
        points: null,
        growthValue: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: 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
      getActiveNewlyweds(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) {
            updateActiveNewlyweds(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActiveNewlyweds(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 delActiveNewlyweds(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/marketingActivity/activeNewlyweds/export', {
        ...this.queryParams
      }, `activeNewlyweds_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
