<template>
	<div>
		<el-container>
			<el-main>
				<el-form :inline="true" :model="formInline" class="demo-form-inline">
					<el-form-item>
						<el-date-picker v-model="formInline.accountDate" type="daterange" align="right" unlink-panels range-separator="~" start-placeholder="开始日期" end-placeholder="结束日期" :value-format="'yyyy-MM-dd'" :picker-options="pickerOptions"></el-date-picker>
					</el-form-item>
					<el-form-item>
						<el-select v-model="formInline.operateType" placeholder="请选择类型" @change="accountManagementPage">
							<el-option v-for="item in operateTypeArray" :key="item.type" :label="item.name" :value="item.type"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-select v-model="formInline.productName" placeholder="请选择业务" @change="accountManagementPage">
							<el-option v-for="type in productName" :key="type" :label="type" :value="type"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-select v-model="formInline.code" placeholder="请选择code" @change="accountManagementPage">
							<el-option v-for="type in code" :key="type" :label="type" :value="type"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="accountManagementPage">查询</el-button>
					</el-form-item>

					<el-form-item>
						<el-button type="primary" @click="add5AccountInfo">同步5页</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="addAccountInfo">同步全部</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="mall">商城</el-button>
					</el-form-item>
					<br />
					<el-form-item label="当前余额:" class="form-item">
						<el-tag style="width: 55px">{{ form.currentAmount }}</el-tag>
					</el-form-item>
					<el-form-item label="永久有效:" class="form-item">
						<el-tag style="width: 55px">{{ form.normalAmount }}</el-tag>
					</el-form-item>
					<el-form-item label="可过期:" class="form-item">
						<el-tag style="width: 55px">{{ form.expireAmount }}</el-tag>
					</el-form-item>
					<el-form-item label="总收入:" class="form-item">
						<el-tag type="success" style="width: 55px">{{ form.totalGetAmount }}</el-tag>
					</el-form-item>
					<el-form-item label="总支出:" class="form-item">
						<el-tag type="warning" style="width: 55px">{{ form.totalPayAmount }}</el-tag>
					</el-form-item>
					<el-form-item label="当页累计:" class="form-item">
						<el-tag style="width: 55px">{{ form.currentTotal }}</el-tag>
					</el-form-item>
				</el-form>
				<el-table border :data="accountManagementList" v-loading="loading">
					<el-table-column sortable align="center" prop="id" label="序号" width="100"></el-table-column>

					<el-table-column sortable align="center" prop="amount" label="金额" show-overflow-tooltip>
						<template slot-scope="scope">
							<el-tag :type="isMaxValue(scope.row.amount, 'amount')">{{ scope.row.amount }}</el-tag>
						</template>
					</el-table-column>

					<el-table-column prop="product" label="业务类型" show-overflow-tooltip></el-table-column>
					<el-table-column prop="productName" label="业务名称" show-overflow-tooltip></el-table-column>
					<el-table-column prop="code" label="业务code" show-overflow-tooltip></el-table-column>
					<el-table-column prop="orderNo" label="订单号" show-overflow-tooltip></el-table-column>
					<el-table-column sortable prop="expireTime" label="过期时间" show-overflow-tooltip></el-table-column>

					<el-table-column align="center" prop="operateType" label="收入支出" show-overflow-tooltip>
						<template slot-scope="scope">
							<el-tag :type="getTagType(scope.row.operateTypeName)">{{ scope.row.operateTypeName }}</el-tag>
						</template>
					</el-table-column>

					<el-table-column prop="description" label="详情" show-overflow-tooltip></el-table-column>

					<el-table-column label="业务时间">
						<template slot-scope="props">{{ props.row.time | dateFormat }}</template>
					</el-table-column>
				</el-table>
				<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 50, 100, 200, 400]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
			</el-main>
			<el-backtop class="backtop"></el-backtop>
		</el-container>
	</div>
</template>

