<template>
	<div>
		<el-container>
			<el-main>
				<el-form :inline="true" :model="formInline" class="demo-form-inline">
					<el-form-item>
						<el-input size="small" clearable v-model="formInline.userName" placeholder="请输入用户名" @keydown.enter.native="userPage"></el-input>
					</el-form-item>
					<el-form-item>
						<el-select size="small" v-model="formInline.relationType" multiple placeholder="请选择关系类型" @change="fansInfoPage">
							<el-option v-for="type in relationType" :key="type" :label="type" :value="type"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button size="small" type="primary" @click="fansInfoPage">查询</el-button>
					</el-form-item>
					<el-form-item>
						<el-button size="small" type="primary" @click="prepareSyncFansInfo">同步粉丝</el-button>
						<el-dialog title="提示" :visible.sync="syncFansInfoDialogVisible" width="30%" :before-close="handleClose">
							<span>确认同步粉丝吗?</span>
							<span slot="footer" class="dialog-footer">
								<el-button @click="syncFansInfoDialogVisible = false">取 消</el-button>
								<el-button type="primary" @click.prevent="syncFansInfo()">确 定</el-button>
							</span>
						</el-dialog>
					</el-form-item>
					<el-form-item>
						<el-button size="small" type="primary" @click="prepareSyncFollowInfo">同步关注</el-button>
						<el-dialog title="提示" :visible.sync="syncFollowInfoDialogVisible" width="30%" :before-close="handleClose">
							<span>确认同步关注吗?</span>
							<span slot="footer" class="dialog-footer">
								<el-button @click="syncFollowInfoDialogVisible = false">取 消</el-button>
								<el-button type="primary" @click.prevent="syncFollowInfo()">确 定</el-button>
							</span>
						</el-dialog>
					</el-form-item>
					<el-form-item>
						<el-button size="small" type="primary" @click="prepareCancleFollowInfo">取消关注</el-button>
						<el-dialog title="提示" :visible.sync="cancleFollowInfoDialogVisible" width="30%" :before-close="handleClose">
							<span>确认取消关注吗?</span>
							<span slot="footer" class="dialog-footer">
								<el-button @click="cancleFollowInfoDialogVisible = false">取 消</el-button>
								<el-button type="primary" @click.prevent="cancleFollowInfo()">确 定</el-button>
							</span>
						</el-dialog>
					</el-form-item>
					<el-form-item>
						<el-button size="small" type="primary" @click="prepareCancleNoArticle">取消关注无文章</el-button>
						<el-dialog title="提示" :visible.sync="noArticleDialogVisible" width="30%" :before-close="handleClose">
							<span>确认取消关注无文章吗?</span>
							<span slot="footer" class="dialog-footer">
								<el-button @click="noArticleDialogVisible = false">取 消</el-button>
								<el-button type="primary" @click.prevent="cancleFansNoArticle()">确 定</el-button>
							</span>
						</el-dialog>
					</el-form-item>
				</el-form>
				<el-table border :data="fansInfoList" v-loading="loading">
					<el-table-column prop="id" label="序号" width="100" sortable></el-table-column>
					<el-table-column prop="userName" label="用户名" show-overflow-tooltip></el-table-column>

					<el-table-column prop="nickName" label="用户昵称" show-overflow-tooltip>
						<template slot-scope="scope">
							<a :href="scope.row.blogUrl" target="_blank">{{ scope.row.nickName }}</a>
						</template>
					</el-table-column>

					<el-table-column prop="relationType" label="关系" show-overflow-tooltip></el-table-column>
				</el-table>
				<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[8, 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 {
            formInline: {
                relationType: [],
                userName: '',
            },
            relationType: ['互关', '粉丝', '已关注'],
            // 用户列表数据
            fansInfoList: [],
            loading: false,
            syncFansInfoDialogVisible: false,
            syncFollowInfoDialogVisible: false,
            cancleFollowInfoDialogVisible: false,
            noArticleDialogVisible: false,
            elementui_page_component_key: 0,
            currentPage: Number(localStorage.getItem('csdnFansInfoPage')) || 1,
            pageSize: 9,
            total: 0,
        }
    },

    watch: {
        'formInline.relationType'(newVal, oldVal) {
            if (newVal !== oldVal) {
                this.currentPage = 1
                localStorage.setItem('csdnFansInfoPage', this.currentPage)
                this.fansInfoPage()
            }
        },
        'formInline.userName'(newVal, oldVal) {
            if (newVal !== oldVal) {
                this.currentPage = 1
                localStorage.setItem('csdnFansInfoPage', this.currentPage)
                this.fansInfoPage()
            }
        },
        // 监听currentPage的变化，将新值保存到localStorage中
        currentPage(newPage) {
            localStorage.setItem('csdnFansInfoPage', newPage.toString())
        },
    },
    created() {
        //获取问题类型的枚举
        this.fansInfoPage()
    },
    mounted() {
        this.currentPage = 1
    },
    methods: {
        prepareSyncFansInfo() {
            this.syncFansInfoDialogVisible = true
        },
        prepareCancleNoArticle() {
            this.noArticleDialogVisible = true
        },
        prepareSyncFollowInfo() {
            this.syncFollowInfoDialogVisible = true
        },
        prepareCancleFollowInfo() {
            this.cancleFollowInfoDialogVisible = true
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then((_) => {
                    done()
                })
                .catch((_) => {})
        },
        async fansInfoPage() {
            this.loading = true
            const { data: res } = await ApiService.fansInfoPage(this.currentPage, this.pageSize, this.formInline)
            if (res.code === 200) {
                this.total = res.result.totalElements
                this.fansInfoList = res.result.content
            }
            this.loading = false
        },

        async syncFansInfo() {
            this.loading = true
            this.syncFansInfoDialogVisible = false
            const { data: res } = await ApiService.syncFansInfo()
            if (res.code === 200) {
                this.fansInfoPage()
            }
            this.loading = false
        },
        async cancleFansNoArticle() {
            this.loading = true
            this.noArticleDialogVisible = false
            const { data: res } = await ApiService.cancleFansNoArticle()
            if (res.code === 200) {
                this.fansInfoPage()
            }
            this.loading = false
        },

        async syncFollowInfo() {
            this.loading = true
            this.syncFollowInfoDialogVisible = false
            const { data: res } = await ApiService.syncFollowInfo()
            if (res.code === 200) {
                this.fansInfoPage()
            }
            this.loading = false
        },
        async cancleFollowInfo() {
            this.loading = true
            this.cancleFollowInfoDialogVisible = false
            const { data: res } = await ApiService.cancleFollowInfo()
            if (res.code === 200) {
                this.fansInfoPage()
            }
            this.loading = false
        },
        refreshPage() {
            //获取问题类型的枚举
            this.fansInfoPage()
            location.reload()
        },
        handleCurrentChange(currentPage) {
            this.currentPage = currentPage
            this.fansInfoPage()
        },
        handleSizeChange(currentSize) {
            this.pageSize = currentSize
            this.fansInfoPage()
        },
    },
}
</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>