<template>
	<view>
		<!-- tabs标签 -->
		<view class="tabsinfo flex alignCenter justifyCenter">
			<up-tabs :list="list4" lineWidth="24" lineHeight="4" @click="tabsClick" lineColor="#6aa2ff" :activeStyle="{
		    color: '#000000',
		    fontSize: '30rpx',
		    transform: 'scale(1.05)'
		}" :inactiveStyle="{
		    color: '#999999',
		    fontSize: '28rpx',
		    transform: 'scale(1.05)'
		}" itemStyle="  width: 185rpx; padding-bottom:18rpx;">
			</up-tabs>
		</view>
		<!-- 审核的信息 -->
		<up-list @scrolltolower="scrolltolower" :showScrollbar="false" :pagingEnabled="true">
			<up-list-item v-for="(item, index) in modelValue" :key="index">
				<view class="shenghe_item" @click="gotoworkinfo(item)" >
			<view class="user_header">
				<view class="">
					<view class="user_img">
						<image :src="avatarIcon(item.launchAvatar)" mode=""></image>
					</view>
					<view class="user_msg">
						<h4>{{ item.launchNickName }}</h4>
						<p>{{timeAgo(item.startTime)}}·{{ item.launchSchool }}</p>
					</view>
				</view>

				<view class="text_style">
					<view v-if="item.auditStatus == 0" 
                            style="font-size: 24rpx;color: #00CCBE ;width: 74rpx; height: 25rpx;">已通过</view>
                            <view v-else-if="item.auditStatus == 1 | item.auditStatus == 2 |item.auditStatus==3" 
                            style="font-size: 24rpx;color: #6AA2FF ;width: 74rpx; height: 25rpx;">待审核</view>
                            <view v-else-if="item.auditStatus==4" 
                            style="font-size: 24rpx;color: #FFABAB ;width: 74rpx; height: 25rpx;">未通过</view>
				</view>
			</view>
			<!-- 审核的文章标题 -->
			<view class="shenghe_title">
				<h4>{{ item.acTitle }}</h4>
			</view>
			<!-- 开始时间 -->
			<view class="start_time">
				<image src="../../../static/images/icon/starttime.png" mode=""></image>
				<p>开始时间:{{ item.startTime }}</p>
			</view>
			<!-- 结束时间 -->
			<view class="end_time">
				<image src="../../../static/images/icon/starttime.png" mode=""></image>
				<p>结束时间: {{ item.endTime }}</p>
			</view>
			<!-- 活动地点 -->
			<view class="active_address">
				<image src="../../../static/images/icon/local.png" mode=""></image>
				<p>活动地点: {{ item.addres }}</p>
			</view>
		</view>
			</up-list-item>

		</up-list>

	</view>
</template>

<script setup>
import { getAuditlistInfo } from "@/api/activityInfo/info";
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ref, getCurrentInstance, reactive, toRefs } from 'vue';
import { getDicts } from "@/api/system/dict/data";
import { onShow, onLoad, onPullDownRefresh } from '@dcloudio/uni-app'
import image from 'uview-plus/libs/config/props/image';
const { proxy } = getCurrentInstance();
const isOfficial = ref(true)
const store = useStore();
const iconConfig = proxy.iconConfig;
const bjt = iconConfig.bjt;
const tu51 = iconConfig.tu51;
const tu52 = iconConfig.tu52;
const tu53 = iconConfig.tu53;
const QNDomain = store.state.user.QNDomain;
	const list4 = ref([
    { name: '全部审核', state: 9 },
    { name: '已通过', state: 0 },
    { name: '未通过', state: 4 },
    { name: '待审核', state: 2 }
]);

	function gotoworkinfo(item){
		proxy.$tab.navigateTo(`/pages/work/activity_sh/activity_info?id=${item.id}`);
	}
	const total = ref()
