<script setup>
import { reactive, ref, computed } from 'vue'
import { onLoad, onNavigationBarButtonTap } from '@dcloudio/uni-app'
import SaveDialog from './components/save-dialog.vue'
import * as NongchangAPI from '@/api/model/nongchang'
import { useDictStore } from '@/store/modules/dict'
import { merge } from 'lodash-es'

const dictStore = useDictStore()
const deviceType = ref('') // 设备类型参数
const pageData = reactive({
    param: {
        deviceName: '',
        deviceType: '', // 添加设备类型筛选
        farmBaseId: '', //基地id
    },
    list: [],
    selectedCategory: 'all',
    selectedDevice: {},
})

// 摄像头设备的分类标签
const cameraCategories = computed(() => {
    const dictList =
        dictStore.getDictList.deviceType?.map((item) => ({
            id: item.value,
            name: item.text,
        })) || []
    return [{ id: 'all', name: '全部' }, ...dictList]
})

onLoad((options) => {
    // 获取设备类型参数
    if (options.deviceType) {
        deviceType.value = options.deviceType
        pageData.selectedCategory = options.deviceType
        pageData.param.deviceType = options.deviceType
        pageData.param.farmBaseId = options.farmBaseId
    }
    getList()
})
// 判断是否为摄像头设备
const isCameraDevice = computed(() => {
    return deviceType.value == '1'
})

// 获取设备列表
function getList() {
    const formData = {
        deviceName: pageData.param.deviceName,
        deviceType: pageData.param.deviceType,
        farmBaseId: pageData.param.farmBaseId,
    }

    NongchangAPI.queryByType(formData)
        .then((res) => {
            pageData.list = res.result || []
            if (res.result.length > 0) handleDeviceClick(res.result[0])
        })
        .catch(() => {
            pageData.list = []
        })
}

function handleSearch() {
    // 搜索时重新获取列表
    getList()
}

const showDialog = ref(false)
const currentEditData = ref(null)

onNavigationBarButtonTap((_) => {
    showAddDialog()
})

function showAddDialog() {
    currentEditData.value = null
    showDialog.value = true
}

function handleSubmitSuccess() {
    // 提交成功后刷新列表
    getList()
}

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

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

// 分类标签点击
function handleCategoryClick(category) {
    pageData.selectedCategory = category.id
    if (category.id === 'all') {
        pageData.param.deviceType = ''
    } else {
        pageData.param.deviceType = category.id
    }
    getList()
}

// 设备卡片点击
function handleDeviceClick(device) {
    pageData.selectedDevice = device
    deviceType.value = device.deviceType
}
</script>

