<template>
	<view style="padding-top: 4vh;background-color: rgb(220, 244, 255);">
	    <uni-nav-bar left-icon="left" @clickLeft="leftClick" right-icon="scan" @clickRight="rightClick" title="修试入库" title-style="font-weight: bolder;"  backgroundColor="rgb(220, 244, 255)" :border="false" />
	</view>
    <!-- 维修列表 -->
    <view class="page-container">
        <view class="complete-btn">
            <view class="btn" @click="changeTab(1)">
                <span>未完成</span>
                <view v-if="active == 1" class="bt-line"></view>
            </view>
            <view class="btn" style="margin-left: 120rpx" @click="changeTab(2)">
                <span>已完成</span>
                <view v-if="active == 2" class="bt-line"></view>
            </view>
        </view>
        <uni-row :gutter="24" class="search-form">
            <uni-col :span="18">
                <view>
                    <uni-datetime-picker
                        v-model="dateArray"
                        type="daterange"
                        @maskClick="maskClick"
                        @change="onChangeDate"
                        placeholder="选择日期范围"
                    />
                </view>
            </uni-col> 
        </uni-row>
        <uni-row :gutter="24" class="search-form">
            <uni-col :span="18">
                <view>
                    <uni-easyinput placeholder="请输入内容" v-model="queryParams.keyWord" />
                </view>
            </uni-col>
            <uni-col :span="4">
                <view class="search" @click="onSearchBtn()">搜索</view>
            </uni-col>
        </uni-row>

        <scroll-view scroll-y @scrolltolower="onScrollTolower" class="scroll-container">
            <view
                class="table-list-item"
                :key="index"
                @click="handleItem(item)"
                v-for="(item, index) in tableList"
            >
                <!-- <uni-swipe-action> -->
                    <!-- <uni-swipe-action-item @click="onClick($event, item)" :right-options="options"> -->
                        <div class="title">
                            <span style="font-size: 15px; font-weight: 800">修试入库</span>
                            <span :style="{ color: active == 1 ? '#ff4d4f' : '#3784fb' }">{{active == 1 ? '未完成' : '已完成'}}</span>
                        </div>
                        <view class="line"></view>
                        <uni-row :gutter="24">
                            <uni-col :span="6">入库单号：</uni-col>
                            <uni-col :span="18">
                                <view class="cont">{{ item.inputCode }}</view>
                            </uni-col>
                        </uni-row>
						<uni-row :gutter="24">
                            <uni-col :span="6">维修单号：</uni-col>
                            <uni-col :span="18">
                                <view class="cont">{{ item.repairCode }}</view>
                            </uni-col>
                        </uni-row> 
                        <uni-row :gutter="24">
                            <uni-col :span="6">退料单位：</uni-col>
                            <uni-col :span="18">
                                <view class="cont">{{ item.backUnit }}</view>
                            </uni-col>
                        </uni-row>
                        <uni-row :gutter="24">
                            <uni-col :span="6">退料工程：</uni-col>
                            <uni-col :span="18">
                                <view class="cont">{{ item.backPro }}</view>
                            </uni-col>
                        </uni-row> 
						<uni-row :gutter="24">
						    <uni-col :span="6">物资类型：</uni-col>
						    <uni-col :span="18">
						        <view class="cont">{{ item.materialType }}</view>
						    </uni-col>
						</uni-row>  
                        <uni-row :gutter="24">
                            <uni-col :span="6">预入库数量：</uni-col>
                            <uni-col :span="18"
                                ><view class="cont">{{ item.pendingInputNum }}</view>
                            </uni-col>
                        </uni-row>
                        <uni-row :gutter="24">
                            <uni-col :span="6">已入库数量：</uni-col>
                            <uni-col :span="18"
                                ><view class="cont">{{ item.inputNum }}</view>
                            </uni-col>
                        </uni-row>
						<uni-row :gutter="24">
						    <uni-col :span="6">创建时间：</uni-col>
						    <uni-col :span="18"
						        ><view class="cont">{{ item.createTime }}</view>
						    </uni-col>
						</uni-row> 
                        <uni-row :gutter="24">
                            <uni-col :span="6">状态：</uni-col>
                            <uni-col :span="18">
                                <view class="cont">
                                    <uni-tag
                                        text="未完成"
                                        type="warning"
                                        v-if="item.taskStatus == 0"
                                    />
                                    <uni-tag
                                        text="已完成"
                                        type="success"
                                        v-if="item.taskStatus == 1"
                                    />
                                </view>
                            </uni-col>
                        </uni-row>
                    <!-- </uni-swipe-action-item> -->
                <!-- </uni-swipe-action> -->
            </view>
            <view class="loading-text">
                {{ finish ? '没有更多数据了~' : '正在加载...' }}
            </view>
        </scroll-view>
    </view>
