<template>
    <div class="app-container">
        <div class="page-container">
            <el-form :model="queryParams" :inline="true" label-width="80px">
                <el-form-item label="姓名">
                    <el-input v-model="queryParams.nickName" placeholder="请输入" style="width: 220px;"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleQuery">查询</el-button>
                    <el-button @click="handleReset">重置</el-button>
                </el-form-item>
            </el-form>
            <el-row class="mb10">
                <el-col :span="2">
                    <el-button type="primary" @click="handleAdd">积分规则设置</el-button>
                </el-col>
            </el-row>
            <el-table :data="tableList">
                <el-table-column label="序号" width="55" type="index" align="center"></el-table-column>
                <el-table-column label="用户姓名" prop="nickName" align="center"></el-table-column>
                <el-table-column label="用户名" prop="userName" align="center"></el-table-column>
                <el-table-column label="所在部门" prop="deptName" align="center"></el-table-column>
                <el-table-column label="会员等级" prop="gradeName" align="center"></el-table-column>
                <el-table-column label="累计积分" prop="accumulatedPoints" align="center"></el-table-column>
                <el-table-column label="可用积分" prop="currentPoints" align="center"></el-table-column>
                <el-table-column label="操作" align="center" width="200">
                    <template #default="{ row }">
                        <el-button type="text" @click="handleAddPoints(row)">增加</el-button>
                        <el-button type="text" @click="handleDecreasePoints(row)">扣减</el-button>
                        <el-button type="text" @click="handleExchange(row)">兑换</el-button>
                        <el-button type="text" @click="handleDetail(row)">查看明细</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" />
        </div>
        <!-- 积分兑换 -->
        <exchange-dialog ref="exchangeDialogRef" @handleOk="handleReset"></exchange-dialog>
        <!-- 积分明细 -->
        <detail-dialog ref="detailDialogRef"></detail-dialog>
        <!-- 积分规则设置 -->
         <points-rules-dialog ref="pointsRulesDialogRef"></points-rules-dialog>
    </div>
</template>

<script>
import { listData, delData, addData, editData } from '@/api/pointsManage';
import ExchangeDialog from './ExchangeDialog.vue';
import DetailDialog from './DetailDialog.vue';
import PointsRulesDialog from './PointsRulesDialog.vue';
export default {
    name: 'PointsMall',
    data() {
        return {
            queryParams: {
                pageNum: 1,
                pageSize: 10
            },
            tableList: [],
            total: 0
        }
    },
    components: {
        ExchangeDialog,
        DetailDialog,
        PointsRulesDialog
    },
    created() {

    },
    mounted() {
        this.getList()
    },
    methods: {
        // 列表数据
        getList() {
            listData(this.queryParams).then(res => {
                console.log(res)
                this.tableList = res.rows;
                this.total = res.total
            })
        },
        //积分规则设置
        handleAdd() {
            this.$refs.pointsRulesDialogRef.openModal()
        },
        // 增加积分
        handleAddPoints(row) {
            this.$prompt('增加积分', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^[1-9]\d*$/,
                inputErrorMessage: '增加积分需为大于0的正整数'
            }).then(({ value }) => {
                let data = {
                    userName: row.userName,
                    incrOrDecrPoints: value
                }
                addData(data).then(res => {
                    if (res.code == 200) {
                        this.$modal.msgSuccess("增加积分成功");
                        this.getList()
                    } else {
                        this.$modal.msgError(res.msg || "增加积分失败");
                    }
                })
            }).catch(() => {

            });
        },
        // 扣减积分
        handleDecreasePoints(row) {
            this.$prompt('扣减积分', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /^[1-9]\d*$/,
                inputErrorMessage: '扣减积分需为大于0的正整数'
            }).then(({ value }) => {
                let data = {
                    userName: row.userName,
                    incrOrDecrPoints: value
                }
                editData(data).then(res => {
                    if (res.code == 200) {
                        this.$modal.msgSuccess("扣减积分成功");
                        this.getList()
                    } else {
                        this.$modal.msgError(res.msg || "扣减积分失败");
                    }
                })
            }).catch(() => {

            });
        },
        // 兑换
        handleExchange(row) {
            this.$refs.exchangeDialogRef.openModal(row)
        },
        // 积分明细
        handleDetail(row) {
            this.$refs.detailDialogRef.openModal(row)
        },
        // 删除
        handleDelete(row) {
            this.$modal.confirm('确认要删除商品名称为：' + row.name + '的数据吗？').then(function () {
                return delData(row.id);
            }).then(() => {
                this.$modal.msgSuccess("删除成功");
                this.getList()
            })
        },
        // 查询
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList()
        },
        // 重置
        handleReset() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10
            }
            this.getList()
        }
    }
}
</script>

<style lang="scss" scoped>
.app-container {
    background-color: rgba(241, 242, 247, 1);
    height: calc(100vh - 50px);

    .page-container {
        background-color: white;
        height: 100%;
        border-radius: 6px;
        padding: 20px 30px;
        box-sizing: border-box;
        overflow: auto;
    }
}
</style>