const actiInfoList = ref()
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 5,
        launchUserId: null,
        accendCondition: null,
        acName: null,
        acTitle: null,
        acContent: null,
        startTime: null,
        endTime: null,
        state: null,
        addres: null,
        longitude: null,
        latitude: null,
        file: null,
        elroll: null,
        acQrcode: null,
        tab: null,
        readCount: null,
        transmitCount: null,
        likeCount: null,
        commentCount: null,
        favoriteCount: null,
        postTop: null,
        type: null,
        anonymity: null,
        auditor: null,
        auditStatus: null,
        auditTime: null,
        auditAdvice: null,
        cut: null,
    },
    rules: {
        auditStatus: [
            { required: true, message: "状态0正常1待脱敏 2待审核 3脱敏未通过 4 审核未通过 5草稿不能为空", trigger: "change" }
        ],
    }
});
const avatarIcon = (e) => {
    return QNDomain + e;
};
// 表单重置
function reset() {
    form.value = {
        id: null,
        launchUserId: null,
        accendCondition: null,
        acName: null,
        acTitle: null,
        acContent: null,
        startTime: null,
        endTime: null,
        state: null,
        addres: null,
        longitude: null,
        latitude: null,
        file: null,
        elroll: null,
        acQrcode: null,
        tab: null,
        readCount: null,
        transmitCount: null,
        likeCount: null,
        commentCount: null,
        favoriteCount: null,
        postTop: null,
        type: null,
        anonymity: null,
        createTime: null,
        auditor: null,
        auditStatus: null,
        auditTime: null,
        auditAdvice: null,
        cut: null,
        delFlag: null
    };
    proxy.resetForm("infoRef");
}
const { queryParams, form, rules } = toRefs(data);
const modelValue = ref()
// 在data中添加isLoading标志
const isLoading = ref(false);
function timeAgo(input) {
		const date = new Date(input);
		if (isNaN(date)) {
			throw new Error('Invalid date');
		}
		const now = new Date();
		const diff = now - date;
		const minutes = Math.floor(diff / 60000); // 1 minute = 60000 ms
		const hours = Math.floor(diff / 3600000); // 1 hour = 3600000 ms
		const days = Math.floor(diff / 86400000); // 1 day = 86400000 ms
		if(diff < 0){
			return formatDate(date);
		}

		if (minutes < 60) {
			if (minutes == 0) {
				return '刚刚';
			}
			return `${minutes}分钟前`;
		} else if (hours < 24) {
			return `${hours}小时前`;
		} else if (days < 3) {
			return `${days}天前`;
		} else {
			return formatDate(date);
		}
	}
	function formatDate(input) {
		const date = new Date(input);
		if (isNaN(date)) {
			throw new Error('Invalid date');
		}
		const year = date.getFullYear();
		const month = String(date.getMonth() + 1).padStart(2, '0');
		const day = String(date.getDate()).padStart(2, '0');
		return `${year}.${month}.${day}`;
	}
const scrolltolower = async () => {
    console.log("加载新数据：");
    // 检查是否还有更多数据可以加载
    if (queryParams.value.pageNum * queryParams.value.pageSize < total.value) {
        // 防止在数据加载时重复触发
        if (isLoading.value) return;
        isLoading.value = true;
        queryParams.value.pageNum += 1;
        try {
            const response = await getAuditlistInfo(queryParams.value);
            // 使用Array.prototype.push.apply将新数据追加到actiInfoList中
            Array.prototype.push.apply(modelValue.value, response.rows);
            console.log(modelValue.value);
        } catch (error) {
            console.error("加载更多数据时发生错误：", error);
        } finally {
            isLoading.value = false;
        }
    } else {
        // 可以在这里处理没有更多数据的情况，例如显示提示信息
        console.log("没有更多数据了");
    }
};
const toInfo = (item) => {
    proxy.$tab.navigateTo(`/pages/mine/myActivity/info?id=${item.id}`);
}
//删除分页信息
const removePage = () => {
    queryParams.value.pageNum = 1
    queryParams.value.pageSize = 5
}
const activityState = ref()
// 页面加载
onShow(() => {
    getList()
})
onLoad((options) => {
    getDicts('activity_state').then(e => {
        activityState.value = e.data;
    })
    reset()
    getListBytype(options.type)
})
// 页面 渲染 状态
const getListBytype = (type) => {
    if(type == 0){
        queryParams.value.auditStatus = null;
    }else if(type == 1){
        queryParams.value.auditStatus = 0;
    }else if(type == 2){
        queryParams.value.auditStatus = 2;
    }else if(type == 4){
        queryParams.value.auditStatus = 4;
    }
    
    getList()
}
// 下拉刷新
onPullDownRefresh((options) => {
    removePage();
    console.log("下拉刷新,", queryParams.value);
    getList();
})
//按照条件查询
const tabsClick = (index) => {
    removePage()
    if (index.state == 9) {
        queryParams.value.auditStatus = null;
    } else {
        queryParams.value.auditStatus = index.state
    }
    getList()

}
// 获取 全部数据
const getList = () => {
    getAuditlistInfo(queryParams.value)
        .then(res => {
			console.log("数据：",res.rows)
            uni.stopPullDownRefresh()
            total.value = res.total
            modelValue.value = res.rows
        })
}
const getDictLabelByValue = (state) => {
    const dict = activityState.value.find((dict) => dict.dictValue === state);

    return dict ? dict : '未知状态';
};
</script>