</template>

<script setup>
import { ref, computed } from 'vue' 
import { getRepairedList } from '@/services/repair/testedInBound.js' 
import { onShow } from '@dcloudio/uni-app'
import { debounce } from 'lodash-es'

const total = ref(0) //                 数据总量
const active = ref(1) //                tap索引
const tableList = ref([]) //            列表数据源
const dateArray = ref([]) //            日期范围
// 查询参数
const queryParams = ref({
    startTime: '', //                   开始时间
    endTime: '', //                     结束时间
    keyWord: '', //                     关键字 
    pageNum: 1,
    pageSize: 5,
	taskStatus: 0,
})

// 右滑按钮组
const options = ref([
    {
        text: '提交',
        style: {
            backgroundColor: '#84c649',
            color: '#fff',
            fontSize: '30rpx',
        },
    },

    {
        text: '驳回',
        style: {
            backgroundColor: '#ed6042',
            color: '#fff',
            fontSize: '30rpx',
        },
    },
])

// 右滑按钮点击事件
const onClick = async (e, item) => {
    if (e.index === 0) {
        // 提交逻辑
        // const res = await repairSubmitAPI([{ taskId: item.taskId }])
        // if (res.code === 200) {
        //     uni.showToast({
        //         title: '提交成功！',
        //         icon: 'none',
        //     })
        // }
    } else {
        // 驳回逻辑
        // console.log('驳回')
        // const res = await repairRejectAPI(item.taskId)
        // if (res.code === 200) {
        //     uni.showToast({
        //         title: '驳回成功！',
        //         icon: 'none',
        //     })
        // }
    }
}

// 日期 change 事件
const onChangeDate = (val) => {
    const [val_1, val_2] = val
    queryParams.value.startTime = val_1
    queryParams.value.endTime = val_2
}

// 搜索按钮
const onSearchBtn = () => {
    queryParams.value.pageNum = 1
    tableList.value = []
    getTableList()
}

// 获取列表数据
const getTableList = async (isTap = false) => {
    // console.log('queryParams.value查询参数', queryParams.value)
    const res = await getRepairedList(queryParams.value)
    total.value = res.total
    if (isTap) {
        tableList.value = res.rows
    } else {
        if (res.rows.length == 0) {
            tableList.value = []
        } else {
            tableList.value.push(...res.rows)
        }
    }
}

// // 页面加载完毕
// onLoad(() => {
//     getTableList()
// })

onShow(() => {
    tableList.value = []
    total.value = 0
    getTableList()
})

// 滚动触底事件
const onScrollTolower = debounce(() => {
    console.log('触底事件')
    if (total.value > tableList.value.length) {
        queryParams.value.pageNum++
        getTableList()
    }
}, 500)

// tap 栏切换
const changeTab = (index) => {
    active.value = index
    if (index == 1) {
        queryParams.value.taskStatus = 0 // 查未完成的
        queryParams.value.pageNum = 1
        getTableList(true)
    } else if (index == 2) {
        queryParams.value.taskStatus = 1 // 查已完成的
        queryParams.value.pageNum = 1
        getTableList(true)
    }
}
// 点击跳转详情
const handleItem = (item) => {
    uni.navigateTo({ url: `/pages/repair/testedInBound/details?taskInfo=${JSON.stringify(item)}` })
}

// 判断数据是否加载完毕
const finish = computed(() => {
    if (total.value === tableList.value.length) return true
})


const leftClick = () => {
    console.log(1)
    uni.navigateBack({
        delta: 1 // 返回到已存在的页面
    });
}

const rightClick = () => {
    console.log(2)
    uni.navigateTo({ url: `/pages/repair/testedInBound/codeScan` })
}
const maskClick = () => {}
</script>