<template>
    <view class="codefun-flex-col page">
        <!-- 顶部占位区域 - 根据设备类型显示 -->
        <!-- 摄像头设备 - 视频占位区域 -->
        <view v-if="isCameraDevice" class="video-placeholder-top">
            <view class="video-overlay">
                <view class="video-info">
                    <text class="video-location">{{ pageData.selectedDevice.deviceName }}</text>
                    <!-- <text class="video-time">2022年11月01日 星期二 14:53:24</text> -->
                </view>
                <image class="fullscreen-icon" src="/static/images/device/fullscreen.png" mode="aspectFit" />
            </view>
        </view>

        <!-- 其他设备 - 设备信息卡片 -->
        <view v-else class="device-info-card">
            <view class="device-header">
                <view class="device-icon">
                    <image class="icon-img" src="/static/images/device/location-icon.png" mode="aspectFit" />
                </view>
                <view class="device-title-group">
                    <view class="device-title">{{ pageData.selectedDevice.deviceName }}</view>
                    <view class="device-badges">
                        <!-- <text class="badge badge-green" v-if="pageData.selectedDevice.isOnline == 1">在线</text>
                        <text class="badge badge-gray" v-else>离线</text> -->
						<view class="online-status online-green">
							<view class="status-point"></view>
							<text>在线</text>
						</view>
						<view class="online-status">
							<view class="status-point"></view>
							<text>离线</text>
						</view>
                    </view>
                </view>
            </view>
            <view class="device-stats">
                <view class="stat-item">
                    <text class="stat-label">设备ID：</text>
                    <text class="stat-value">{{ pageData.selectedDevice.deviceIdentifier }}</text>
                </view>
                <view class="stat-item">
                    <text class="stat-label">位置坐标：</text>
                    <text class="stat-value">30.2680, 120.0450</text>
                </view>
            </view>
            <view class="device-stats">
                <view class="stat-item">
                    <text class="stat-label">设备类型：</text>
                    <text class="stat-value">灌溉设备</text>
                </view>
                <view class="stat-item">
                    <text class="stat-label">归属地：</text>
                    <text class="stat-value">西区油茶基地</text>
                </view>
            </view>
        </view>

        <!-- 通用设备列表区域 -->
        <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="isCameraDevice ? '请输入关键词搜索' : '请输入设备名称搜索'"
                    border="none"
                    class="codefun-ml-8"
                    @confirm="handleSearch"
                /> -->
				<u-input
				    v-model="pageData.param.deviceName"
				    placeholder="请输入搜索内容"
				    border="none"
				    class="codefun-ml-8"
				    @confirm="handleSearch"
				/>
            </view>

            <!-- 分类标签 -->
            <view class="category-tabs">
                <view
                    v-for="category in cameraCategories"
                    :key="category.id"
                    class="category-tab"
                    :class="{ active: pageData.selectedCategory === category.id }"
                    @click="handleCategoryClick(category)"
                >
                    {{ category.name }}
                </view>
            </view>

            <!-- 设备卡片网格 -->
            <view class="device-grid">
                <view
                    v-for="(item, index) in pageData.list"
                    :key="index"
                    class="device-card"
                    :class="{ selected: pageData.selectedDevice.id == item.id }"
                    @click="handleDeviceClick(item)"
                >
                    <view class="">
                        <view class="device-name">{{ item.deviceName }}</view>
                        <!-- <view class="device-status" :class="getStatusClass(item.connectStatus_dictText)">
                            {{ item.connectStatus_dictText || '未知' }}
                        </view> -->
                    </view>
                    <!-- <view class="device-card-body">
                        <view class="device-detail">
                            <text class="detail-label">设备类型：</text>
                            <text class="detail-value">{{ item.deviceType_dictText || '未知' }}</text>
                        </view>
                        <view class="device-detail">
                            <text class="detail-label">设备编号：</text>
                            <text class="detail-value">{{ item.deviceIdentifier }}</text>
                        </view>
                    </view> -->
                </view>
            </view>

            <!-- 空状态 -->
            <view class="empty-state" v-if="pageData.list.length === 0">
                <text>暂无设备数据</text>
            </view>
        </view>

        <!-- 弹窗组件 -->
        <SaveDialog
            :show="showDialog"
            :editData="currentEditData"
            @update:show="showDialog = $event"
            @submit-success="handleSubmitSuccess"
            @close="handleDialogClose"
        />
    </view>
</template>

<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 {
    border-radius: 16rpx;
    margin-bottom: 20rpx;
}

.device-info {
    flex: 1;
}

.device-name {
    font-size: 28rpx;
    font-weight: 400;
    letter-spacing: 0px;
    text-align: center;
}

.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;
}

/* 分类标签样式 */
.category-tabs {
    display: flex;
    gap: 16rpx;
    padding: 24rpx 0;
    overflow-x: auto;
    white-space: nowrap;
}

.category-tab {
    padding: 12rpx 32rpx;
    background-color: #ffffff;
    border-radius: 40rpx;
    font-size: 28rpx;
    color: #333333;
    cursor: pointer;
    flex-shrink: 0;
    border: 2rpx solid transparent;
    transition: all 0.3s;

    &.active {
        background-color: #5db66f;
        color: #ffffff;
        font-weight: bold;
    }
}

