<navigation-bar background="{{themeMode === 'dark' ? '#111827' : '#FFF'}}"></navigation-bar>
<view wx:if="{{toastText}}" class="page-toast page-toast--{{toastType}} {{themeClass}}">
  {{toastText}}
</view>
<view class="subpage-fixed-header subpage-fixed-header--generic {{themeClass}}">
  <view class="subpage-page-header">
    <view wx:if="{{activeParamView == 'parameterGroups'}}" class="panel-actions subpage-actions generic-protocol-actions">
      <view class="panel-action-button {{isStorageAccessProtocol && !connectedDevice ? 'is-disabled' : ''}}" bindtap="syncParameterGroups">同步</view>
      <view class="panel-action-button {{isStorageAccessProtocol && !connectedDevice ? 'is-disabled' : ''}} {{parameterAutoPollEnabled ? 'is-active' : ''}}" bindtap="toggleParameterPolling">轮询</view>
      <view class="panel-action-button" bindtap="saveParameterGroupsJson">保存</view>
      <view class="panel-action-button" bindtap="importParameterGroupsJson">加载</view>
      <view wx:if="{{isStorageAccessProtocol}}" class="panel-action-button" bindtap="completeParameterStructs">结构</view>
      <view wx:if="{{isModbusProtocol}}" class="panel-action-button panel-action-button--icon" bindtap="openParameterDraft">+</view>
    </view>
    <view wx:elif="{{activeParamView == 'parameterGroup' && isModbusProtocol}}" class="panel-actions subpage-actions">
      <view
        class="panel-action-button {{connectedDevice && !activeParameterGroup.addressOverflow ? '' : 'is-disabled'}}"
        data-group-id="{{activeParameterGroup.id}}"
        bindtap="readParameterGroup"
      >读取</view>
      <view
        wx:if="{{activeParameterGroup.writable}}"
        class="panel-action-button {{connectedDevice && !activeParameterGroup.addressOverflow ? '' : 'is-disabled'}}"
        data-group-id="{{activeParameterGroup.id}}"
        bindtap="writeParameterGroup"
      >写入</view>
      <view class="panel-action-button" bindtap="backToParamsHome">返回</view>
    </view>
  </view>
