<template>
    <div class="container">

        <div class="pageTitle">
            我的积分
        </div>

        <div class="point">
            <div class="point-item">
                <div class="title">
                    可用积分
                </div>
                <div class="val" style="color:#0B9EC0">
                    {{userInfo.moneyPoint}}
                </div>
            </div>
            <div class="point-item">
                <div class="title">
                    将要过期的积分
                </div>
                <div class="val">
                    {{userInfo.moneyPoint}}
                </div>
            </div>
            <div class="point-item">
                <el-button type="warning" style="width:248px;" disabled plain round>积分兑换</el-button>
            </div>
        </div>

        <el-table class="pointTable" :data="tableData" row-key="id" ref="table" style="margin-top:10px">

            <el-table-column prop="orderId" label="来源/用途" min-width="320" align="center">
            </el-table-column>

            <el-table-column prop="money" label="积分变化" min-width="100" align="center">
            </el-table-column>

            <el-table-column prop="createTime" label="日期" min-width="200" align="center">
            </el-table-column>

            <el-table-column prop="typeFlag" label="备注" min-width="100"  align="center">
            </el-table-column>

        </el-table>
        <div class="pagination-wrapper">

            <el-pagination v-if="totalNumber>pageSize" background @current-change="currentPageChange" :page-size="pageSize" layout="total, prev, pager, next" :total="totalNumber">
            </el-pagination>
        </div>

        <el-dialog title="查看图片" :visible.sync="imageDialogVisible" size="small">
            <img width="100%" :src="imgSrc" alt="">
        </el-dialog>

    </div>
</template>
<script>
import { mapState } from 'vuex'
import { format } from 'date-fns'
import zh from 'date-fns/locale/zh_cn'

