<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="发送者用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入发送者用户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="红包唯一编号" prop="redPacketNo">
        <el-input
          v-model="queryParams.redPacketNo"
          placeholder="请输入红包唯一编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="红包总积分" prop="totalAmount">
        <el-input
          v-model="queryParams.totalAmount"
          placeholder="请输入红包总积分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="红包总个数" prop="totalCount">
        <el-input
          v-model="queryParams.totalCount"
          placeholder="请输入红包总个数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="剩余积分" prop="remainAmount">
        <el-input
          v-model="queryParams.remainAmount"
          placeholder="请输入剩余积分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="剩余个数" prop="remainCount">
        <el-input
          v-model="queryParams.remainCount"
          placeholder="请输入剩余个数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="过期时间" prop="expireTime">
        <el-date-picker clearable
          v-model="queryParams.expireTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择过期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="领取密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入领取密码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="红包标题/祝福语" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入红包标题/祝福语"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否公开(0:私密,1:公开)" prop="isPublic">
        <el-select v-model="queryParams.isPublic" placeholder="请选择是否公开(0:私密,1:公开)" clearable>
          <el-option
            v-for="dict in sys_general_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发送渠道(1:聊天,2:群组,3:活动)" prop="channel">
        <el-input
          v-model="queryParams.channel"
          placeholder="请输入发送渠道(1:聊天,2:群组,3:活动)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button 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="Plus"
          @click="handleAdd"
          v-hasPermi="['system:pointsRedPacket:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:pointsRedPacket:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:pointsRedPacket:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:pointsRedPacket:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pointsRedPacketList" @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="userId" />
      <el-table-column label="红包唯一编号" align="center" prop="redPacketNo" />
      <el-table-column label="红包总积分" align="center" prop="totalAmount" />
      <el-table-column label="红包总个数" align="center" prop="totalCount" />
      <el-table-column label="剩余积分" align="center" prop="remainAmount" />
      <el-table-column label="剩余个数" align="center" prop="remainCount" />
      <el-table-column label="红包类型(1:普通红包,2:拼手气红包,3:定额红包)" align="center" prop="packetType" />
      <el-table-column label="状态(1:进行中,2:已领完,3:已过期,4:已撤销)" align="center" prop="status" />
      <el-table-column label="过期时间" align="center" prop="expireTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expireTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="领取密码" align="center" prop="password" />
      <el-table-column label="红包标题/祝福语" align="center" prop="title" />
      <el-table-column label="红包描述" align="center" prop="description" />
      <el-table-column label="是否公开(0:私密,1:公开)" align="center" prop="isPublic">
        <template #default="scope">
          <dict-tag :options="sys_general_yes_no" :value="scope.row.isPublic"/>
        </template>
      </el-table-column>
      <el-table-column label="发送渠道(1:聊天,2:群组,3:活动)" align="center" prop="channel" />
      <el-table-column label="接收者类型(1:指定用户,2:所有人,3:新用户,4:特定群组)" align="center" prop="receiverType" />
      <el-table-column label="接收者ID列表(JSON数组)" align="center" prop="receiverIds" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:pointsRedPacket:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:pointsRedPacket:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改积分红包主对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="pointsRedPacketRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发送者用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入发送者用户ID" />
        </el-form-item>
        <el-form-item label="红包唯一编号" prop="redPacketNo">
          <el-input v-model="form.redPacketNo" placeholder="请输入红包唯一编号" />
        </el-form-item>
        <el-form-item label="红包总积分" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入红包总积分" />
        </el-form-item>
        <el-form-item label="红包总个数" prop="totalCount">
          <el-input v-model="form.totalCount" placeholder="请输入红包总个数" />
        </el-form-item>
        <el-form-item label="剩余积分" prop="remainAmount">
          <el-input v-model="form.remainAmount" placeholder="请输入剩余积分" />
        </el-form-item>
        <el-form-item label="剩余个数" prop="remainCount">
          <el-input v-model="form.remainCount" placeholder="请输入剩余个数" />
        </el-form-item>
        <el-form-item label="过期时间" prop="expireTime">
          <el-date-picker clearable
            v-model="form.expireTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领取密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入领取密码" />
        </el-form-item>
        <el-form-item label="红包标题/祝福语" prop="title">
          <el-input v-model="form.title" placeholder="请输入红包标题/祝福语" />
        </el-form-item>
        <el-form-item label="红包描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否公开(0:私密,1:公开)" prop="isPublic">
          <el-select v-model="form.isPublic" placeholder="请选择是否公开(0:私密,1:公开)">
            <el-option
              v-for="dict in sys_general_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发送渠道(1:聊天,2:群组,3:活动)" prop="channel">
          <el-input v-model="form.channel" placeholder="请输入发送渠道(1:聊天,2:群组,3:活动)" />
        </el-form-item>
        <el-form-item label="接收者ID列表(JSON数组)" prop="receiverIds">
          <el-input v-model="form.receiverIds" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PointsRedPacket">