<style lang="scss" scoped>
.page-container {
    display: flex;
    height: 100vh;
    padding: 24rpx;
    flex-direction: column;
    background-color: #f7f8fa;

    // 顶部切换按钮
    .complete-btn {
        display: flex;
        padding: 24rpx;
        background: #fff;
        border-radius: 20rpx;
        margin-bottom: 12rpx;
        box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);

        .btn {
            position: relative;
            padding: 16rpx 32rpx;
            font-size: 28rpx;
            color: #8c8c8c;
            font-weight: 600;
            transition: all 0.3s ease;

            &:has(.bt-line) {
                color: #3784fb;
            }

            .bt-line {
                position: absolute;
                left: 50%;
                bottom: -4rpx;
                transform: translateX(-50%);
                width: 48rpx;
                height: 4rpx;
                background: #3784fb;
                border-radius: 4rpx;
            }
        }
    }

    // 搜索区域
    .search-form {
        margin-bottom: 24rpx;
        // width: 96%;
		margin: 10px;
		display: flex;
		align-items: center;
		background-color: #fff;
		border-radius: 20rpx; 
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06); 
        :deep(.uni-easyinput__content) {
            background-color: #fff;
            border-radius: 12rpx;
            height: 88rpx;
            padding: 0 24rpx;
            transition: all 0.3s ease;

            &:focus-within {
                border-color: #3784fb;
                box-shadow: 0 0 0 2rpx rgba(55, 132, 251, 0.1);
            }
        }

        // 日期选择器
        :deep(.uni-date) {
            .uni-date-editor {
                background-color: #fff;
                border-radius: 12rpx;
                height: 88rpx;
                padding: 10rpx 24rpx;
                transition: all 0.3s ease;

                &:focus-within {
                    border-color: #3784fb;
                    box-shadow: 0 0 0 2rpx rgba(55, 132, 251, 0.1);
                }
            }

            .uni-date-range--text {
                font-size: 28rpx;
                color: #262626;
            }

            .uni-date-range--btn {
                margin: 0 16rpx;
                color: #8c8c8c;
            }
        }

        .search {
            height: 88rpx;
            line-height: 88rpx;
            text-align: center;
            color: #fff;
            font-size: 28rpx;
            font-weight: 600;
            border-radius: 12rpx;
            transition: all 0.3s ease;
            background: linear-gradient(135deg, #4b8eff 0%, #3784fb 100%);
            box-shadow: 0 6rpx 20rpx rgba(55, 132, 251, 0.2);

            &:active {
                transform: scale(0.98);
                opacity: 0.9;
            }
        }
    }

    // 列表区域
    .scroll-container {
        flex: 1;
        overflow: hidden;

        .table-list-item {
            background-color: #fff;
            border-radius: 20rpx;
            padding: 24rpx;
            margin-bottom: 24rpx;
            box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);
            transition: all 0.3s ease;
            cursor: pointer;

            &:active {
                transform: scale(0.99);
                opacity: 0.9;
            }

            // 标题栏
            .title {
                padding: 16rpx 0;
                display: flex;
                justify-content: space-between;
                align-items: center;
                border-bottom: 2rpx solid #f5f5f5;
                
                span {
                    font-size: 32rpx;
                    color: #262626;
                    font-weight: 600;
                }
            }

            // 分隔线
            .line {
                margin: 0;
                height: 2rpx;
                background-color: #f5f5f5;
            }

            // 内容行
            .uni-row {
                padding: 16rpx 0;
                font-size: 28rpx;
                border-bottom: 2rpx solid #f5f5f5;

                &:last-child {
                    border-bottom: none;
                }

                .uni-col-6 {
                    color: #8c8c8c;
                }

                .cont {
                    color: #262626;
                    display: flex;
                    justify-content: flex-end;

                    // 标签样式
                    :deep(.uni-tag) {
                        margin: 0;
                        padding: 0 20rpx;
                        height: 48rpx;
                        line-height: 48rpx;
                        border-radius: 24rpx;
                        font-size: 24rpx;
                        font-weight: 500;
                        border: none;

                        &.uni-tag--warning {
                            color: #ff9800;
                            background-color: rgba(255, 152, 0, 0.1);
                        }

                        &.uni-tag--success {
                            color: #52c41a;
                            background-color: rgba(82, 196, 26, 0.1);
                        }
                    }
                }
            }
        }
    }
}

// 加载提示文字
.loading-text {
    text-align: center;
    font-size: 28rpx;
    color: #8c8c8c;
    padding: 24rpx 0;
}

// 右滑按钮样式
:deep(.uni-swipe_button) {
    writing-mode: vertical-rl;
    padding: 0 24rpx !important;
    font-size: 28rpx !important;
    font-weight: 600;
    transition: all 0.3s ease;

    &:active {
        opacity: 0.8;
    }
}
</style>
