<template>
    <view class="codefun-flex-col page">
        <z-paging ref="paging" v-model="pageData.list" @query="queryList">
            <view class="codefun-flex-col group_3">
                <view class="codefun-flex-row codefun-items-center section_2">
                    <image class="image_6" src="/static/images/codefun/6c5c5a3c082b8c60a307d3a7caee623c.png" />
                    <u-input v-model="pageData.param.deviceName" placeholder="请输入设备名称搜索" border="none"
                        class="codefun-ml-8" @confirm="handleSearch" />
                </view>

                <!-- 设备列表 -->
                <view class="device-list">
                    <uni-swipe-action>
                        <uni-swipe-action-item v-for="(item, index) in pageData.list" :key="index" :right-options="swipeOptions"
                            @click="(e) => handleSwipeAction(e, item)">
                            <view class="device-item">
                                <view class="device-info">
                                    <view class="device-name">{{ item.deviceName }}</view>
                                    <view class="device-details">
                                        <text class="device-type">类型: {{ item.deviceType_dictText || '未知' }}</text>
                                        <text class="device-identifier">标识: {{ item.deviceIdentifier }}</text>
                                    </view>
                                </view>
                                <view class="device-status-time">
                                    <text class="device-status" :class="getStatusClass(item.connectStatus)">
                                        {{ item.connectStatus_dictText || '未知' }}
                                    </text>
                                    <view style="opacity: 0;">1</view>
                                    <view class="device-time">添加日期: {{ item.createTime }}</view>
                                </view>
                            </view>
                        </uni-swipe-action-item>
                    </uni-swipe-action>

                </view>

                <!-- 空状态 -->
                <view class="empty-state" v-if="pageData.list.length === 0">
                    <text>暂无设备数据</text>
                </view>
            </view>
        </z-paging>
        <!-- 弹窗组件 -->
        <save-dialog :show="showDialog" :editData="currentEditData" @update:show="showDialog = $event"
            @submitSuccess="handleSubmitSuccess" @close="handleDialogClose" />
    </view>
</template>

<script setup>
import { nextTick, reactive, ref } from 'vue'
import { onLoad, onNavigationBarButtonTap, onShow } from '@dcloudio/uni-app'
import * as NongchangAPI from '@/api/model/nongchang'
import SaveDialog from './components/save-dialog.vue'

const isOnePage = ref(true)
const paging = ref(null)
const pageData = reactive({
    param: {
        pageNo: 1,
        pageSize: 10,
        deviceName: '',
    },
    list: [],
})

// 滑动操作选项
const swipeOptions = [
    {
        text: '编辑',
        style: {
            backgroundColor: 'var(--fui-color-primary)'
        }
    },
    {
        text: '删除',
        style: {
            backgroundColor: 'var(--fui-color-danger)'
        }
    }
]
function getList() {
    if (!paging.value) return
    NongchangAPI.getDeviceList(pageData.param)
        .then((res) => {
            pageData.total = res.total
            paging.value.complete(res.records)
        })
        .catch(() => {
            paging.value.complete(false)
        })
}
function queryList(pageNo, pageSize) {
    pageData.param.pageNo = pageNo
    pageData.param.pageSize = pageSize
    getList()
}

function handleSearch() {
    // 重置页码为1，重新搜索
    pageData.param.pageNo = 1
    if (paging.value) {
        paging.value.reload()
    }
}
const showDialog = ref(false)
const currentEditData = ref(null)
onNavigationBarButtonTap((_) => {
    showAddDialog()
})
const showAddDialog = () => {
    currentEditData.value = null
    showDialog.value = true
}

const showEditDialog = (device) => {
    currentEditData.value = device
    showDialog.value = true
}


const handleSubmitSuccess = () => {
    // 提交成功后刷新列表
    handleSearch()
}

const handleDialogClose = () => {
    // 弹窗关闭后的处理逻辑
}

// 删除设备
const handleDelete = async (id) => {
    try {
        await uni.showModal({
            title: '确认删除',
            content: '确定要删除这个设备吗？',
            success: async (res) => {
                if (res.confirm) {
                    await NongchangAPI.delDevice( id )
                    uni.showToast({ title: '删除成功', icon: 'success' })
                    handleSearch()
                }
            }
        })
    } catch (error) {
        uni.showToast({ title: '删除失败', icon: 'none' })
    }
}

