import { Component, OnInit, ViewChild } from '@angular/core';
import { CommonCompModule } from '../../../services/common.modules';
import { Router, ActivatedRoute } from '@angular/router';
import { ReactiveFormsModule } from '@angular/forms';
import { NzAnchorModule } from 'ng-zorro-antd/anchor';
import { DatePipe } from '@angular/common';
import Parse from 'parse';
import { NzCollapseModule } from 'ng-zorro-antd/collapse';
import { BrowserModule } from '@angular/platform-browser';
import { CommonModule } from '@angular/common';
import { NzTabsModule } from 'ng-zorro-antd/tabs';
import { NzTableModule } from 'ng-zorro-antd/table';
import { textbookServer } from '../../../services/textbook';
import * as compute from '../../../services/compute';

@Component({
  selector: 'app-textbook-details',
  imports: [
    CommonCompModule,
    ReactiveFormsModule,
    NzAnchorModule,
    DatePipe,
    NzCollapseModule,
    CommonModule,
    NzTabsModule,
    NzTableModule,
  ],
  standalone: true,
  templateUrl: './textbook-details.component.html',
  styleUrls: ['./textbook-details.component.scss'],
  providers: [DatePipe],
})
export class TextbookDetailsComponent implements OnInit {
  @ViewChild('templ1') templ1: any;
  user?: Parse.Object;
  textBook: object | any = {};
  active:number = 0
  /**扩展表记录 */
  eduTextbookVolumeList: Array<any> = [];

  /** 册数*/
  typeNumber: any;
  /** 申报类型*/
  type: any;

  get total():number{
    let num = 0
    this.reviewList.forEach(item=>{
      num = compute.addNum(
        item?.get('score'),
        num
      );
    })
    return num
  }

  constructor(
    public tbookSer: textbookServer,
    private router: Router,
    private activeRoute: ActivatedRoute
  ) {}

  ngOnInit() {
    this.activeRoute.paramMap.subscribe(async (params) => {
      this.user = Parse.User.current();
      let id = params.get('id');
      console.log(id);
      if (id) {
        let query = new Parse.Query('EduTextbook');
        query.equalTo('objectId', id);
        query.include('childrens');
        let r = await query.first();
        this.textBook = r?.toJSON();
        console.log(this.textBook);
        this.eduTextbookVolumeList = this.textBook?.childrens;
        this.getExpertGroup();
        this.getEduReview();
      }
    });
  }

  handleClick(e: string): void {
    console.log(e);
  }
  back() {
    history.back();
  }
  edit() {
    this.router?.navigate(
      ['/nav-author/manage/apply', { id: this.textBook.objectId }],
      { replaceUrl: true }
    );
  }
  openFile(url: string) {
    console.log(url);
    window.open(url);
  }
  /**锚点 */
  herfLink(domId: string) {
    console.log(domId);
    let topEle = document.getElementById(domId);
    if (topEle) {
      topEle.scrollIntoView({ behavior: 'smooth' });
    }
  }

  groupLogin: boolean = true;
  reviewLogin: boolean = true;
  groupList: Array<Parse.Object> = [];
  /**获取本教材评审组 */
  async getExpertGroup() {
    let groupQuery = new Parse.Query('ExpertGroup');
    groupQuery.notEqualTo('isDeleted', true);

    let bookQuery = new Parse.Query('EduTextbook');
    bookQuery.equalTo('objectId', this.textBook?.objectId);

    groupQuery.matchesQuery('textbookList', bookQuery);
    this.groupList = await groupQuery.find();
    console.log(this.groupList);
    this.groupLogin = false;
  }
  reviewList: Array<Parse.Object> = [];
  /**获取本教程评分 */
  async getEduReview() {
    let query = new Parse.Query('EduReview');
    query.notEqualTo('isDeleted', true);
    query.equalTo('eduTextbook', this.textBook?.objectId);
    query.include('profile', 'profile.user');
    query.equalTo('verify',true)
    this.reviewList = await query.find();
    console.log(this.reviewList);
    this.reviewLogin = false;
  }
}