/* 摄像头设备样式 */
/* 顶部视频占位区域 */
.video-placeholder-top {
    position: relative;
    width: 100%;
    height: 400rpx;
    //background-image: url('/static/images/device/video-bg.jpg');
    background-size: cover;
    background-position: center;
    flex-shrink: 0;
}

.video-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 20rpx 24rpx;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.6), transparent);
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
}

.video-info {
    display: flex;
    flex-direction: column;
    gap: 8rpx;
}

.video-location {
    color: #ffffff;
    font-size: 32rpx;
    font-weight: bold;
    text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.3);
}

.video-time {
    color: #ffffff;
    font-size: 24rpx;
    opacity: 0.9;
    text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.3);
}

.fullscreen-icon {
    width: 48rpx;
    height: 48rpx;
    opacity: 0.9;
}

/* 设备信息卡片样式 */
.device-info-card {
    margin: 24rpx 24rpx 0rpx 24rpx;
    padding: 32rpx 24rpx;
    background-color: #ffffff;
    border-radius: 16rpx;
    box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
}

.device-header {
    display: flex;
    align-items: center;
    gap: 20rpx;
    margin-bottom: 24rpx;
}

.device-icon {
    width: 80rpx;
    height: 80rpx;
    background-color: #e8f5e9;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.icon-img {
    width: 48rpx;
    height: 48rpx;
}

.device-title-group {
	display: flex;
	align-items: center;
}

.device-title {
    font-size: 32rpx;
    font-weight: bold;
    color: #333333;
    margin-bottom: 12rpx;
}

.device-badges {
    display: flex;
	align-items: center;
	margin-left: 16rpx;
	.online-status{
		width: 84rpx;
		height: 36rpx;
		opacity: 1;
		border-radius: 4rpx;
		background: rgba(159, 174, 181, 0.1);
		
		font-size: 24rpx;
		font-weight: 400;
		letter-spacing: 0px;
		color: #9FAEB5;
		text-align: center;
		margin-right: 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		.status-point{
			width: 12rpx;
			height: 12rpx;
			background: #9FAEB5;
			border-radius: 12rpx;
			margin-right: 8rpx;
		}
	}
	.online-green{
		color: #43CF7C;
		.status-point{
			background: #43CF7C;
		}
	}
}

.badge {
    padding: 6rpx 16rpx;
    border-radius: 20rpx;
    font-size: 24rpx;
}

.badge-green {
    background-color: #5db66f;
    color: #ffffff;
}

.badge-gray {
    background-color: #e0e0e0;
    color: #666666;
}

.device-stats {
    display: flex;
    justify-content: space-between;
    margin-bottom: 16rpx;
	padding-right: 26rpx;
    &:last-child {
        margin-bottom: 0;
    }
}

.stat-item {
    flex: 1;
    display: flex;
    align-items: center;
}

.stat-label {
    font-size: 24rpx;
    color: #999999;
}

.stat-value {
    font-size: 24rpx;
    color: #666666;
}

.device-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20rpx;
    margin-top: 20rpx;
}

.device-card {
    background-color: #ffffff;
    border-radius: 16rpx;
    padding: 24rpx;
    box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
    cursor: pointer;
    transition: all 0.3s;
    border: 2rpx solid transparent;

    &.selected {
        border-color: #5db66f;
        box-shadow: 0 4rpx 12rpx rgba(93, 182, 111, 0.3);
        background: #5DB66F;
        color: #FFFFFF !important;
    }
}

.device-card-header {
    display: flex;
    flex-direction: column;
    gap: 8rpx;
    margin-bottom: 16rpx;
    border-bottom: 2rpx solid #f0f0f0;
    padding-bottom: 16rpx;

    .device-name {
        font-size: 28rpx;
        font-weight: bold;
        color: #333333;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

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

.device-card-body {
    display: flex;
    flex-direction: column;
    gap: 12rpx;

    .device-detail {
        font-size: 22rpx;
        display: flex;
        flex-direction: column;
        gap: 4rpx;

        .detail-label {
            color: #999999;
        }

        .detail-value {
            color: #666666;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
    }
}
</style>