<style scoped>
	.tabsinfo {
		background-color: #fff;
		height: 80rpx;
		margin-bottom: 24rpx;
		
	}

	.shenghe_item {
		
		width: 686rpx;
		background: #ffffff;
		padding: 48rpx 32rpx 26rpx 32rpx;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		margin: 0 auto;
		margin-bottom: 24rpx;
	}

	.user_header {
		width: 100%;
		display: flex;
		justify-content: space-between;
		height: 84rpx;

		.user_img {
			width: 84rpx;
			height: 84rpx;
			margin-right: 19rpx;

			image {
				width: 84rpx;
				height: 84rpx;
				border-radius: 50%;
			}
		}

		.user_msg {
			h4 {
				font-weight: 600;
				
				height: 31rpx;
				font-size: 30rpx;
				text-align: left;
				color: #000000 100%;
				margin-bottom: 18rpx;
			}

			p {
				height: 30rpx;
				font-size: 30rpx;
				color: #999999;
			}
		}

		.text_style {


			right: 0;
			font-weight: 400;
			width: 84rpx;
			height: 28rpx;
			font-size: 28rpx;
			text-align: left;
			color: #6AA2FF;
		}
	}

	.user_header>view {
		display: flex;

	}

	.shenghe_title {
		h4 {
			font-weight: 600;
			width: 100%;
			height: 80rpx;
			font-size: 30rpx;
			text-align: left;
			color: #000000;
			margin-top: 31rpx;
		}
	}

	.shenghe_text {
		width: 100%;
		word-break: break-all;
		overflow: hidden;
		display: -webkit-box;
		-webkit-line-clamp: 2;
		-webkit-box-orient: vertical;
		margin-top: 26rpx;
	}

	.imgs_box {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;
		padding-top: 34rpx;

		image {
			width: 222rpx;
			height: 222rpx;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			margin-bottom: 6rpx;
			margin-right: 6rpx;
		}
	}

	.text_span {
		margin-top: 28rpx;

		view {
			padding: 8rpx;
			background-color: #F7F8FA;
			display: inline-block;
			border-radius: 10rpx 10rpx 10rpx 10rpx;
			font-size: 24rpx;
			color: #3477FC;
			font-weight: 600;
			margin-bottom: 20rpx;
			margin-right: 24rpx;
		}
	}
	.start_time{
		display: flex;
		align-items: center;
		height: 26rpx;
		margin-top: 34rpx;
	}
	.start_time image,.end_time image{
		width: 26rpx;
		height: 26rpx;
	}
	.start_time p,.end_time p{
		font-size: 24rpx;
		line-height: 26rpx;
	}
	.end_time{
		display: flex;
		align-items: center;
		height: 26rpx;
		margin-top: 12rpx;
	}
	.active_address{
		display: flex;
		align-items: center;
		height: 26rpx;
		margin-top: 23rpx;
	}
	.active_address image{
		width: 26rpx;
		height: 26rpx;
	}
	.active_address p{
		font-size: 24rpx;
		line-height: 26rpx;
	}
</style>