<template>
    <view class="task-center">

        <!-- <u-subsection :list="tabList" 
		active-color="#ffaa48"
		bg-color="#fff"
		inactive-color="#aaaaaa"
		font-size="16"
		:current="curNow" 
		@change="sectionChange"></u-subsection> -->

        <!-- <u-tabs :list="list1" @click="sectionChange"></u-tabs> -->

        <view class="container-tabs">
            <u-tabs :list="tabList" :scrollable="false" lineWidth="68rpx" :activeStyle="{
                color: '#fff',
                fontWeight: 'bold',
                transform: 'scale(1.05)'
            }" :inactiveStyle="{
            color: '#eee',
            transform: 'scale(1)'
        }" :itemStyle="{ height: '88rpx', padding: '0 52rpx' }" lineColor="#fff" @click="sectionChange"></u-tabs>
        </view>

        <!-- 待接受任务列表 -->
        <view v-if="curNow === 0">
            <view v-for="(item, index) in pendingTasks" style="padding:28rpx 36rpx 0;" :key="index">
                <view class="task-card">
                    <view class="task-header">
                        <view class="task-image">
                            <image
                                src="https://image.hhlm1688.com/2025/08/07767f2fc0a686433bb9e3f90bc6e60990Group%201000001612@3x.png"
                                mode="heightFix"></image>
                        </view>
                        <view class="task-type">悬赏任务</view>
                        <view class="task-reward" v-if="item.type == 0">酬劳 <text>{{ item.taskCouponTitle }}</text></view>
                        <view class="task-reward" v-if="item.type == 1">酬劳 <text> ¥{{ item.taskMoney }}</text></view>
                    </view>
                    <view class="task-content">
                        <view class="task-icon">
                            <image :src="item.taskIcon" style="width: 150rpx; height: 150rpx;" mode="aspectFill">
                            </image>
                        </view>
                        <view class="task-info">
                            <view class="task-title">{{ item.title }}</view>
                            <view class="task-desc">主题：{{ item.theme }}</view>
                            <view class="task-deadline">任务截止日期: {{ item.endTime }}</view>
                        </view>
                    </view>
                    <view class="task-footer">
                        <u-button shape="circle" plain text="查看详情" @click="viewTaskDetail(item, 0)"></u-button>
                        <u-button shape="circle" color="#ffaa48" text="立即接受"
                            @click="acceptTaskHandler(item)"></u-button>
                    </view>
                </view>
            </view>
            <view v-if="pendingTasks.length === 0" class="empty-tip">
                <text>暂无待接受任务</text>
            </view>
        </view>

        <!-- 已接收任务列表 -->
        <view v-else>
            <view v-for="(item, index) in acceptedTasks" style="padding:28rpx 36rpx 0;" :key="index">
                <view class="task-card">
                    <view class="task-header">
                        <view class="task-image">
                            <image
                                src="https://image.hhlm1688.com/2025/08/07767f2fc0a686433bb9e3f90bc6e60990Group%201000001612@3x.png"
                                mode="heightFix"></image>
                        </view>
                        <view class="task-type">悬赏任务</view>
                        <!-- <template v-if="item.status == 0">
                            <view class="task-reward" v-if="item.type == 0">酬劳 <text>{{ item.taskCouponTitle }}</text></view>
                            <view class="task-reward" v-if="item.type == 1">酬劳 <text> ¥{{ item.taskMoney }}</text></view>
                        </template> -->
                        <view class="task-status-tag"
                            :class="{ 'status-pending': item.status === 0, 'status-reviewing': item.status === 1, 'status-rejected': item.status === 3, 'status-approved': item.status === 2 }">
                            <text v-if="item.status === 0">待上传</text>
                            <text v-else-if="item.status === 1">待审核</text>
                            <text v-else-if="item.status === 2">审核通过</text>
                            <text v-else-if="item.status === 3">已驳回</text>
                        </view>
                    </view>
                    <view class="task-content">
                        <view class="task-icon">
                            <image :src="item.taskIcon" style="width: 150rpx; height: 150rpx;" mode="aspectFill">
                            </image>
                        </view>
                        <view class="task-info">
                            <view class="task-title">{{ item.title }}</view>
                            <view class="task-desc">主题：{{ item.theme }}</view>
                            <view class="task-deadline">任务截止日期: {{ item.endTime }}</view>
                        </view>
                    </view>
                    <view class="task-footer">
                        <u-button shape="circle" plain text="查看详情" @click="viewTaskDetail(item, 1)"></u-button>
                        <u-button v-if="item.status === 3" shape="circle" type="error" text="驳回原因"
                            @click="showRejectReason(item)"></u-button>
                        <u-button v-if="item.status === 0" shape="circle" color="#ffaa48" text="立即上传"
                            @click="uploadTask(item)"></u-button>
                        <u-button v-if="item.status === 1" shape="circle" disabled text="审核中"></u-button>
                        <u-button v-if="item.status === 3" shape="circle" color="#ffaa48" text="重新上传"
                            @click="uploadTask(item)"></u-button>
                        <view v-if="item.status === 2" class="task-status task-status-approved">已通过</view>
                    </view>
                </view>
            </view>
            <view v-if="acceptedTasks.length === 0" class="empty-tip">
                <text>暂无已接收任务</text>
            </view>
        </view>
    </view>
</template>