// 获取状态样式类
const getStatusClass = (status) => {
    if (status === '已连接') return 'status-connected'
    if (status === '未连接') return 'status-disconnected'
    return 'status-unknown'
}

// 处理滑动打开事件
const handleSwipeOpen = (index) => {
    // 关闭其他已打开的滑动单元格，确保同时只有一个处于打开状态
    pageData.list.forEach((item, i) => {
        item.isShow = i === index
    })
}

// 处理滑动操作
const handleSwipeAction = (e, device) => {
    const index = e.index
    const position = e.position
    if (position === 'right') {
        if (index === 0) {
            // 编辑操作
            showEditDialog(device)
        } else if (index === 1) {
            // 删除操作
            handleDelete(device.id)
        }
    }
}
</script>

<style lang="scss">
body {
    background-color: #e6f5e8;
}

.mt-5 {
    margin-top: 10rpx;
}

.mt-11 {
    margin-top: 22rpx;
}

.ml-5 {
    margin-left: 10rpx;
}

.ml-13 {
    margin-left: 26rpx;
}

.ml-9 {
    margin-left: 18rpx;
}

.page {
    background-color: #e6f5e8;
    mix-blend-mode: NOTTHROUGH;
    width: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    height: 100%;

    .section {
        padding: 32rpx 24rpx 32rpx 36rpx;
        background-color: #5db66f;
        mix-blend-mode: NOTTHROUGH;

        .image {
            border-radius: 64rpx;
            width: 108rpx;
            height: 42rpx;
        }

        .group {
            margin-right: 4rpx;

            .image_2 {
                mix-blend-mode: NOTTHROUGH;
                width: 34rpx;
                height: 22rpx;
            }

            .image_3 {
                mix-blend-mode: NOTTHROUGH;
                width: 30rpx;
                height: 22rpx;
            }

            .image_4 {
                width: 48rpx;
                height: 22rpx;
            }
        }

        .group_2 {
            padding-left: 6rpx;

            .image_5 {
                mix-blend-mode: NOTTHROUGH;
                width: 14rpx;
                height: 26rpx;
            }

            .pos {
                position: absolute;
                left: 6rpx;
                top: 50%;
                transform: translateY(-50%);
            }

            .text {
                color: #ffffffe6;
                line-height: 29.6rpx;
            }

            .pos_2 {
                position: absolute;
                right: 0;
                top: 50%;
                transform: translateY(-50%);

                .text_2 {
                    line-height: 25.78rpx;
                }
            }
        }
    }

    .group_3 {
        padding: 28rpx 24rpx 58rpx;

        .section_2 {
            padding: 16rpx 20rpx;
            background-color: #ffffff;
            border-radius: 1998rpx;
            mix-blend-mode: NOTTHROUGH;

            .text_3 {
                color: #cccccc;
                line-height: 26.02rpx;
            }
        }

        .section_3 {
            padding: 0 24rpx;
            background-color: #ffffff;
            border-radius: 18.46rpx;
            mix-blend-mode: NOTTHROUGH;

            .group_4 {
                padding: 42rpx 4rpx 30rpx 22rpx;
                border-bottom: solid 2rpx #eeeeee;

                .image_7 {
                    width: 100rpx;
                    height: 86rpx;
                }

                .group_7 {
                    margin-left: 32rpx;

                    .text_5 {
                        color: #5db66f;
                        font-size: 40rpx;
                        font-family: SourceHanSansCN;
                        line-height: 40rpx;
                    }

                    .text_7 {
                        line-height: 22.18rpx;
                    }
                }

                .text_4 {
                    color: #333333;
                    line-height: 40rpx;
                }

                .group_5 {
                    width: 219.94rpx;

                    .group_6 {
                        overflow: hidden;

                        .text-wrapper_2 {
                            padding: 8rpx 0 4rpx;
                            overflow: hidden;
                            width: 60rpx;
                            height: 40rpx;
                        }
                    }

                    .group_8 {
                        padding-top: 16rpx;

                        .text-wrapper_3 {
                            padding: 8rpx 0 4rpx;
                            overflow: hidden;
                            width: 60rpx;
                            height: 40rpx;

                            .text_8 {
                                color: #ff9800;
                            }
                        }
                    }

                    .text-wrapper {
                        padding: 8rpx 0 4rpx;
                        overflow: hidden;
                        width: 112rpx;
                        height: 40rpx;

                        .text_6 {
                            line-height: 25.74rpx;
                        }
                    }
                }
            }

            .group_9 {
                padding: 12rpx 4rpx;

                .image_8 {
                    margin: 4rpx 0;
                    width: 40rpx;
                    height: 40rpx;
                }

                .text-wrapper_4 {
                    padding: 12rpx 0;
                    overflow: hidden;
                    width: 168rpx;
                    height: 48rpx;
                }

                .image_9 {
                    margin-right: 12rpx;
                    width: 16rpx;
                    height: 26rpx;
                }
            }

            .font_5 {
                font-size: 28rpx;
                font-family: SourceHanSansCN;
                line-height: 25.76rpx;
                color: #5db66f;
            }
        }

        .font_3 {
            font-size: 28rpx;
            font-family: SourceHanSansCN;
            line-height: 25.76rpx;
            color: #1f2937;
        }

        .section_4 {
            padding-left: 6rpx;
            padding-bottom: 26rpx;
            background-color: #ffffff;
            border-radius: 26.28rpx;
            mix-blend-mode: NOTTHROUGH;

            .list {
                margin-left: 18rpx;
                margin-right: 24rpx;

                .list-item {
                    padding: 24rpx 0;
                    border-bottom: solid 2rpx #eeeeee;

                    .section_5 {
                        padding: 12rpx 0 116rpx;
                        background-image: url('/static/images/codefun/4be80e2618f3c4b4aa1ce64fd9063abf.png');
                        background-size: 100% 100%;
                        background-repeat: no-repeat;
                        width: 160rpx;
                        height: 160rpx;

                        .text-wrapper_5 {
                            padding: 8rpx 0;
                            background-image: linear-gradient(90deg, #43cf7c 0%, #5db66f 100%);
                            border-radius: 0rpx 8rpx 8rpx 0rpx;
                            mix-blend-mode: NOTTHROUGH;
                            width: 84rpx;

                            .font_6 {
                                font-size: 20rpx;
                                font-family: SourceHanSansCN;
                                line-height: 18.38rpx;
                                color: #ffffff;
                            }
                        }
                    }

                    .group_10 {
                        margin-right: 40rpx;

                        .text_9 {
                            line-height: 22.34rpx;
                        }

                        .text_10 {
                            line-height: 22.22rpx;
                        }

                        .group_11 {
                            padding-left: 64rpx;

                            .image_10 {
                                width: 28rpx;
                                height: 28rpx;
                            }

                            .text-wrapper_6 {
                                padding-top: 8rpx;
                                overflow: hidden;
                                width: 48rpx;
                                height: 32rpx;

                                .text_11 {
                                    line-height: 22.12rpx;
                                }
                            }

                            .font_8 {
                                font-size: 24rpx;
                                font-family: SourceHanSansCN;
                                line-height: 22.28rpx;
                                color: #f44336;
                            }
                        }
                    }
                }
            }

            .group_14 {
                margin: 18rpx 24rpx 0;

                .text_14 {
                    line-height: 26.12rpx;
                }

                .text-wrapper_8 {
                    padding: 4rpx 0;
                    overflow: hidden;
                    width: 48rpx;
                    height: 32rpx;

                    .text_15 {
                        line-height: 22.2rpx;
                    }
                }
            }

            .font_7 {
                font-size: 24rpx;
                font-family: SourceHanSansCN;
                line-height: 22.28rpx;
                color: #5db66f;
            }

            .group_15 {
                margin-left: 24rpx;
                margin-top: 28rpx;
                width: 528.18rpx;

                .text_16 {
                    line-height: 25.12rpx;
                }

                .text_17 {
                    line-height: 26.26rpx;
                }
            }

            .group_16 {
                margin-left: 24rpx;
                margin-top: 28rpx;
                width: 216.94rpx;

                .text_18 {
                    line-height: 25.82rpx;
                }

                .text_19 {
                    line-height: 25.34rpx;
                }
            }

            .font_9 {
                font-size: 28rpx;
                font-family: SourceHanSansCN;
                line-height: 25.76rpx;
                color: #6b7280;
            }

            .group_17 {
                margin-left: 24rpx;
                margin-top: 28rpx;

                .text_20 {
                    line-height: 26.16rpx;
                }

                .text_21 {
                    line-height: 22.66rpx;
                }
            }

            .divider {
                margin: 28rpx 18rpx 0 24rpx;
                background-color: #f3f4f6;
                height: 2rpx;
            }

            .text_22 {
                margin-left: 24rpx;
                margin-top: 32rpx;
                line-height: 26.12rpx;
            }

            .group_18 {
                margin-left: 24rpx;
                margin-top: 32rpx;

                .text_23 {
                    line-height: 26.06rpx;
                }

                .text_24 {
                    line-height: 22.66rpx;
                }
            }

            .font_10 {
                font-size: 28rpx;
                font-family: SourceHanSansCN;
                line-height: 22.28rpx;
                color: #1f2937;
            }

            .group_19 {
                margin-left: 24rpx;
                margin-top: 28rpx;

                .text_25 {
                    line-height: 25.9rpx;
                }

                .text_26 {
                    line-height: 25.78rpx;
                }
            }

            .group_20 {
                margin-left: 24rpx;
                margin-top: 32rpx;

                .text_27 {
                    line-height: 25.96rpx;
                }

                .text_28 {
                    line-height: 25.88rpx;
                }
            }

            .divider_2 {
                margin-top: 28rpx;
                background-color: #f3f4f6;
                width: 716rpx;
                height: 2rpx;
            }

            .text-wrapper_9 {
                margin-left: 24rpx;
                margin-top: 32rpx;
                padding: 24rpx 0;
                background-color: #5db66f;
                border-radius: 400rpx;
                mix-blend-mode: NOTTHROUGH;

                .text_29 {
                    line-height: 25.9rpx;
                }
            }
        }

        .font_4 {
            font-size: 24rpx;
            font-family: SourceHanSansCN;
            line-height: 22.28rpx;
            color: #555555;
        }
    }

    .image_6 {
        width: 32rpx;
        height: 32rpx;
    }

    .font {
        font-size: 32rpx;
        font-family: SourceHanSansCN;
        line-height: 29.88rpx;
        color: #333333;
    }

    .font_2 {
        font-size: 28rpx;
        font-family: SourceHanSansCN;
        line-height: 25.76rpx;
        color: #ffffff;
    }
}

/* 设备列表样式 */
.device-list {
    margin-top: 20rpx;
}

.device-item {
    background-color: #ffffff;
    border-radius: 16rpx;
    padding: 24rpx;
    display: flex;
    // justify-content: space-between;
    align-items: center;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
}

::v-deep .uni-swipe_box {
    border-radius: 16rpx;
    margin-bottom: 20rpx;
}

.device-info {
    flex: 1;
}

.device-name {
    font-size: 32rpx;
    font-weight: bold;
    color: #333333;
    margin-bottom: 8rpx;
    border-left: 6rpx solid #5DB66F;
    padding-left: 6rpx;
}

.device-details {
    display: flex;
    flex-direction: column;
    gap: 6rpx;
    margin-bottom: 8rpx;
}

.device-type,
.device-identifier {
    font-size: 24rpx;
    color: #666666;
}

.device-status-time {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 6rpx;
    min-width: 180rpx;
}

.device-status {
    font-size: 24rpx;
    font-weight: bold;
}

.device-time {
    font-size: 22rpx;
    color: #999999;
}

.device-actions {
    display: flex;
    flex-direction: column;
    gap: 12rpx;
}

.status-connected {
    color: #5db66f !important;
    font-weight: bold;
}

.status-disconnected {
    color: #f44336 !important;
    font-weight: bold;
}

.status-unknown {
    color: #ff9800 !important;
    font-weight: bold;
}

.empty-state {
    text-align: center;
    padding: 100rpx 0;
    color: #999999;
    font-size: 28rpx;
}
</style>
