import { Component, Input } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { CommonModule } from '@angular/common';
import { IonicModule } from '@ionic/angular';
import { CloudObject, CloudQuery } from 'src/lib/ncloud';

@Component({
  selector: 'app-tools-modal',
  standalone: true,
  imports: [CommonModule, IonicModule],
  template: `
    <ion-header>
      <ion-toolbar>
        <ion-title>可借用工具
         <ion-chip (click)="importTools()">导入测试数据</ion-chip>
         </ion-title>      
        <ion-buttons slot="end">
          <ion-button (click)="dismiss()">关闭</ion-button>
        </ion-buttons>
      </ion-toolbar>
    </ion-header>
    <ion-content>
      <ion-list>
        <ion-item *ngFor="let tool of tools">
          <ion-label>
            <h2>{{ tool.name }}</h2>
            <p>所需积分: {{ tool.points }}</p>
            <p>最晚归还时间: {{ tool.returnTime }}</p>
          </ion-label>
          <ion-button slot="end" fill="outline" color="primary">借用</ion-button>
        </ion-item>
      </ion-list>
    </ion-content>
  `
})
export class ToolsModalPage {
  @Input() tools: any[] = [];

  constructor(private modalCtrl: ModalController) {
    this.loadTools()
  }
storeList: CloudObject[]=[]
async loadTools(){
    let query = new CloudQuery("Tools");
    this.storeList = await query.find();
  }

  dismiss() {
    this.modalCtrl.dismiss();
  }
  async importTools() {
    const toolsData = [
        {name: '轮椅', points: 50, returnTime: '2023-12-31'},
        {name: '拐杖', points: 20, returnTime: '2023-11-15'},
        {name: '血压计', points: 30, returnTime: '2023-10-20'},
        {name: '血糖仪', points: 40, returnTime: '2024-01-10'},
        {name: '助行器', points: 25, returnTime: '2023-12-01'},
        {name: '制氧机', points: 80, returnTime: '2024-02-28'},
        {name: '护理床', points: 100, returnTime: '2023-11-30'},
        {name: '电动轮椅', points: 120, returnTime: '2024-03-15'},
        {name: '便携式马桶', points: 35, returnTime: '2023-12-25'},
        {name: '防褥疮垫', points: 45, returnTime: '2024-01-05'},
    ];
    try {
        for (const toolData of toolsData) {
            const tool = new CloudObject('Tools');
            tool.set(toolData);
            await tool.save();
            console.log(`成功保存工具: ${toolData.name}`);
        }
        console.log('所有工具数据已成功保存到数据库');
        this.tools = [...this.tools, ...toolsData]; // 更新本地工具列表
    } catch (error) {
        console.error('保存数据时出错:', error);
    }
}
}