<navigation-bar background="{{themeMode === 'dark' ? '#111827' : '#FFF'}}"></navigation-bar>
<view wx:if="{{toastText}}" class="page-toast page-toast--{{toastType}} {{themeClass}}">
  {{toastText}}
</view>
<scroll-view class="scrollarea {{themeClass}}" scroll-y type="list">
  <view class="page-shell">
    <view class="panel status-summary-panel">
      <view class="panel-header panel-header--with-actions">
        <view class="panel-icon icon-status"></view>
        <view class="panel-title">状态</view>
        <view class="panel-actions panel-actions--status">
          <view
            class="panel-action-button auto-read-button {{autoReadStatus ? 'is-active' : ''}} {{autoReadStatus || canReadStatus ? '' : 'is-disabled'}}"
            bindtap="onAutoReadStatusTap"
          >
            {{autoReadStatus ? '停止' : '自动'}}
          </view>
          <view
            class="panel-action-button {{canReadStatus ? '' : 'is-disabled'}}"
            bindtap="readStatus"
          >
            读取
          </view>
        </view>
      </view>
      <view class="status-summary-body">
        <view class="status-summary-top">
          <view class="status-summary-box status-summary-box--state">
            <view class="status-summary-box-label">状态机</view>
            <view class="status-summary-box-value">{{statusSummary.stateText}}</view>
          </view>
          <view class="status-summary-box status-summary-box--fault {{statusSummary.faultClass}}">
            <view class="status-summary-box-label">故障码</view>
            <view class="status-summary-box-value">{{statusSummary.faultText}}</view>
          </view>
        </view>
        <view class="status-summary-line status-summary-line--metrics">
          <view
            wx:for="{{statusSummary.metrics}}"
            wx:key="key"
            class="status-metric"
          >
            {{item.displayText}}
          </view>
        </view>
      </view>
    </view>

    <view class="panel control-panel">
      <view class="panel-header panel-header--with-actions">
        <view class="panel-icon icon-control"></view>
        <view class="panel-title">控制指令</view>
        <view class="panel-actions panel-actions--three control-actions">
          <view
            class="panel-action-button {{connectedDevice && !isBootloaderBusy ? '' : 'is-disabled'}}"
            bindtap="readControlStatus"
          >
            读取
          </view>
          <view
            wx:for="{{controlActionButtons}}"
            wx:key="key"
            class="panel-action-button {{connectedDevice && !isBootloaderBusy ? '' : 'is-disabled'}}"
            data-key="{{item.key}}"
            bindtap="onControlButtonTap"
          >
            {{item.name}}
          </view>
        </view>
      </view>
      <view class="param-row input-row">
        <view class="param-main">
          <view class="param-name">{{speedCommand.name}}</view>
          <view class="param-meta {{speedCommand.isDirty ? 'param-meta--dirty' : ''}}">{{speedCommand.addressDisplay}} {{speedCommand.writeValue}}</view>
        </view>
        <view class="input-wrap">
          <input
            class="value-input"
            type="{{speedCommand.unit ? 'text' : 'digit'}}"
            placeholder="--"
            value="{{speedCommand.inputValue}}"
            bindinput="onSpeedCommandInput"
            bindblur="onSpeedCommandBlur"
          />
        </view>
      </view>
      <view class="control-grid">
        <view wx:for="{{controlButtons}}" wx:key="key" class="control-cell control-cell--{{item.key}}">
          <button
            class="control-button control-button--{{item.key}}"
            disabled="{{!connectedDevice || isBootloaderBusy}}"
            data-key="{{item.key}}"
            bindtap="onControlButtonTap"
          >
            <text class="control-name">{{item.name}}</text>
          </button>
        </view>
      </view>
    </view>

    <view class="panel upgrade-panel">
      <view class="panel-header panel-header--with-actions">
        <view class="panel-icon icon-chip"></view>
        <view class="panel-title">BootLoader</view>
        <view class="panel-actions upgrade-actions">
          <view
            class="panel-action-button {{isBootloaderBusy ? 'is-disabled' : ''}}"
            bindtap="openFirmwareFile"
          >
            打开
          </view>
          <view
            class="panel-action-button {{isBootloaderBusy ? 'is-disabled' : ''}}"
            bindtap="chooseFirmwareFile"
          >
            聊天
          </view>
          <view
            class="panel-action-button {{connectedDevice && !isBootloaderBusy ? '' : 'is-disabled'}}"
            bindtap="handshakeBootloader"
          >
            握手
          </view>
          <view
            class="panel-action-button {{connectedDevice && !isBootloaderBusy ? '' : 'is-disabled'}}"
            bindtap="readProgramChecksum"
          >
            读取
          </view>
          <view
            class="panel-action-button {{connectedDevice && !isBootloaderBusy ? '' : 'is-disabled'}}"
            bindtap="exitBootloader"
          >
            退出
          </view>
          <view
            class="panel-action-button {{connectedDevice && isFirmwareReady && !isBootloaderBusy ? '' : 'is-disabled'}}"
            bindtap="startFirmwareUpgrade"
          >
            升级
          </view>
        </view>
      </view>
      <view class="upgrade-body">
        <view class="upgrade-row">
          <text class="upgrade-label">芯片型号</text>
          <text class="upgrade-value">{{chipModel}}</text>
        </view>
        <view class="upgrade-row upgrade-row--file">
          <view class="upgrade-file-head">
            <text class="upgrade-label">固件文件</text>
            <view class="upgrade-file-meta">
              <text class="upgrade-file-meta-item upgrade-file-meta-item--program">{{deviceProgramCrcText}}</text>
              <text class="upgrade-file-meta-item upgrade-file-meta-item--checksum">{{firmwareChecksumText}}</text>
              <text class="upgrade-file-meta-item upgrade-file-meta-item--size">{{firmwareSizeText}}</text>
            </view>
          </view>
          <text class="upgrade-file-name">{{firmwareName ? firmwareName : '--'}}</text>
        </view>
        <view class="upgrade-row">
          <text class="upgrade-label">Bootloader</text>
          <text class="upgrade-value">{{bootloaderVersion}} / {{bootloaderChipId}}</text>
        </view>
        <view wx:if="{{bootloaderStatusText || bootloaderDetailText}}" class="upgrade-status {{isFirmwareReady ? 'upgrade-status--ready' : ''}}">
          {{bootloaderStatusText}}<text wx:if="{{bootloaderDetailText}}"> · {{bootloaderDetailText}}</text>
        </view>
        <view class="upgrade-progress">
          <view class="upgrade-progress-bar" style="width: {{bootloaderProgress}}%;"></view>
        </view>
      </view>
    </view>
  </view>
</scroll-view>
