import {
  Component,
  ElementRef,
  EventEmitter,
  Input,
  OnInit,
  Output,
  ViewChild,
} from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { CommonCompModule } from '../../../../services/common.modules';
import { TextbookComponent } from '../../../../app/textbook/textbook.component';

import Parse from 'parse';
import { textbookServer } from '../../../../services/textbook';
import { NzMessageService } from 'ng-zorro-antd/message';
@Component({
  selector: 'app-export-file',
  templateUrl: './export-file.component.html',
  styleUrls: ['./export-file.component.scss'],
  imports: [CommonCompModule, TextbookComponent],
  standalone: true,
})
export class ExportFileComponent implements OnInit {
  @ViewChild('textbook') textbook: ElementRef | any;
  @Output() change: EventEmitter<any> = new EventEmitter<any>();
  @Input('eduProcess') eduProcess?: Parse.Object;

  starCount: number = 0;
  otherCount: number = 0;
  allCount: number = 0;
  tbookId: any;
  filterObj: any = {
    isCheck: false,
    status: ['400'],
    btns: {},
  };
  showLoading: boolean = false; //全局

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

  ngOnInit() {
    this.activeRoute.paramMap.subscribe(async (params) => {
      let id = params.get('id');
      this.tbookId = id;
      // let query = new Parse.Query('EduProcess');
      // query.include('branch', 'department');
      // query.equalTo('objectId', id);
      // this.eduProcess = await query.first();
      console.log(id);
      let query = new Parse.Query('EduTextbook');
      query.notEqualTo('isDeleted', true);
      query.equalTo('recommend', true);
      query.equalTo('eduProcess', id);
      this.allCount = await query.count();
      query.containedIn('approval', [
        '首届全国教材建设奖优秀教材（高等教育类）教材',
        '否',
      ]);
      this.otherCount = await query.count();
    });
  }
  close() {
    history.back();
  }
  next() {
    this.change.emit('next');
  }
  async onDownload() {
    if (this.showLoading) return;
    this.showLoading = true;
    try {
      await this.tbookSer.tbookExportReport({ processId: this.tbookId });
      this.showLoading = false;
    } catch (err) {
      this.message.error('导出超时，请稍后重试');
      console.log(err);
      this.showLoading = false;
    }
  }
}
