<!-- pages/basics/BluetoothDebugging/B13page/B13page.wxml -->
<view class="container">
  <cu-custom bgColor="bg-gradual-blue" isBack="true">
    <view slot="content">{{DevName || 'B13设备'}}</view>
  </cu-custom>

  <!-- 顶部标签：参考主机升级页面的切换方式 -->
  <scroll-view scroll-x class="bg-white nav text-center text-bold text-xl">
    <view class="flex text-center">
      <view class="cu-item {{1==TabCur?'text-blue cur':''}}" bindtap="tabSelect" data-id="1">蓝牙调试</view>
      <view class="cu-item {{2==TabCur?'text-blue cur':''}}" bindtap="tabSelect" data-id="2">蓝牙升级</view>
    </view>
  </scroll-view>

  <!-- Tab: 蓝牙调试 -->
  <view wx:if="{{TabCur==1}}" class="content">
    <!-- 顶部设备分类 -->
    <view class="grid">
      <view class="grid-item" wx:for="{{radarLights}}" wx:key="key">
        <view class="circle {{item.colorClass}}"></view>
        <text class="label">{{item.label}}</text>
      </view>
    </view>

    <!-- 状态功能块 -->
    <view class="cards">
      <view class="card">
        <view class="icon orange"></view>
        <text class="card-title">房间有人</text>
      </view>
      <view class="card">
        <view class="icon gray"></view>
        <text class="card-title">房间无人</text>
      </view>
      <view class="card">
        <view class="icon red"></view>
        <text class="card-title">门开</text>
      </view>

      <view class="card">
        <view class="icon green"></view>
        <text class="card-title">门关</text>
      </view>
      <view class="card">
        <view class="icon blue"></view>
        <text class="card-title">卫浴有人</text>
      </view>
      <view class="card">
        <view class="icon gray"></view>
        <text class="card-title">卫浴无人</text>
      </view>
    </view>

    <!-- 延时滑块 -->
    <view class="slider-card">
      <view class="slider-head">
        <text class="title">开门延时</text>
        <text class="value">{{openDelay}}s</text>
      </view>
      <slider bindchange="onOpenDelayChange" value="{{openDelay}}" min="0" max="60" step="1"/>
    </view>

    <view class="slider-card">
      <view class="slider-head">
        <text class="title">卫浴延时</text>
        <text class="value">{{bathDelay}}s</text>
      </view>
      <slider bindchange="onBathDelayChange" value="{{bathDelay}}" min="0" max="60" step="1"/>
    </view>

    <!-- 通讯日志 -->
    <view class="log-card">
      <view class="log-head">
        <text class="title">通讯日志</text>
        <text class="action" bindtap="onClearLogs">清空</text>
      </view>

      <view class="log-options">
        <checkbox-group class="option" bindchange="onCheckboxChange" data-key="hexSend">
          <label><checkbox value="hexSend" checked="{{hexSend}}" />HEX发送</label>
        </checkbox-group>
        <checkbox-group class="option" bindchange="onCheckboxChange" data-key="hexShow">
          <label><checkbox value="hexShow" checked="{{hexShow}}" />HEX显示</label>
        </checkbox-group>
        <checkbox-group class="option" bindchange="onCheckboxChange" data-key="withTimestamp">
          <label><checkbox value="withTimestamp" checked="{{withTimestamp}}" />时间戳</label>
        </checkbox-group>
        <checkbox-group class="option" bindchange="onCheckboxChange" data-key="wrapCRLF">
          <label><checkbox value="wrapCRLF" checked="{{wrapCRLF}}" />回车换行</label>
        </checkbox-group>
      </view>

      <view class="log-input-row">
        <textarea class="log-input" placeholder="输入要发送的数据" value="{{sendText}}" bindinput="onInputChange" disable-default-padding="true" />
        <button class="send-btn" size="mini" type="primary" bindtap="onSend">发送</button>
      </view>

      <scroll-view class="log-scroll" scroll-y="true">
        <block wx:if="{{logList.length > 0}}">
          <view class="log-item" wx:for="{{logList}}" wx:key="id">
            <text class="log-time">{{item.time}}</text>
            <text class="log-text">{{item.text}}</text>
          </view>
        </block>
        <view class="log-empty" wx:else>暂无日志记录</view>
      </scroll-view>
    </view>
  </view>

  <!-- Tab: 蓝牙升级 -->
  <view wx:if="{{TabCur==2}}" class="content">
    <view class="device-row">
      <view class="device-left">
        <view class="device-line">
          <text class="dr-label">蓝牙名称：</text>
          <text class="dr-value">{{bleName || '-'}}</text>
        </view>
        <view class="device-line">
          <text class="dr-label">MAC：</text>
          <text class="dr-value">{{bleMac || '-'}}</text>
          <text class="dr-label">版本：</text>
          <text class="dr-value">{{bleVersion || '-'}}</text>
        </view>
      </view>
      <view class="dr-btn dr-btn-view" bindtap="onSendReadVersion">读取蓝牙信息</view>
    </view>

    <!-- 功能栏（保留导出/导入） -->
    <view class="cfg-card">
      <view class="toolbar">
        <view class="import-box">{{importFileName || '未选择文件'}}</view>
        <view class="toolbar-actions">
          <button size="mini" type="default" bindtap="onExport">导出文件</button>
          <button size="mini" type="default" bindtap="onImport">导入文件</button>
          <button size="mini" type="primary" bindtap="onOneKeySend">一键下发</button>
        </view>
      </view>
    </view>

    <!-- 端口配置表 -->
    <view class="cfg-card">
      <view class="cfg-head head-row">
        <text>端口配置</text>
        <view class="head-actions">
          <button size="mini" type="default" bindtap="onReadPorts">读取配置</button>
          <button size="mini" type="primary" bindtap="onSavePorts">保存配置</button>
        </view>
      </view>
      <view class="table header port">
        <view class="tr">
          <view class="th">端口</view>
          <view class="th">主机输入端口</view>
          <view class="th">无人至有人阈值</view>
          <view class="th">有人至无人阈值</view>
          <view class="th">启用</view>
          <view class="th">检测时间</view>
          <view class="th">检测时间单位</view>
        </view>
      </view>
      <view class="table body port">
        <view class="tr" wx:for="{{ports}}" wx:key="name">
          <view class="td">{{item.portLabel}}</view>
          <view class="td">
            <input class="picker-text" placeholder="输入端口" value="{{item.loop}}" type="number" bindinput="onPortNumberInput" data-idx="{{index}}" data-field="loop" />
          </view>
          <view class="td">
            <input class="picker-text" placeholder="无人→有人阈值" value="{{item.thresholdUp}}" type="number" bindinput="onPortNumberInput" data-idx="{{index}}" data-field="thresholdUp" />
          </view>
          <view class="td">
            <input class="picker-text" placeholder="有人→无人阈值" value="{{item.thresholdDown}}" type="number" bindinput="onPortNumberInput" data-idx="{{index}}" data-field="thresholdDown" />
          </view>
          <view class="td">
            <switch checked="{{item.enabled}}" bindchange="onPortSwitch" data-idx="{{index}}" data-field="enabled" />
          </view>
          <view class="td">
            <input class="picker-text" placeholder="检测时间" value="{{item.detectTime}}" type="number" bindinput="onPortNumberInput" data-idx="{{index}}" data-field="detectTime" />
          </view>
          <view class="td">
            <picker mode="selector" range="{{timeUnits}}" value="{{item.detectUnit}}" bindchange="onPortUnitChange" data-idx="{{index}}" data-field="detectUnit">
              <view class="picker-text">{{timeUnits[item.detectUnit]}}</view>
            </picker>
          </view>
        </view>
      </view>
    </view>

    <!-- 条件配置（二级菜单：按组折叠） -->
    <view class="cfg-card cond-card">
      <view class="cfg-head head-row">
        <text>条件配置</text>
        <view class="head-actions">
          <button size="mini" type="warn" bindtap="onDeleteCondGroup">删除条件组</button>
          <button size="mini" type="warn" bindtap="onDeleteCondition">删除条件</button>
          <button size="mini" type="primary" bindtap="onAddCondGroup">添加条件组</button>
          <button size="mini" type="primary" bindtap="onAddCondition">添加条件</button>
        </view>
      </view>
      <scroll-view scroll-y class="cond-scroll">
        <view class="cond-groups">
          <view class="group-card" wx:for="{{condGroups}}" wx:key="group" wx:for-item="grp" wx:for-index="gidx">
          <view class="group-head head-row">
            <view class="group-info">
              <text class="group-title">条件组：</text>
              <text class="group-seq">{{grp.group}}</text>
              <view class="group-time">
                <text class="label">超时：</text>
                <picker class="timeout-picker" mode="selector" range="{{ grp.timeoutUnit === 0 ? hourValues : msValues }}" value="{{ (grp.timeout || 1) - 1 }}" bindchange="onGroupPickerChange" data-idx="{{gidx}}" data-field="timeout">
                  <view class="picker-text">{{grp.timeout || 1}}</view>
                </picker>
                <text class="label">单位：</text>
                <picker class="unit-picker" mode="selector" range="{{timeUnits}}" value="{{grp.timeoutUnit}}" bindchange="onGroupPickerChange" data-idx="{{gidx}}" data-field="timeoutUnit">
                  <view class="picker-text">{{timeUnits[grp.timeoutUnit]}}</view>
                </picker>
              </view>
            </view>
            <view class="head-actions">
              <button size="mini" type="default" bindtap="onToggleGroup" data-idx="{{gidx}}">{{grp.expanded?'收起':'展开'}}</button>
            </view>
          </view>

          <view wx:if="{{grp.expanded}}">
            <view class="table body cond-item">
              <block wx:for="{{grp.items}}" wx:key="seq" wx:for-item="it" wx:for-index="iidx">
                <view class="cond-item-card">
                  <!-- 第一行：序号(只读)、持续判定时间(下拉选择)、单位(下拉选择) -->
                  <view class="tr tr-top">
                    <view class="td">
                      <text class="label">条件序号：</text>
                      <text class="seq-value">{{it.seq}}</text>
                    </view>
                    <view class="td">
                      <text class="label">持续判定时间：</text>
                      <picker mode="selector" range="{{ it.judgeUnit === 0 ? hourValues : msValues }}" value="{{ (it.judgeTime || 1) - 1 }}" bindchange="onItemPickerChange" data-gidx="{{gidx}}" data-iidx="{{iidx}}" data-field="judgeTime">
                        <view class="picker-text">{{it.judgeTime || 1}}</view>
                      </picker>
                    </view>
                    <view class="td">
                      <text class="label">单位：</text>
                      <picker mode="selector" range="{{timeUnits}}" value="{{it.judgeUnit}}" bindchange="onItemPickerChange" data-gidx="{{gidx}}" data-iidx="{{iidx}}" data-field="judgeUnit">
                        <view class="picker-text">{{timeUnits[it.judgeUnit]}}</view>
                      </picker>
                    </view>
                  </view>
                  <!-- 第二行：标题 -->
                  <view class="tr tr-mid">
                    <view class="th">有无人标记</view>
                    <view class="th">有卡取电</view>
                    <view class="th">开门磁</view>
                    <view class="th">门口红外</view>
                    <view class="th">卫红外</view>
                    <view class="th">浴红外</view>
                  </view>
                  <!-- 第三行：对应值 -->
                  <view class="tr tr-bottom">
                    <view class="td">
                      <picker mode="selector" range="{{tagOptions}}" value="{{it.tag}}" bindchange="onItemPickerChange" data-gidx="{{gidx}}" data-iidx="{{iidx}}" data-field="tag">
                        <view class="picker-text">{{tagOptions[it.tag]}}</view>
                      </picker>
                    </view>
                    <view class="td">
                      <picker mode="selector" range="{{stateOptions}}" value="{{it.cardPower || 0}}" bindchange="onItemPickerChange" data-gidx="{{gidx}}" data-iidx="{{iidx}}" data-field="cardPower">
                        <view class="picker-text">{{stateOptions[it.cardPower || 0]}}</view>
                      </picker>
                    </view>
                    <view class="td">
                      <picker mode="selector" range="{{stateOptions}}" value="{{it.doorMag}}" bindchange="onItemPickerChange" data-gidx="{{gidx}}" data-iidx="{{iidx}}" data-field="doorMag">
                        <view class="picker-text">{{stateOptions[it.doorMag]}}</view>
                      </picker>
                    </view>
                    <view class="td">
                      <picker mode="selector" range="{{stateOptions}}" value="{{it.irHall}}" bindchange="onItemPickerChange" data-gidx="{{gidx}}" data-iidx="{{iidx}}" data-field="irHall">
                        <view class="picker-text">{{stateOptions[it.irHall]}}</view>
                      </picker>
                    </view>
                    <view class="td">
                      <picker mode="selector" range="{{stateOptions}}" value="{{it.bathRadar}}" bindchange="onItemPickerChange" data-gidx="{{gidx}}" data-iidx="{{iidx}}" data-field="bathRadar">
                        <view class="picker-text">{{stateOptions[it.bathRadar]}}</view>
                      </picker>
                    </view>
                    <view class="td">
                      <picker mode="selector" range="{{stateOptions}}" value="{{it.bathroomRadar}}" bindchange="onItemPickerChange" data-gidx="{{gidx}}" data-iidx="{{iidx}}" data-field="bathroomRadar">
                        <view class="picker-text">{{stateOptions[it.bathroomRadar]}}</view>
                      </picker>
                    </view>
                  </view>
                </view>
              </block>
            </view>
          </view>
          </view>
        </view>
      </scroll-view>
    </view>
    
  </view>
</view>
