<view class="page">
  <view class="header">
    <text class="eyebrow">上课助手</text>
    <text class="title">课堂投票</text>
  </view>

  <view wx:if="{{!poll}}" class="panel form-panel">
    <view class="field">
      <text class="label">投票题目</text>
      <input class="input" value="{{question}}" bindinput="onQuestionInput" placeholder="例如：今天先讲哪一章？" />
    </view>

    <view class="field">
      <text class="label">选项</text>
      <block wx:for="{{optionInputs}}" wx:key="index">
        <input
          class="input option-input"
          data-index="{{index}}"
          value="{{item}}"
          bindinput="onOptionInput"
          placeholder="选项 {{index + 1}}"
        />
      </block>
    </view>

    <text wx:if="{{errorMessage}}" class="error">{{errorMessage}}</text>
    <button class="primary-button action-button" bindtap="createPoll">创建投票</button>
  </view>

  <view wx:else class="panel vote-panel">
    <view class="poll-head">
      <text class="section-label">正在投票</text>
      <text class="question">{{poll.question}}</text>
    </view>

    <view class="options">
      <block wx:for="{{poll.options}}" wx:key="id">
        <view
          class="option {{selectedOptionId === item.id ? 'option-selected' : ''}}"
          data-id="{{item.id}}"
          bindtap="selectOption"
        >
          <text class="option-text">{{item.text}}</text>
          <text class="option-count">{{item.votes}} 票</text>
        </view>
      </block>
    </view>

    <text wx:if="{{errorMessage}}" class="error">{{errorMessage}}</text>

    <button class="primary-button action-button" bindtap="submitVote">提交投票</button>
    <view class="button-row">
      <button class="ghost-button row-button" bindtap="goResults">查看结果</button>
      <button class="danger-button row-button" bindtap="resetPoll">重置</button>
    </view>
  </view>
</view>
