<script setup>
    import { reactive, ref } from 'vue';
    import Taro from "@tarojs/taro";
    import { storeToRefs } from "pinia";
    import { useReturnTripStore } from "../../stores";

    const { getReturnTripList } = useReturnTripStore();
    const { returnTripList, returnTripExtra, returnTripQueries } = storeToRefs(useReturnTripStore());

    const state = reactive({
        msg: '错误提示',
        type: 'warn',
        show: false,
        cover: true,
        center: true,
    });

    function openToast(type = 'warn', msg = '错误提示') {
        state.msg = msg;
        state.type = type;
        state.show = true;
    }

    const loading = ref(false);
    const isRefreshing = ref(false);

    function onRefresh() {
        isRefreshing.value = true;
        returnTripQueries.value.pageNum = 1;
        getReturnTripList().then(() => {
            Taro.stopPullDownRefresh();
        }).catch(({ msg }) => {
            if (msg)  openToast('warn', msg);
        }).finally(() => {
            isRefreshing.value = false;
        });
    }

    function onLoadMore() {
        if (loading.value) return;
        if (returnTripList.value.length >= returnTripExtra.value.total) return;
        loading.value = true;
        returnTripQueries.value.pageNum += 1;
        getReturnTripList().catch(({ msg }) => {
            if (msg)  openToast('warn', msg);
        }).finally(() => {
            loading.value = false;
        });
    }

    function onNavTo(id) {
        Taro.navigateTo({
            url: `/pages/returnTripMap/index?id=${id}`,
        });
    }

    Taro.usePullDownRefresh(() => {
        onRefresh();
    });

    Taro.useReachBottom(() => {
        onLoadMore();
    });

    Taro.useDidShow(() => {
        getReturnTripList().catch(({ msg }) => {
            if (msg)  openToast('warn', msg);
        });
    });
</script>

<template>
    <div :class="s.root">
        <div class="list">
            <div class="item" v-for="item in returnTripList" :key="item.recordId" :class="{ 'is-pass': item.isPass }">
                <div class="title">
                    <span>{{ item.studentName || '-' }}</span>
                    <nut-button size="mini" type="info" @click="onNavTo(item.recordId)">回放</nut-button>
                </div>
                <div class="info">
                    <div class="info-row">
                        <span>班级：{{ item.className || '-' }}</span>
                        <span>无人机：{{ item.droneName || '-' }}</span>
                    </div>
                    <div class="info-row">
                        <span>教师：{{ item.teacherName || '-' }}</span>
                    </div>
                    <div class="info-row">
                        <span>开始时间：{{ item.startTime || '-' }}</span>
                    </div>
                    <div class="info-row">
                        <span>结束时间：{{ item.endTime || '-' }}</span>
                    </div>
                    <div class="info-row">
                        <span><nut-tag :type="item.isPass ? 'success' : 'danger'">{{ item.isPass ? '通过' : '未通过' }}</nut-tag></span>
                        <span><nut-tag type="warning" v-if="item.inProgress">进行中</nut-tag></span>
                    </div>
                </div>
            </div>
            <nut-empty v-if="!loading && !returnTripList.length" description="暂无数据" />
            <nut-infiniteloading
                v-if="returnTripList.length"
                load-txt="加载中..."
                load-more-txt="没有更多了"
                :has-more="returnTripList.length < returnTripExtra.total"
                @load-more="onLoadMore"
            />
        </div>
    </div>

    <nut-toast :msg="state.msg" v-model:visible="state.show" :type="state.type" :cover="state.cover" :duration="2000" />
</template>

<style lang="less" module="s">
    page {
        height: 100%;
        padding: 20px 0;
        box-sizing: border-box;
        background-color: #eaeaea;
        overflow: hidden;
    }

    .root {
        height: 100%;
        display: flex;
        flex-direction: column;

        :global {
            .list {
                flex: 1;
                overflow: auto;
                padding: 0 20px;

                .item {
                    background-color: white;
                    border-radius: 16px;
                    padding: 28px;
                    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.06);
                    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
                    cursor: pointer;
                    position: relative;
                    overflow: hidden;

                    &::before {
                        content: '';
                        position: absolute;
                        top: 0;
                        left: 0;
                        width: 6px;
                        height: 100%;
                        background-color: #4CAF50;
                        opacity: 0.8;
                    }

                    &:not(.is-pass)::before {
                        background-color: #FF5252;
                    }

                    &:hover {
                        transform: translateY(-4px);
                        box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
                    }

                    &:active {
                        transform: translateY(-2px);
                        box-shadow: 0 6px 20px rgba(0, 0, 0, 0.08);
                    }

                    & + .item {
                        margin-top: 28px;
                    }

                    .title {
                        font-size: 38px;
                        font-weight: 600;
                        margin-bottom: 24px;
                        display: flex;
                        justify-content: space-between;
                        align-items: center;
                        color: #2c3e50;

                        .class-name {
                            font-size: 32px;
                            color: #666;
                        }
                    }

                    .info {
                        color: #666;
                        font-size: 28px;
                        line-height: 1.6;

                        .info-row {
                            display: flex;
                            justify-content: space-between;
                            margin-bottom: 16px;
                            padding-right: 12px;

                            &:last-child {
                                margin-bottom: 0;
                            }

                            span {
                                color: #34495e;
                                &:first-child {
                                    color: #7f8c8d;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
</style>
