<!-- src/app/tab2/tab2.page.html -->
<ion-header>
  <ion-toolbar color="primary">
    <ion-title class="center-title">AI 问诊</ion-title>
  </ion-toolbar>
</ion-header>

<ion-content padding>
  <ion-list>
    <ion-item *ngFor="let message of messages" [class.user]="message.sender === 'user'" [class.ai]="message.sender === 'ai'">
      <div class="message-container" [class.user-message]="message.sender === 'user'" [class.ai-message]="message.sender === 'ai'">
        <div class="avatar-container" *ngIf="message.sender === 'user'">
          <ion-avatar class="user-avatar">
            <img [src]="'../../assets/images/user-avatar.png'" />
          </ion-avatar>
        </div>
        <div class="message-text">
          <h3>{{ message.sender === 'user' ? '您' : 'AI 医生' }}</h3>
          <p>{{ message.text }}</p>
        </div>
        <div class="avatar-container" *ngIf="message.sender === 'ai'">
          <ion-avatar class="ai-avatar">
            <img [src]="'../../assets/images/AI医生.png'" />
          </ion-avatar>
        </div>
      </div>
    </ion-item>
  </ion-list>
  
  <!-- 显示连接状态 -->
  <div *ngIf="!isSocketOpen" class="connection-status">
    <ion-spinner name="crescent"></ion-spinner>
    正在尝试连接...
  </div>
</ion-content>

<ion-footer>
  <ion-toolbar>
    <ion-grid>
      <ion-row>
        <ion-col size="10">
          <ion-input [(ngModel)]="userInput" placeholder="请输入您的问题..." (keyup.enter)="sendMessage()"></ion-input>
        </ion-col>
        <ion-col size="2" class="send-button-container">
          <ion-button (click)="sendMessage()" expand="block" [disabled]="isLoading || !userInput.trim()">
            发送
          </ion-button>
          <ion-spinner *ngIf="isLoading" name="crescent"></ion-spinner>
        </ion-col>
      </ion-row>
    </ion-grid>
  </ion-toolbar>
</ion-footer>