</view>
<scroll-view class="scrollarea {{themeClass}} scrollarea--subpage scrollarea--generic" scroll-y type="list">
  <view class="page-shell">
    <block wx:if="{{activeParamView == 'parameterGroups'}}">
      <view wx:if="{{isStorageAccessProtocol}}" class="panel storage-code-info-card">
        <view class="storage-code-info-top">
          <text class="storage-code-info-model">{{storageCodeInfoCard.model}}</text>
        </view>
        <view class="storage-code-info-bottom">
          <text class="storage-code-info-chip">{{storageCodeInfoCard.chipModel}}</text>
          <view class="storage-code-info-metrics">
            <view wx:for="{{storageCodeInfoCard.metricItems}}" wx:for-item="item" wx:key="text" class="storage-code-info-metric">{{item.text}}</view>
          </view>
        </view>
      </view>
      <view
        wx:for="{{parameterGroups}}"
        wx:for-item="group"
        wx:key="id"
        class="generic-group-shell {{group.deleteVisible ? 'is-delete-visible' : ''}}"
      >
        <view
          wx:if="{{!group.expanded && group.deleteVisible}}"
          class="generic-delete-action"
          data-group-id="{{group.id}}"
          bindtap="deleteParameterGroup"
        >
          -
        </view>
        <view
          class="panel generic-group-panel {{group.expanded ? '' : 'panel--collapsed'}}"
          data-group-id="{{group.id}}"
          bindtouchstart="onParameterGroupTouchStart"
          bindtouchend="onParameterGroupTouchEnd"
        >
          <view class="panel-header panel-header--with-actions">
            <view
              class="panel-heading-toggle"
              data-group-id="{{group.id}}"
              bindtap="openParameterGroup"
            >
              <view class="panel-icon icon-terminal">
                <image class="panel-icon-image" src="/assets/icons/terminal-white.png" mode="aspectFit" />
              </view>
              <view class="generic-group-title-wrap">
                <view class="panel-title" data-group-id="{{group.id}}" catchlongpress="openParameterGroupEdit">{{group.displayName || group.name}}</view>
                <view class="param-meta generic-group-meta">{{group.listMetaText || group.addressRangeText}}</view>
              </view>
            </view>
            <view wx:if="{{isModbusProtocol}}" class="panel-actions generic-group-actions">
              <view
                class="panel-action-button {{connectedDevice && !group.addressOverflow ? '' : 'is-disabled'}}"
                data-group-id="{{group.id}}"
                bindtap="readParameterGroup"
              >
                读取
              </view>
              <view
                wx:if="{{group.writable}}"
                class="panel-action-button {{connectedDevice && !group.addressOverflow ? '' : 'is-disabled'}}"
                data-group-id="{{group.id}}"
                bindtap="writeParameterGroup"
              >
                写入
              </view>
              <view class="entry-chevron"></view>
            </view>
          </view>

        </view>
      </view>
    </block>

    <block wx:elif="{{activeParamView == 'parameterGroup'}}">
      <view wx:if="{{activeParameterGroup}}" class="panel generic-group-detail-panel">
        <view class="generic-group-detail-header">
          <view class="panel-title">{{activeParameterGroup.detailTitleText || activeParameterGroup.displayName || activeParameterGroup.name}}</view>
        </view>
        <view class="generic-group-detail-meta">
          {{activeParameterGroup.detailMetaText || activeParameterGroup.addressRangeText}}
        </view>
        <view
          wx:for="{{activeParameterRegisterRows.length ? activeParameterRegisterRows : activeParameterGroup.registers}}"
          wx:for-item="register"
          wx:for-index="registerIndex"
          wx:key="id"
          class="generic-register-row {{register.dragClass}}"
          style="{{register.dragStyle}}"
        >
          <view
            wx:if="{{!activeParameterGroup.isStructLayout}}"
            class="generic-register-drag-handle {{register.dragHandleClass}}"
            data-group-id="{{activeParameterGroup.id}}"
            data-index="{{register.sourceIndex !== undefined ? register.sourceIndex : registerIndex}}"
            catchtouchstart="onParameterRegisterDragStart"
            catchtouchmove="onParameterRegisterDragMove"
            catchtouchend="onParameterRegisterDragEnd"
            catchtouchcancel="onParameterRegisterDragCancel"
          >
            <view class="generic-register-drag-bar"></view>
            <view class="generic-register-drag-bar"></view>
            <view class="generic-register-drag-bar"></view>
          </view>
          <view wx:else class="generic-register-layout-spacer"></view>
          <view class="generic-register-main">
            <view
              class="generic-register-name"
              data-group-id="{{activeParameterGroup.id}}"
              data-index="{{register.sourceIndex !== undefined ? register.sourceIndex : registerIndex}}"
              bindtap="openParameterRegisterInfo"
              catchlongpress="openParameterRegisterEdit"
            >
              {{register.name}}
            </view>
            <view class="generic-register-meta">
              <text>{{register.metaText || (register.addressText + ' ' + register.rawValueText)}}</text>
            </view>
          </view>
          <view class="generic-register-input-wrap {{register.showUnit && register.unit ? 'generic-register-input-wrap--unit' : ''}}">
            <block wx:if="{{activeParameterGroup.writable}}">
              <block wx:if="{{register.conversionFormula}}">
                <view class="param-value generic-readonly-value">{{register.displayValue || '--'}}{{register.showUnit && register.unit ? ' ' + register.unit : ''}}</view>
              </block>
              <block wx:else>
                <input
                  class="value-input generic-register-value {{register.isDirty ? 'value-input--dirty' : ''}}"
                  placeholder="--"
                  data-group-id="{{activeParameterGroup.id}}"
                  data-index="{{register.sourceIndex !== undefined ? register.sourceIndex : registerIndex}}"
                  value="{{register.inputValue}}"
                  bindinput="onParameterRegisterValueInput"
                  bindblur="onParameterRegisterValueBlur"
                />
                <view wx:if="{{register.showUnit && register.unit}}" class="generic-register-unit">{{register.unit}}</view>
              </block>
            </block>
            <view wx:else class="param-value generic-readonly-value">{{register.displayValue || '--'}}{{register.showUnit && register.unit ? ' ' + register.unit : ''}}</view>
            <view wx:if="{{register.displayMetaText}}" class="generic-register-display-meta">{{register.displayMetaText}}</view>
          </view>
        </view>
      </view>
    </block>

  </view>