<script>
import axios from 'axios'
import ApiService from '../../api/ApiService'
export default {
    name: 'MyAccountManagement',

    data() {
        return {
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now()
                },

                shortcuts: [
                    {
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date()
                            const start = new Date()
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
                            picker.$emit('pick', [start, end])
                        },
                    },
                    {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date()
                            const start = new Date()
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
                            picker.$emit('pick', [start, end])
                        },
                    },
                    {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date()
                            const start = new Date()
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
                            picker.$emit('pick', [start, end])
                        },
                    },
                ],
            },
            productName: ['全部', '红包', '盲盒', '商城iOS支付', 'proceeds_balance', 'VIP'],
            code: [7000, 7002],
            formInline: {
                accountDate: null,
                operateType: null,
                productName: null,
                code: null,
            },
            form: {
                currentAmount: '',
                totalGetAmount: '',
                totalPayAmount: '',
                expireAmount: '',
                normalAmount: '',
                currentTotal: '',
            },
            operateTypeArray: [
                {
                    type: -1,
                    name: '全部',
                },
                {
                    type: 1,
                    name: '收入',
                },
                { type: 2, name: '支出' },
            ],

            accountManagementList: [],
            loading: false,
            elementui_page_component_key: 0,
            currentPage: Number(localStorage.getItem('accountManagementPage')) || 1,
            pageSize: 10,
            total: 0,
        }
    },
    watch: {
        formInline: {
            handler(newVal, oldVal) {
                this.currentPage = 1
                localStorage.setItem('accountManagementPage', this.currentPage)
                this.accountManagementPage()
            },
            deep: true,
        },
        // 监听currentPage的变化，将新值保存到localStorage中
        currentPage(newPage) {
            localStorage.setItem('accountManagementPage', newPage.toString())
        },
    },
    created() {
        //获取问题类型的枚举
        this.accountManagementPage()
    },
    mounted() {
        this.currentPage = 1
    },
    activated() {
        this.accountManagementPage()
    },
    methods: {
        async accountManagementPage() {
            this.loading = true
            const { data: res } = await ApiService.accountManagementPage(this.currentPage, this.pageSize, this.formInline)
            if (res.code === 200) {
                this.total = res.result.from.totalElements
                const userWeightMap = {}
                this.operateTypeArray.forEach((option) => {
                    userWeightMap[option.type] = option.name
                })
                res.result.from.content.forEach((item) => {
                    item.operateTypeName = userWeightMap[item.operateType]
                })
                this.accountManagementList = res.result.from.content
                this.form.currentAmount = res.result.currentAmount
                this.form.totalGetAmount = res.result.totalGetAmount
                this.form.totalPayAmount = res.result.totalPayAmount
                this.form.expireAmount = res.result.expireAmount
                this.form.normalAmount = res.result.normalAmount
                this.form.currentTotal = res.result.currentTotal
            }
            this.loading = false
        },

        async addAccountInfo() {
            this.loading = true
            const { data: res } = await ApiService.addAccountInfo()
            if (res.code === 200) {
                this.accountManagementPage()
            }
            this.loading = false
        },
        async add5AccountInfo() {
            this.loading = true
            const { data: res } = await ApiService.add5AccountInfo()
            if (res.code === 200) {
                this.accountManagementPage()
            }
            this.loading = false
        },

        handleCurrentChange(currentPage) {
            this.currentPage = currentPage
            this.accountManagementPage()
        },
        handleSizeChange(currentSize) {
            this.pageSize = currentSize
            this.accountManagementPage()
        },
        isMaxValue(value, tag) {
            const maxValue = this.getMaxValue(tag)[tag]
            if (value === maxValue) {
                if (tag === 'loseRedPackageCount') {
                    return 'danger'
                } else {
                    return 'success'
                }
            }
            const minValue = this.getMinValue(tag)[tag]
            if (value === minValue) {
                if (tag === 'loseRedPackageCount') {
                    return 'success'
                } else {
                    return 'danger'
                }
            }
            return 'primary'
        },
        getMaxValue(_tag) {
            return this.accountManagementList.reduce((maxObj, currentObj) => {
                const tagValue = currentObj[_tag]

                if (tagValue !== undefined && tagValue > maxObj[_tag]) {
                    return currentObj
                } else {
                    return maxObj
                }
            }, this.accountManagementList[0])
        },
        getMinValue(_tag) {
            return this.accountManagementList.reduce((maxObj, currentObj) => {
                const tagValue = currentObj[_tag]

                if (tagValue !== undefined && tagValue < maxObj[_tag]) {
                    return currentObj
                } else {
                    return maxObj
                }
            }, this.accountManagementList[0])
        },
        refreshPage() {
            //获取问题类型的枚举
            this.accountManagementPage()
            location.reload()
        },
        getTagType(operateTypeName) {
            if (operateTypeName == '收入') {
                return 'success'
            } else {
                return 'danger'
            }
        },
        mall() {
            // 新页面打开
            window.open('https://mall.csdn.net/search?cat1=379&cat2=385&cat3=431&sort=salesVolume&asc=desc&page=1')
        },
    },
}
</script>

<style lang="less" scoped>
.el-header {
	background-color: #b3c0d1;
	color: #333;
	line-height: 60px;
}

.el-aside {
	color: #333;
}

.pagination {
	margin-top: 16px;
	text-align: right;
}
.header-button-item {
	margin-right: 15px;
	font-size: 20px;
}

.red-title {
	line-height: 24px;
	font-size: 18px;
	color: red;
}

.backtop {
	position: fixed;
	bottom: 50px;
	right: 50px;
	height: 40px;
	width: 40px;
	line-height: 40px;
	text-align: center;
	border-radius: 20px;
	background-color: #007aff;
	color: #fff;
	cursor: pointer;
	z-index: 999;
}
.custom-textarea {
	width: 100%;
	text-align: left;
}
.backtop:hover {
	background-color: #0050a0;
}
</style>