<template>
    <view class="container" @click="playGlobalSound">
        <!-- 任务列表 -->
        <view class="task-list">
            <template v-if="!loading">
                <view
                    v-if="taskList.length > 0"
                    v-for="(item, index) in taskList"
                    :key="index"
                    class="task-card"
                    @click="handleTaskClick(item)"
                >
                    <view class="task-header">
                        <text class="task-no">作业单号：{{ item.taskCode }}</text>
                    </view>
                    <view class="task-content">
                        <view class="flex-a mb-10">
                            <text class="label">仓库：</text>
                            <text class="value">{{ item.godownName }}</text>
                        </view>
                        <view class="flex-a mb-10">
                            <text class="label">订单数量：</text>
                            <text class="value">{{ item.orderNum }}</text>
                        </view>
                        <view class="flex-a mb-10">
                            <text class="label">创建时间：</text>
                            <text class="value">{{ item.createTime }}</text>
                        </view>
                        <view class="flex-a">
                            <text class="label">作业状态：</text>
                            <text class="value">{{ getTaskStatusText(item.taskStatus) }}</text>
                        </view>
                    </view>
                </view>
                <view v-else class="empty-state">
                    <text>暂无任务数据</text>
                </view>
            </template>
            <view v-else class="loading-state">
                <u-loading-icon></u-loading-icon>
                <text>加载中...</text>
            </view>
        </view>
    </view>
</template>

<script setup>
import { ref, onMounted } from "vue";

// 搜索文本
const searchText = ref("");

// 点击全局音效
function playGlobalSound(){
    uni.$u.playClickSound()
}

// 任务列表数据
const taskList = ref([]);

// 加载状态
const loading = ref(false);

// 作业状态枚举
const taskStatusEnum = {
    1: "待作业",
    2: "作业中",
    3: "已完成",
    4: "已关闭",
};

// 获取状态文本
const getTaskStatusText = (status) => {
    return taskStatusEnum[status] || "未知状态";
};

// 获取任务列表
const fetchTaskList = async () => {
    loading.value = true;
    try {
        const response = await uni.$u.http.post("/app/appstock/getNeedStockList");
        console.log(response);
        taskList.value = response;
        if (response.code === 200) {
            taskList.value = response.data || [];
        } else {
            uni.showToast({
                title: response.msg || "获取任务列表失败",
                icon: "none",
            });
        }
    } catch (error) {
        uni.showToast({
            title: "获取任务列表失败",
            icon: "none",
        });
    } finally {
        loading.value = false;
    }
};

// 搜索处理
const onSearch = () => {
    fetchTaskList();
};

// 打开扫码
const openScan = () => {
    // #ifdef APP-PLUS || MP-WEIXIN
    uni.scanCode({
        success: (res) => {
            searchText.value = res.result;
            onSearch();
        },
        fail: (err) => {
            uni.showToast({
                title: "扫码失败",
                icon: "error",
            });
        },
    });
    // #endif

    // #ifdef H5
    uni.showToast({
        title: "H5环境不支持扫码",
        icon: "none",
    });
    // #endif
};

// 点击任务卡片
const handleTaskClick = (task) => {
    uni.showModal({
        title: "领取确认",
        content: "是否确认领取下架任务?",
        cancelText: "取消",
        confirmText: "确认",
        success: async (res) => {
            playGlobalSound()
            if (res.confirm) {
                try {
                    const response = await uni.$u.http.post(`/app/appstock/lockStockTask?taskCode=${task.taskCode}`);
                    if (response.code === 200) {
                        uni.redirectTo({
                            url: `/pages/index/wms/task-detail?taskCode=${task.taskCode}`,
                        });
                    } else {
                        uni.showToast({
                            title: response.msg || "任务锁定失败",
                            icon: "none",
                        });
                    }
                } catch (error) {
                    uni.showToast({
                        title: error,
                        icon: "none",
                    });
                }
            }
        },
    });
};

// 页面加载时获取任务列表
onMounted(() => {
    fetchTaskList();
});
</script>

<style scoped>
.task-card {
    background-color: #ffffff;
    padding: 20rpx;
    margin-top: 16rpx;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.task-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16rpx;
}

.task-no {
    font-size: 28rpx;
    font-weight: bold;
}

.empty-state {
    text-align: center;
    padding: 40rpx;
    color: #999;
}

.loading-state {
    text-align: center;
    padding: 40rpx;
}

.loading-state text {
    margin-left: 12rpx;
    color: #666;
    font-size: 28rpx;
}
</style>
