<template>
    <div class="app-container">
        <div class="background-color-fff padding-20 border-radius-8px margin-bottom-20">
            <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="changeType">
                    <el-select v-model="queryParams.changeType" placeholder="请选择类型">
                        <el-option label="拼单获得" :value="1"></el-option>
                        <el-option label="商品消费" :value="2"></el-option>
                        <el-option label="订单退款" :value="3"></el-option>
                        <el-option label="系统赠送" :value="4"></el-option>
                        <el-option label="系统扣除" :value="5"></el-option>
                        <el-option label="签到" :value="6"></el-option>
                        <el-option label="转出" :value="7"></el-option>
                        <el-option label="提现冻结" :value="8"></el-option>
                        <el-option label="提现解冻" :value="9"></el-option>
                        <el-option label="提现成功扣除" :value="10"></el-option>
                        <el-option label="发红包支出" :value="11"></el-option>
                        <el-option label="抢红包收入" :value="12"></el-option>
                        <el-option label="红包退回" :value="13"></el-option>
                        <el-option label="直播活动冻结" :value="14"></el-option>
                        <el-option label="直播活动成功扣除" :value="15"></el-option>
                        <el-option label="直播活动冻结退回" :value="16"></el-option>
                        <el-option label="参与直播活动" :value="17"></el-option>
                        <el-option label="后台充值" :value="18"></el-option>
                    </el-select>
                </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>
        </div>

        <div class="background-color-fff padding-20 border-radius-8px margin-bottom-20">
            <el-row :gutter="10" class="mb8">
                <!-- <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="Plus"
                        @click="handleAdd"
                        v-hasPermi="['system:logPoints:add']"
                    >
                        新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="Edit"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['system:logPoints:edit']"
                    >
                        修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        plain
                        icon="Delete"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['system:logPoints:remove']"
                    >
                        删除
                    </el-button>
                </el-col> -->
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="Download"
                        @click="handleExport"
                        v-hasPermi="['system:logPoints:export']"
                    >
                        导出
                    </el-button>
                </el-col>
                <right-toolbar
                    v-model:showSearch="showSearch"
                    @queryTable="getList"
                ></right-toolbar>
            </el-row>
        </div>

        <div class="background-color-fff padding-20 border-radius-8px margin-bottom-20">
            <el-table
                v-loading="loading"
                :data="logPointsList"
                @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="changeType">
                    <template #default="scope">
                        <el-tag type="primary">{{ filterType(scope.row.changeType) }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="变更数量" align="center" prop="changeAmount" />
                <el-table-column label="变更前积分" align="center" prop="beforePoints" />
                <el-table-column label="变更后积分" align="center" prop="afterPoints" />
                <!-- <el-table-column label="关联订单ID" align="center" prop="orderId" /> -->
                <!-- <el-table-column label="关联拼单ID" align="center" prop="groupId" /> -->
                <el-table-column label="描述" align="center" prop="description" />
                <el-table-column label="状态" align="center" prop="status">
                    <template #default="scope">
                        <el-tag v-if="scope.row.status === 1" type="success">成功</el-tag>
                        <el-tag v-else-if="scope.row.status === 2" type="danger">失败</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" align="center" prop="createTime" />
                <!-- <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:logPoints:edit']"
                        >
                            修改
                        </el-button>
                        <el-button
                            link
                            type="primary"
                            icon="Delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['system:logPoints: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"
            />
        </div>

        <!-- 添加或修改积分流水对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="logPointsRef" :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="changeAmount">
                    <el-input v-model="form.changeAmount" placeholder="请输入变更数量" />
                </el-form-item>
                <el-form-item label="变更前积分" prop="beforePoints">
                    <el-input v-model="form.beforePoints" placeholder="请输入变更前积分" />
                </el-form-item>
                <el-form-item label="变更后积分" prop="afterPoints">
                    <el-input v-model="form.afterPoints" placeholder="请输入变更后积分" />
                </el-form-item>
                <el-form-item label="关联订单ID" prop="orderId">
                    <el-input v-model="form.orderId" placeholder="请输入关联订单ID" />
                </el-form-item>
                <el-form-item label="关联拼单ID" prop="groupId">
                    <el-input v-model="form.groupId" placeholder="请输入关联拼单ID" />
                </el-form-item>
                <el-form-item label="描述" prop="description">
                    <el-input v-model="form.description" 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="LogPoints">
import {
    listLogPoints,
    getLogPoints,
    delLogPoints,
    addLogPoints,
    updateLogPoints,
} from '@/api/system/logPoints';

const { proxy } = getCurrentInstance();

const logPointsList = 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,
        changeType: null,
        changeAmount: null,
        beforePoints: null,
        afterPoints: null,
        orderId: null,
        groupId: null,
        description: null,
        status: null,
    },
    rules: {
        userId: [{ required: true, message: '用户ID不能为空', trigger: 'blur' }],
        changeType: [
            {
                required: true,
                message: '变更类型(1:拼单获得,2:消费,3:退款,4:系统赠送,5:系统扣除)不能为空',
                trigger: 'change',
            },
        ],
        changeAmount: [{ required: true, message: '变更数量不能为空', trigger: 'blur' }],
        beforePoints: [{ required: true, message: '变更前积分不能为空', trigger: 'blur' }],
        afterPoints: [{ required: true, message: '变更后积分不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '描述不能为空', trigger: 'blur' }],
    },
});

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

/** 查询积分流水列表 */
function getList() {
    loading.value = true;
    listLogPoints(queryParams.value).then(response => {
        logPointsList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

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

// 表单重置
function reset() {
    form.value = {
        id: null,
        userId: null,
        changeType: null,
        changeAmount: null,
        beforePoints: null,
        afterPoints: null,
        orderId: null,
        groupId: null,
        description: null,
        status: null,
        createTime: null,
    };
    proxy.resetForm('logPointsRef');
}

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

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

function filterType(type) {
    let data = {
        1: '拼单获得',
        2: '商品消费',
        3: '订单退款',
        4: '系统赠送',
        5: '系统扣除',
        6: '签到',
        7: '转出',
        8: '提现冻结',
        9: '提现解冻',
        10: '提现成功扣除',
        11: '发红包支出',
        12: '抢红包收入',
        13: '红包退回',
        14: '直播活动冻结',
        15: '直播活动成功扣除',
        16: '直播活动冻结退回',
        17: '参与直播活动',
        18: '后台充值',
    };
    return data[type];
}

// 多选框选中数据
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;
    getLogPoints(_id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = '修改积分流水';
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs['logPointsRef'].validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateLogPoints(form.value).then(response => {
                    proxy.$modal.msgSuccess('修改成功');
                    open.value = false;
                    getList();
                });
            } else {
                addLogPoints(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 delLogPoints(_ids);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
}

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

getList();
</script>