<script>
import {
    getTaskList,
    getTaskDetail,
    acceptTask,
    getTaskListByUser,
    getTaskDetailUser
} from "@/api/order/task.js"
export default {
    data() {
        return {
            tabList: [
                {
                    name: '待接受',
                    badge: {
                        value: 0,
                    }
                },
                {
                    name: '已接受',
                    badge: {
                        value: 0,
                    }
                },
            ],
            curNow: 0,
            pendingTasks: [],
            acceptedTasks: []
        }
    },
    onShow() {
        // 加载任务列表数据
        this.getTaskList()
    },
    methods: {
        sectionChange({ index }) {
            this.curNow = index;
            this.getTaskList()
        },
        getTaskList() {
            if (this.curNow == 0) {
                getTaskList().then(res => {
                    if (res && res.code === 200) {
                        let rows = res.rows || []
                        this.pendingTasks = rows
                    }
                })
            }else{
                getTaskListByUser().then(res => {
                    if (res && res.code === 200) {
                        let rows = res.rows || []
                        this.acceptedTasks = rows
                    }
                })
            }

        },

        // 格式化日期
        formatDate(dateStr) {
            if (!dateStr) return '';
            let date = new Date(dateStr);
            let year = date.getFullYear();
            let month = (date.getMonth() + 1).toString().padStart(2, '0');
            let day = date.getDate().toString().padStart(2, '0');
            return `${year}-${month}-${day}`;
        },
        viewTaskDetail(task, status) {
            // 查看任务详情，跳转到任务详情页面
            uni.navigateTo({
                url: `/pages_order/task/taskDetail?id=${task.id}&status=${status}`
            });
        },
        acceptTaskHandler(task) {
            // 接受任务
            uni.showModal({
                title: '接受任务',
                content: `确定接受任务: ${task.title}？`,
                success: res => {
                    if (res.confirm) {
                        acceptTask({ id: task.id }).then(res => {
                            if (res && res.code === 200) {
                                uni.showToast({
                                    title: '任务接受成功',
                                    icon: 'success'
                                });
                                // 刷新任务列表
                                this.getTaskList();
                            }
                        });
                    }
                }
            });
        },
        uploadTask(task) {
            // 上传任务
            uni.navigateTo({
                url: `/pages_order/task/taskUpload?id=${task.id}`
            });
        },
        showRejectReason(task) {
            // 显示驳回原因
            if (task.rejectTxt) {
                uni.showModal({
                    title: '驳回原因',
                    content: task.rejectTxt,
                    showCancel: false
                });
            } else {
                uni.showToast({
                    title: '暂无驳回原因',
                    icon: 'none'
                });
            }
        },
    }
}
</script>

<style lang="scss">
.task-center {
    background-color: #f5f5f7;
    min-height: 100vh;

    .container-tabs {
        background-color: #FFBF60;
    }

    .task-status-tag {
        display: inline-block;
        font-size: 24rpx;
        padding: 6rpx 20rpx;
        border-radius: 20rpx;
        margin-top: 10rpx;
        margin-left: auto;

        &.status-pending {
            // background-color: #E6F7FF;
            color: #1890FF;
        }

        &.status-reviewing {
            // background-color: #FFF7E6;
            color: #FA8C16;
        }

        &.status-rejected {
            // background-color: #FFF1F0;
            color: #F5222D;
        }

        &.status-approved {
            // background-color: #F6FFED;
            color: #52C41A;
        }
    }

    .task-card {
        background-color: #FFFFFF;
        border-radius: 16rpx;
        margin-bottom: 30rpx;
        overflow: hidden;

        .task-header {
            display: flex;
            align-items: center;
            margin-bottom: 20rpx;
            background-color: #FFF4E599;
            padding: 15rpx;

            .task-image {
                margin-right: 30rpx;
                display: flex;
                align-items: center;

                image {
                    height: 60rpx;
                    width: 50rpx;
                }
            }

            .task-type {
                color: #C68C5B;
                font-size: 26rpx;
                background-color: #FFE4BB;
                padding: 10rpx 30rpx;
                border-radius: 30rpx;
                flex-shrink: 0;
            }

            .task-reward {
                font-size: 26rpx;
                margin-left: auto;
                flex-shrink: 0;

                text {
                    color: #FF5722;
                    font-weight: bold;
                    margin-left: 10rpx;
                    font-size: 30rpx;
                }
            }
        }

        .task-content {
            display: flex;
            margin-bottom: 30rpx;
            padding: 0 30rpx 0 30rpx;

            .task-icon {
                margin-right: 30rpx;
            }

            .task-info {
                flex: 1;
                position: relative;
                display: flex;
                flex-direction: column;
                gap: 10rpx;

                .task-title {
                    font-size: 32rpx;
                    font-weight: bold;
                    color: #333;
                    margin-bottom: 10rpx;
                }

                .task-desc {
                    font-size: 28rpx;
                    color: #666;
                    margin-bottom: 10rpx;
                    color: #FFC673;
                }

                .task-deadline {
                    font-size: 24rpx;
                    color: #999;
                    margin-bottom: 10rpx;
                }
            }
        }

        .task-footer {
            padding-bottom: 30rpx;
            display: flex;
            justify-content: center;
            align-items: flex-end;
            gap: 20rpx;
            flex-wrap: wrap;

            .u-button {
                width: 200rpx;
                height: 60rpx;
                font-size: 28rpx;
            }

            .task-status {
                height: 60rpx;
                display: flex;
                align-items: center;
                justify-content: center;
                width: 200rpx;
                font-size: 28rpx;

                &.task-status-approved {
                    color: #52C41A;
                }
            }
        }
    }

    .empty-tip {
        text-align: center;
        padding: 60rpx 0;
        color: #999;
        font-size: 28rpx;
    }
}
</style>