<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.tripletDate" 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.weekInfo" placeholder="请选择星期" @change="tripletDayInfoPage">
							<el-option v-for="item in weekInfo" :key="item" :label="item" :value="item"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="tripletDayInfoPage">查询</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="prepareReset">重置</el-button>
						<el-dialog title="提示" :visible.sync="resetDialogVisible" width="30%">
							<span>确认重置吗?</span>
							<span slot="footer" class="dialog-footer">
								<el-button @click="resetDialogVisible = false">取 消</el-button>
								<el-button type="primary" @click.prevent="tripletDayReset()">确 定</el-button>
							</span>
						</el-dialog>
					</el-form-item>
				</el-form>
				<el-table border :data="tripletDayInfoList" v-loading="loading">
					<el-table-column align="center" prop="id" label="序号" width="100" sortable></el-table-column>
					<el-table-column align="center" label="三连时间">
						<template slot-scope="props">
							{{ props.row.tripletDate | dateDayFormat }}
						</template>
					</el-table-column>
					<el-table-column align="center" sortable prop="likeNum" label="点赞数量" show-overflow-tooltip>
						<template slot-scope="scope">
							<el-tag :type="isMaxValue(scope.row.likeNum, 'likeNum')">{{ scope.row.likeNum }}</el-tag>
						</template>
					</el-table-column>
					<el-table-column align="center" sortable prop="collectNum" label="收藏数量" show-overflow-tooltip>
						<template slot-scope="scope">
							<el-tag :type="isMaxValue(scope.row.collectNum, 'collectNum')">{{ scope.row.collectNum }}</el-tag>
						</template>
					</el-table-column>

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

					<el-table-column align="center" prop="likeStatusName" label="点赞状态" width="130" show-overflow-tooltip>
						<template slot-scope="scope">
							<el-tag :type="getTagType(scope.row.likeStatusName)">{{ scope.row.likeStatusName }}</el-tag>
						</template>
					</el-table-column>

					<el-table-column align="center" prop="collectStatusName" label="收藏状态" width="130" show-overflow-tooltip>
						<template slot-scope="scope">
							<el-tag :type="getTagType(scope.row.collectStatusName)">{{ scope.row.collectStatusName }}</el-tag>
						</template>
					</el-table-column>

					<el-table-column align="center" prop="commentStatusName" label="评论状态" width="130" show-overflow-tooltip>
						<template slot-scope="scope">
							<el-tag :type="getTagType(scope.row.commentStatusName)">{{ scope.row.commentStatusName }}</el-tag>
						</template>
					</el-table-column>

					<el-table-column align="center" prop="weekInfo" label="星期" show-overflow-tooltip></el-table-column>
				</el-table>
				<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[7, 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 ApiService from '../../api/ApiService'
export default {
    name: 'MyTripletDayInfo',

    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])
                        },
                    },
                ],
            },
            formInline: {
                tripletDate: null,
                endDate: null,
                weekInfo: null,
            },
            form: {
            },
            tripletDayInfoList: [],
            weekInfo: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日'],
            likeStatus: [
                {
                    type: -1,
                    name: '全部',
                },
                {
                    type: 0,
                    name: '未处理',
                },
                { type: 1, name: '已经点过赞' },
                { type: 2, name: '点赞已满' },
                { type: 3, name: '取消点赞' },
                { type: 4, name: '文章状态不能点赞' },
                { type: 8, name: '其他错误' },
                { type: 9, name: '点赞成功' },
            ],
            collectStatus: [
                {
                    type: -1,
                    name: '全部',
                },
                {
                    type: 0,
                    name: '未处理',
                },
                { type: 1, name: '已经收藏过' },
                { type: 2, name: '收藏已满' },
                { type: 3, name: '参数缺失' },
                { type: 4, name: '收藏夹不存在' },
                { type: 8, name: '其他错误' },
                { type: 9, name: '收藏成功' },
            ],

            commentStatus: [
                {
                    type: -1,
                    name: '全部',
                },
                {
                    type: 0,
                    name: '未处理',
                },
                { type: 1, name: '已经评论过' },
                { type: 2, name: '评论已满' },
                { type: 3, name: '禁言' },
                { type: 4, name: '评论太快' },
                { type: 5, name: '评论已到40条' },
                { type: 8, name: '其他错误' },
                { type: 9, name: '评论成功' },
            ],
            loading: false,
            resetDialogVisible: false,
            elementui_page_component_key: 0,
            currentPage: Number(localStorage.getItem('csdnTripletDayInfoPage')) || 1,
            pageSize: 7,
            total: 0,
        }
    },
    watch: {
        'formInline.tripletDate'(newVal, oldVal) {
            if (newVal !== oldVal) {
                this.currentPage = 1
                localStorage.setItem('csdnTripletDayInfoPage', this.currentPage)
                this.tripletDayInfoPage()
            }
        },
        'formInline.endDate'(newVal, oldVal) {
            if (newVal !== oldVal) {
                this.currentPage = 1
                localStorage.setItem('csdnTripletDayInfoPage', this.currentPage)
                this.tripletDayInfoPage()
            }
        },
        // 监听currentPage的变化，将新值保存到localStorage中
        currentPage(newPage) {
            localStorage.setItem('csdnTripletDayInfoPage', newPage.toString())
        },
    },
    created() {
        //获取问题类型的枚举
        this.tripletDayInfoPage()
    },
    mounted() {
        this.currentPage = 1
    },
    activated() {
        this.tripletDayInfoPage()
    },
    methods: {
        prepareReset() {
            this.resetDialogVisible = true
        },
        async tripletDayInfoPage() {
            this.loading = true
            const { data: res } = await ApiService.tripletDayInfoPage(this.currentPage, this.pageSize, this.formInline)
            if (res.code === 200) {
                this.total = res.result.from.totalElements

                const likeStatusMap = {}
                this.likeStatus.forEach((option) => {
                    likeStatusMap[option.type] = option.name
                })
                res.result.from.content.forEach((item) => {
                    item.likeStatusName = likeStatusMap[item.likeStatus]
                })

                const collectStatusMap = {}
                this.collectStatus.forEach((option) => {
                    collectStatusMap[option.type] = option.name
                })
                res.result.from.content.forEach((item) => {
                    item.collectStatusName = collectStatusMap[item.collectStatus]
                })

                const commentStatusMap = {}
                this.commentStatus.forEach((option) => {
                    commentStatusMap[option.type] = option.name
                })
                res.result.from.content.forEach((item) => {
                    item.commentStatusName = commentStatusMap[item.commentStatus]
                })
                this.tripletDayInfoList = res.result.from.content
            }
            this.loading = false
        },
        async tripletDayReset() {
            this.loading = true
            const { data: res } = await ApiService.tripletDayReset()
            this.resetDialogVisible = false
            this.tripletDayInfoPage()
            this.loading = false
        },
        refreshPage() {
            //获取问题类型的枚举
            this.tripletDayInfoPage()
            location.reload()
        },
        getTagType(statusName) {
            if (statusName == '未处理') {
                return 'info'
            } else if (statusName == '已经点过赞' || statusName == '已经收藏过' || statusName == '已经评论过') {
                return 'success'
            } else if (statusName == '文章状态不能点赞' || statusName == '收藏已满' || statusName == '收藏夹不存在' || statusName == '评论已满' || statusName == '禁言' || statusName == '评论已到40条') {
                return 'danger'
            } else if (statusName == '点赞成功' || statusName == '收藏成功' || statusName == '评论成功') {
                return 'success'
            } else if (statusName == '点赞已满' || statusName == '参数缺失' || statusName == '其他错误' || statusName == '取消点赞' || statusName == '评论太快') {
                return 'warning'
            }
        },
        handleCurrentChange(currentPage) {
            this.currentPage = currentPage
            this.tripletDayInfoPage()
        },
        handleSizeChange(currentSize) {
            this.pageSize = currentSize
            this.tripletDayInfoPage()
        },
        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.tripletDayInfoList.reduce((maxObj, currentObj) => {
                const tagValue = currentObj[_tag]

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

                if (tagValue !== undefined && tagValue < maxObj[_tag]) {
                    return currentObj
                } else {
                    return maxObj
                }
            }, this.tripletDayInfoList[0])
        },
    },
}
</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>