export default {
    created() {
        this.getData();
    },
    data() {
        return {
            totalNumber: 0,
            totalPage: 10,
            currentPage: 1,
            pageSize: 5,
            tableData: [],
            filter1: '',
            filter2: '',
            tableColumns: [
                {
                    label: '用户',
                    value: 'userId',
                    show: true
                },
                {
                    label: '订单',
                    value: 'orderId',
                    show: true
                },
                {
                    label: '变化数量',
                    value: 'money',
                    show: true
                },
                {
                    label: '剩余金额',
                    value: 'balance',
                    show: true
                },
                {
                    label: '类型',
                    value: 'typeFlag',
                    show: true
                },
                {
                    label: '备注',
                    value: 'remark',
                    show: true
                },
            ],
            multipleMode: false,
            showAdvancedQueryDialog: false,
            advancedQueryFields: [],
            showTableSortDialog: false,
            tableSortFields: [],
            searchMethods: ['=', '!=', '>', '>=', '<', '<=', 'like'],
            advancedQueryColumns: [
                {
                    label: '用户',
                    value: 'user_id'
                },
                {
                    label: '订单',
                    value: 'order_id'
                },
                {
                    label: '变化数量',
                    value: 'money'
                },
                {
                    label: '剩余金额',
                    value: 'balance'
                },
                {
                    label: '类型',
                    value: 'type_flag'
                },
                {
                    label: '备注',
                    value: 'remark'
                },
            ],
            advancedQuerySearchKey: '',
            orderByStr: '',
            imgSrc: '',
            imageDialogVisible: false,
        }
    },
    computed: {
        ...mapState(['userInfo']),
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        pageSizeChange(size) {
            this.currentPage = 1;
            this.pageSize = size;
            this.getData();
        },
        currentPageChange(page) {
            this.currentPage = page;
            this.getData();
        },
        getData() {

            var data = {
                currentPage: this.currentPage,
                pageNumber: this.pageSize,
                userId: this.userInfo.id
            }



            this.$http.get({
                url: '/memberPoint/page',
                data: data
            }).then(res => {
                if (res.success) {
                    this.totalNumber = res.data.page.totalNumber;
                    this.tableData = res.data.pp;
                }
            })
        },
        isColumnShow(column) {
            var row = this.tableColumns.find(i => i.value === column);
            return row ? row.show : false;
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        editRow(row) {
            this.$router.push({
                path: '/memberPoint',
                query: {
                    id: row.id,
                    column: this.$route.query.column,
                }
            })
        },
        operation1() {
            this.$notify({
                title: '提示',
                message: this.selection
            });
        },
        operation2() {
            this.$message('操作2');
        },
        addField() {
            this.advancedQueryFields.push({
                link: 'AND',
                name: '',
                searchMethod: '=',
                value: '',
            });
        },
        removeField(i) {
            if (this.advancedQueryFields.length > 0) {
                this.advancedQueryFields.splice(i, 1);
            }
        },
        advancedQuery() {

            this.advancedQuerySearchKey = '';

            if (this.advancedQueryFields.length > 0) {

                var templist = [];

                this.advancedQueryFields.forEach(item => {
                    if (item.link && item.name && item.searchMethod && item.value) {
                        var tempItem = item.link + '_,' + item.name + '_,' + item.searchMethod + '_,' + item.value;
                        templist.push(tempItem);
                    }
                })

                if (templist.length > 0) {

                    this.advancedQuerySearchKey = templist.join('_;');
                }
            }

            this.getData();
            this.showAdvancedQueryDialog = false;
        },
        addSortField() {
            this.tableSortFields.push({
                name: '',
                order: 'asc',
            });
        },
        removeSortField(i) {
            if (this.tableSortFields.length > 0) {
                this.tableSortFields.splice(i, 1);
            }
        },
        tableSortQuery() {

            this.orderByStr = '';

            if (this.tableSortFields.length > 0) {

                var templist = [];

                this.tableSortFields.forEach(item => {
                    if (item.name && item.order) {
                        var tempItem = item.name + '_,' + item.order;
                        templist.push(tempItem);
                    }
                })

                if (templist.length > 0) {

                    this.orderByStr = templist.join('_;');
                }
            }

            this.getData();
            this.showTableSortDialog = false;
        },
        exportExcel() {
            window.location.href = this.$baseUrl + "/memberPoint/exportExcel?searchKey="
                + this.filter1 + "&advancedQuery=" + this.advancedQuerySearchKey + "&orderByStr=" + this.orderByStr;
        },
        searchData() {
            this.currentPage = 1;
            this.getData();
        },
        deleteRow(row) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/memberPoint/del',
                    data: { id: row.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
                this.getData();
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
        DateTimeFormatter(row, column, cellValue) {
            if (cellValue) {
                return format(cellValue, 'YYYY/MM/DD HH:mm', { locale: zh })
            }

        },
        DateFormatter(row, column, cellValue) {
            if (cellValue) {
                return format(cellValue, 'YYYY/MM/DD', { locale: zh })
            }

        },
        showImg(img) {
            this.imgSrc = img;
            this.imageDialogVisible = true;
        },

    }
}
</script>
<style lang="less" scoped>
.container {
    background: rgba(255, 255, 255, 1);
    border-radius: 4px;
    border: 1px solid rgba(234, 234, 234, 1);
    min-height: 612px;
    padding: 0 25px 0 39px;

    .pageTitle {
        font-size: 14px;
        font-family: MicrosoftYaHei;
        font-weight: bold;
        color: rgba(51, 51, 51, 1);
        line-height: 19px;
        padding: 21px 0 9px;
        border-bottom: 1px solid #ebebeb;
    }

    .point {
        width: 953px;
        height: 106px;
        background: rgba(255, 253, 236, 1);
        line-height: 106px;
        margin-top: 5px;
        box-sizing: border-box;
        font-size: 0;

        .point-item {
            display: inline-block;
            width: 33%;
            vertical-align: middle;
            text-align: center;

            .title {
                font-size: 14px;
                font-family: MicrosoftYaHei;
                color: rgba(51, 51, 51, 1);
                line-height: 19px;
            }

            .val {
                font-size: 36px;
                font-family: MicrosoftYaHei;
                color: rgba(251, 110, 8, 1);
                line-height: 47px;
            }

            &:not(:last-child) {
                border-right: 1px solid #ebebeb;
            }
        }
    }
}
</style>