</scroll-view>
<view wx:if="{{parameterDialog.visible}}" class="generic-dialog-mask {{themeClass}}" bindtap="closeParameterDraft">
  <view class="generic-dialog" catchtap="noop">
    <view class="generic-dialog-header">
      <view class="generic-dialog-title">{{parameterDialog.title}}</view>
      <view class="generic-dialog-close" bindtap="closeParameterDraft">×</view>
    </view>

    <block wx:if="{{parameterDialog.mode == 'createGroup' || parameterDialog.mode == 'editGroup'}}">
      <view class="generic-dialog-body">
        <view class="generic-config-row">
          <view class="param-main">
            <view class="param-name">寄存器组名</view>
            <view class="param-meta">每组寄存器地址连续</view>
          </view>
          <input
            class="value-input generic-value-input"
            data-field="groupName"
            value="{{parameterDialog.groupName}}"
            bindinput="onParameterDraftInput"
          />
        </view>
        <view class="generic-config-row">
          <view class="param-main">
            <view class="param-name">寄存器类型</view>
            <view class="param-meta">决定读取功能码与是否可写</view>
          </view>
          <picker
            mode="selector"
            range="{{parameterRegisterTypeOptions}}"
            range-key="label"
            value="{{parameterDialog.registerTypeIndex}}"
            bindchange="onParameterDraftTypeChange"
          >
            <view class="generic-picker-value">{{parameterDialog.registerTypeText}}</view>
          </picker>
        </view>
        <view class="generic-config-row">
          <view class="param-main">
            <view class="param-name">寄存器起始地址</view>
            <view class="param-meta">16进制，例如 00A0</view>
          </view>
          <input
            class="value-input generic-value-input"
            data-field="startAddress"
            value="{{parameterDialog.startAddress}}"
            bindinput="onParameterDraftInput"
          />
        </view>
        <view class="generic-config-row">
          <view class="param-main">
            <view class="param-name">寄存器数量</view>
            <view class="param-meta">{{parameterDialog.structParsedSummary || '1 - 256'}}</view>
          </view>
          <input
            class="value-input generic-value-input"
            type="number"
            data-field="quantity"
            value="{{parameterDialog.quantity}}"
            bindinput="onParameterDraftInput"
          />
        </view>
        <view wx:if="{{parameterDialog.mode == 'createGroup'}}" class="generic-struct-section">
          <view class="generic-struct-header">
            <view class="param-main">
              <view class="param-name">结构体定义</view>
              <view class="param-meta">支持 typedef struct、typedef 别名与数组</view>
            </view>
            <view class="panel-action-button" bindtap="parseParameterStructDefinition">解析</view>
          </view>
          <textarea
            class="generic-struct-input"
            maxlength="-1"
            placeholder="粘贴 C 结构体定义"
            data-field="structDefinition"
            value="{{parameterDialog.structDefinition}}"
            bindinput="onParameterDraftInput"
          />
        </view>
      </view>
    </block>

    <block wx:elif="{{parameterDialog.mode == 'editRegister' || parameterDialog.mode == 'viewRegister'}}">
      <view class="generic-dialog-body">
        <view class="generic-info-stack">
          <view class="generic-info-row">
            <view class="generic-info-label">名称</view>
            <view wx:if="{{parameterDialog.mode == 'viewRegister'}}" class="generic-info-value">{{parameterDialog.name}}</view>
            <input wx:else class="value-input generic-value-input" data-field="name" value="{{parameterDialog.name}}" bindinput="onParameterDraftInput" />
          </view>
          <view class="generic-info-row">
            <view class="generic-info-label">地址</view>
            <view class="generic-info-value">{{parameterDialog.addressText}}</view>
          </view>
          <view wx:if="{{parameterDialog.sourceMetaText}}" class="generic-info-row">
            <view class="generic-info-label">来源</view>
            <view class="generic-info-value">{{parameterDialog.sourceMetaText}}</view>
          </view>
          <view wx:if="{{parameterDialog.showDataType}}" class="generic-info-row">
            <view class="generic-info-label">类型</view>
            <view wx:if="{{parameterDialog.mode == 'viewRegister'}}" class="generic-info-value">{{parameterDialog.dataTypeText}}</view>
            <picker wx:else mode="selector" range="{{parameterDataTypeOptions}}" range-key="label" value="{{parameterDialog.dataTypeIndex}}" bindchange="onParameterDialogDataTypeChange">
              <view class="generic-picker-value">{{parameterDialog.dataTypeText}}</view>
            </picker>
          </view>
          <view wx:if="{{parameterDialog.showTextLength}}" class="generic-info-row">
            <view class="generic-info-label">长度</view>
            <view wx:if="{{parameterDialog.mode == 'viewRegister'}}" class="generic-info-value">{{parameterDialog.textByteLength || '--'}}B</view>
            <input wx:else class="value-input generic-value-input" type="number" data-field="textByteLength" value="{{parameterDialog.textByteLength}}" bindinput="onParameterDraftInput" />
          </view>
          <view class="generic-info-row">
            <view class="generic-info-label">备注</view>
            <view wx:if="{{parameterDialog.mode == 'viewRegister'}}" class="generic-info-value">{{parameterDialog.remark || '--'}}</view>
            <input wx:else class="value-input generic-value-input" data-field="remark" value="{{parameterDialog.remark}}" bindinput="onParameterDraftInput" />
          </view>
          <view wx:if="{{parameterDialog.showUnit}}" class="generic-info-row">
            <view class="generic-info-label">单位</view>
            <view wx:if="{{parameterDialog.mode == 'viewRegister'}}" class="generic-info-value">{{parameterDialog.unit || '--'}}</view>
            <input wx:else class="value-input generic-value-input" data-field="unit" value="{{parameterDialog.unit}}" bindinput="onParameterDraftInput" />
          </view>
          <view wx:if="{{parameterDialog.showDataType}}" class="generic-info-row">
            <view class="generic-info-label">公式</view>
            <view wx:if="{{parameterDialog.mode == 'viewRegister'}}" class="generic-info-value">{{parameterDialog.conversionFormula || '--'}}</view>
            <input wx:else class="value-input generic-value-input" data-field="conversionFormula" value="{{parameterDialog.conversionFormula}}" placeholder="x" bindinput="onParameterDraftInput" />
          </view>
          <view wx:if="{{parameterDialog.conversionFormulaErrorText}}" class="generic-info-row">
            <view class="generic-info-label">公式</view>
            <view class="generic-info-value">{{parameterDialog.conversionFormulaErrorText}}</view>
          </view>
          <view wx:if="{{parameterDialog.mode == 'viewRegister' || parameterDialog.showRange}}" class="generic-info-row">
            <view class="generic-info-label">最小值</view>
            <view wx:if="{{parameterDialog.mode == 'viewRegister'}}" class="generic-info-value">{{parameterDialog.minValue || '--'}}</view>
            <input wx:else class="value-input generic-value-input" data-field="minValue" value="{{parameterDialog.minValue}}" bindinput="onParameterDraftInput" />
          </view>
          <view wx:if="{{parameterDialog.mode == 'viewRegister' || parameterDialog.showRange}}" class="generic-info-row">
            <view class="generic-info-label">最大值</view>
            <view wx:if="{{parameterDialog.mode == 'viewRegister'}}" class="generic-info-value">{{parameterDialog.maxValue || '--'}}</view>
            <input wx:else class="value-input generic-value-input" data-field="maxValue" value="{{parameterDialog.maxValue}}" bindinput="onParameterDraftInput" />
          </view>
        </view>
      </view>
    </block>

    <view class="generic-draft-actions">
      <view class="panel-action-button" bindtap="closeParameterDraft">{{parameterDialog.cancelText}}</view>
      <view wx:if="{{parameterDialog.confirmText}}" class="panel-action-button is-active" bindtap="confirmParameterDialog">{{parameterDialog.confirmText}}</view>
    </view>
  </view>
</view>
