
import { Component, OnInit } from '@angular/core';
import { DataService } from '../../../service/data.service';

import { ActivatedRoute, Router } from '@angular/router';
import * as Parse from "parse"

(Parse as any).serverURL = "https://web2023.fmode.cn/parse"
Parse.initialize("dev")
@Component({
  selector: 'app-home-page',
  templateUrl: './home-page.component.html',
  styleUrls: ['./home-page.component.scss']
})

export class HomePageComponent implements OnInit {
  columns: any[] = [];
  cardList: any[] = []
  cardDetail: any
  isDropdownOpen = false;
  // constructor() { }

  async ngOnInit(): Promise<void> {
    // 模拟数据
    const data = [
      'Item 1dfadfasdfasfsafasdfasfafafadfasfasdf', 'Item 1dfadfasdfasfsafasdfasfafafadfasfasdf', 'Item 1dfadfasdfasfsafasdfasfafafadfasfasdf',
      'Item 1dfadfasdfasfsafasdfasfafafadfasfasdf', 'Item 1dfadfasdfasfsafasdfasfafafadfasfasdf', 'Item 1dfadfasdfasfsafasdfasfafafadfasfasdf',
      'Item 1dfadfasdfasfsafasdfasfafafadfasfasdf', 'Item 1dfadfasdfasfsafasdfasfafafadfasfasdf',
    ];
    // 将数据均匀分配到四列
    const columnCount = 4;
    for (let i = 0; i < columnCount; i++) {
      this.columns.push([]);
    }
    for (let i = 0; i < data.length; i++) {
      const columnIndex = i % columnCount;
      this.columns[columnIndex].push(data[i]);
    }
    // let Student = Parse.Object.extend("LhyHuanLin")
    // let Query = new Parse.Query("LhyHuanLin")
    // Query.equalTo("name", "全改")
    // let slist = await Query.find()
    // let xiaoming = new Student();
    // for (let i = 0; i < slist.length; i++) {
    //   let stu1 = await Query.get(slist[i].id);
    //   if (stu1) {
    //     stu1.set({ name: "全改" })
    //     stu1.set({ ID: "0123456" })
    //     stu1.destroy();
    //     // stu1.save();
    //   }
    // }


    // let create = Parse.Object.extend('LhyHuanLin')
    // let xiaohu = new create()  //创建
    // xiaohu.set("name", "xiaoxiaohu")
    // xiaohu.set("tool", "axes")
    // xiaohu.save()


    // const Task = Parse.Object.extend('LhyHuanLin');
    // let query = new Parse.Query(Task)
    // query.equalTo("name", "xi")
    // query.first().then((task) => {
    //   if (task) {
    //     // 如果找到了符合条件的任务，则更新数据
    //     task.set('description', 'Updated description');
    //     // return task.save();
    //   } else {
    //     // 如果没有找到符合条件的任务，则创建新数据
    //     const newTask = new Task();
    //     newTask.set('name', 'xi');
    //     newTask.set('description', 'New task');
    //     return newTask.save();
    //   }
    // }).then((result) => {
    //   console.log('Operation completed:', result);
    // }).catch((error) => {
    //   console.error('Error:', error);
    // });
    // let list = await query.find()

    // if (list == null) {
    //   let xiaoxiaohu = Parse.Object.extend('LhyHuanLin')
    //   xiaoxiaohu.set('name', 'xiaoxiaoxiaohu')
    //   xiaoxiaohu.set('tool', '火把')
    //   xiaoxiaohu.save()
    // } else {
    //   for (let i = 0; i < list.length; i++) {
    //     list[i].set('tool', ' wz')
    //     list[i].set('age', i)
    //     list[i].save()
    //   }
    // }


    // 查询Student
    // let query = new Parse.Query("LhyHuanLin")
    // query.equalTo("age", undefined)
    // query.limit(10)
    // let slist = await query.find()
    // slist[0].set("tool", "枪")
    // 修改Student
    // let query2 = new Parse.Query("LhyHuanLin")
    // let stu1 = await query2.get(slist[0].id);
    // stu1.set("age", 100)
    // stu1.save()
    // 删除Student
    // let query3 = new Parse.Query("LhyHuanLin")
    // query3.equalTo("name", "来自JSSDK的小明")
    // let stu2 = await query3.find();
    // for (let i = 0; i < stu2.length; i++) {
    //   stu2[i].destroy()
    // }

    this.getDataFromTable()
  }

  private values: string[] = ['first', 'second', 'third'];
  accordionGroupChange = (ev: any) => {
    const collapsedItems = this.values.filter((value) => value !== ev.detail.value);
    const selectedValue = ev.detail.value;
    console.log(
      `Expanded: ${selectedValue === undefined ? 'None' : ev.detail.value} | Collapsed: ${collapsedItems.join(', ')}`
    );
  };

  //卡片数据
  async getDataFromTable() {
    const MyObject = Parse.Object.extend('LhyCardInfo'); // 替换为你需要读取数据的表名
    const query = new Parse.Query(MyObject);
    try {
      const results = await query.find();
      for (let i = 0; i < results.length; i++) {
        // const dataObject = {
        //   // 根据表结构定义对象的属性，将数据放入对象中
        //   objectId: results[i].id,
        //   src: results[i].get('src'), // 替换为你的表的字段名
        //   title: results[i].get('title'),
        //   writer: results[i].get('writer'),
        //   describe: results[i].get('describe'),
        //   //...
        // };                        //读取换为了数组
        const dataObject = results[i]
        await this.cardList.push(dataObject);
      }
      console.log(this.cardList); // 打印对象数组
    } catch (error) {
      console.error('Error: ', error);
    }
  }

  async OnDetailPage(path: string) {
    const MyObject = Parse.Object.extend('LhyCardDetail'); // 替换为你需要读取数据的表名
    const query = new Parse.Query(MyObject);
    try {
      query.equalTo("title", path)
      const results = query.find();
      this.cardDetail = results
      console.log(this.cardDetail)
    } catch (error) {
      console.error('Error: ', error);
    }
  }

  path = ""
  constructor(private route: ActivatedRoute, private router: Router, private dataService: DataService) {
    this.route.queryParams.subscribe(params => {
      this.path = location.pathname;
    })
  }
  goTab(path: string) {
    this.path = path;
    this.router.navigate([path])
  }
  //发送参数，实现参数的传递
  sendMessage(message: string): void {
    this.dataService.setMessage(message);
  }
  sendQuery(query: any): void {
    this.dataService.updateQuery(query);
  }
  goCardDetail(cardDetail: any) {
    this.router.navigate(["/lesson/student/detail"], {
      queryParams: cardDetail
    })
  }


}