import { listPointsRedPacket, getPointsRedPacket, delPointsRedPacket, addPointsRedPacket, updatePointsRedPacket } from "@/api/system/pointsRedPacket";

const { proxy } = getCurrentInstance();
const { sys_general_yes_no } = proxy.useDict('sys_general_yes_no');

const pointsRedPacketList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    redPacketNo: null,
    totalAmount: null,
    totalCount: null,
    remainAmount: null,
    remainCount: null,
    packetType: null,
    status: null,
    expireTime: null,
    password: null,
    title: null,
    description: null,
    isPublic: null,
    channel: null,
    receiverType: null,
    receiverIds: null,
  },
  rules: {
    userId: [
      { required: true, message: "发送者用户ID不能为空", trigger: "blur" }
    ],
    redPacketNo: [
      { required: true, message: "红包唯一编号不能为空", trigger: "blur" }
    ],
    totalAmount: [
      { required: true, message: "红包总积分不能为空", trigger: "blur" }
    ],
    totalCount: [
      { required: true, message: "红包总个数不能为空", trigger: "blur" }
    ],
    remainAmount: [
      { required: true, message: "剩余积分不能为空", trigger: "blur" }
    ],
    remainCount: [
      { required: true, message: "剩余个数不能为空", trigger: "blur" }
    ],
    packetType: [
      { required: true, message: "红包类型(1:普通红包,2:拼手气红包,3:定额红包)不能为空", trigger: "change" }
    ],
    status: [
      { required: true, message: "状态(1:进行中,2:已领完,3:已过期,4:已撤销)不能为空", trigger: "change" }
    ],
    expireTime: [
      { required: true, message: "过期时间不能为空", trigger: "blur" }
    ],
    isPublic: [
      { required: true, message: "是否公开(0:私密,1:公开)不能为空", trigger: "change" }
    ],
    channel: [
      { required: true, message: "发送渠道(1:聊天,2:群组,3:活动)不能为空", trigger: "blur" }
    ],
    receiverType: [
      { required: true, message: "接收者类型(1:指定用户,2:所有人,3:新用户,4:特定群组)不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询积分红包主列表 */
function getList() {
  loading.value = true;
  listPointsRedPacket(queryParams.value).then(response => {
    pointsRedPacketList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    redPacketNo: null,
    totalAmount: null,
    totalCount: null,
    remainAmount: null,
    remainCount: null,
    packetType: null,
    status: null,
    expireTime: null,
    password: null,
    title: null,
    description: null,
    isPublic: null,
    channel: null,
    receiverType: null,
    receiverIds: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("pointsRedPacketRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加积分红包主";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getPointsRedPacket(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改积分红包主";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["pointsRedPacketRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePointsRedPacket(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addPointsRedPacket(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除积分红包主编号为"' + _ids + '"的数据项？').then(function() {
    return delPointsRedPacket(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/pointsRedPacket/export', {
    ...queryParams.value
  }, `pointsRedPacket_${new Date().getTime()}.xlsx`)
}

getList();
</script>
