import {
  Component,
  EventEmitter,
  OnInit,
  Output,
  ViewChild,
  ElementRef,
  Input,
} from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { TextbookComponent } from '../../../../app/textbook/textbook.component';
import { CommonCompModule } from '../../../../services/common.modules';
import Parse from 'parse';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-export-list',
  templateUrl: './export-list.component.html',
  styleUrls: ['./export-list.component.scss'],
  imports: [CommonCompModule, TextbookComponent],
  standalone: true,
})
export class ExportListComponent implements OnInit {
  @ViewChild('textbook') textbook: ElementRef | any;

  @Output() change: EventEmitter<any> = new EventEmitter<any>();
  textBookList: Parse.Object | any;
  @Input('eduProcess') eduProcess?: Parse.Object;

  filterObj: any = {
    isCheck: false,
    status: ['200'],
    btns: {},
  };
  constructor(
    private message: NzMessageService,
    private router: Router,
    private activeRoute: ActivatedRoute
  ) {}

  ngOnInit() {
    this.activeRoute.paramMap.subscribe(async (params) => {
      // let id = params.get('id');
      // let query = new Parse.Query('EduProcess');
      // query.include('department');
      // query.equalTo('objectId', id);
      // this.eduProcess = await query.first();
    });
  }
  async onChange(type: string) {
    if (type == 'next') {
      if(!await this.submitted()) return
      this.change.emit('next');
    } else {
      this.change.emit('pre');
    }
  }
  //报送
  async submitted():Promise<boolean|undefined> {
    this.eduProcess?.set('status', '300');
    this.eduProcess?.set('releaseDate', new Date());
    let query = new Parse.Query('EduTextbook');
    query.equalTo('eduProcess', this.eduProcess?.id);
    query.notEqualTo('isDeleted', true);
    query.notEqualTo('discard', true);
    query.equalTo('render', true);
    query.equalTo('recommend', true);
    query.equalTo('status', '200');
    let list = await query.find();
    query.containedIn('approval', [
      '首届全国教材建设奖优秀教材（高等教育类）教材',
      '否',
    ]);
    let count = await query.count();
    if (count > this.eduProcess?.get('num')) {
      this.message.warning('当前推荐教材数量已超额')
      return;
    }
    await this.eduProcess?.save();
    for (let index = 0; index < list.length; index++) {
      const item = list[index];
      item.set('status', '201');
      await item.save();
    }
    return true
  }
}
