import { Component, OnInit, ViewChild, Input } from '@angular/core';
import { ActivatedRoute, RouterOutlet, Router } from '@angular/router';
import { CompTableListComponent } from '../../../app/comp-table/comp-table-list/comp-table-list.component';
import { EduTextbook } from '../../../schemas/EduTextbook';
import { NzPageHeaderModule } from 'ng-zorro-antd/page-header';
import { NzBreadCrumbModule } from 'ng-zorro-antd/breadcrumb';
import * as Parse from 'parse';
import { CommonModule } from '@angular/common';
import { textbookServer } from '../../../services/textbook';
import { TextbookComponent } from '../../../app/textbook/textbook.component';
import { NzSpaceModule } from 'ng-zorro-antd/space';
import { CommonCompModule } from '../../../services/common.modules';
import { NzTabsModule } from 'ng-zorro-antd/tabs';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-page-textbook',
  templateUrl: './page-textbook.component.html',
  styleUrls: ['./page-textbook.component.scss'],
  imports: [
    CommonModule,
    RouterOutlet,
    TextbookComponent,
    CompTableListComponent,
    NzPageHeaderModule,
    CommonCompModule,
    NzBreadCrumbModule,
    NzSpaceModule,
    NzTabsModule,
  ],
  standalone: true,
})
export class PageTextbookComponent implements OnInit {
  @ViewChild(CompTableListComponent) list: CompTableListComponent | undefined;
  @Input('discard') discard: boolean = false;
  @Input('render') render: boolean = false;
  active: number = 0;

  EduTextbook = EduTextbook;
  user: Parse.User | undefined;
  className: string | undefined;
  queryParams: any | undefined;
  fieldsArray: Array<any> | undefined;

  //待审核
  beforeFilterObj: any = {
    showMore: true, //显示更多字段
    isCheck: true,
    noStared: true,
    status: ['200'],
    btns: {
      reject: true, //退回教材
      star: true, //移除推荐
      export:true
    },
  };
  //已加入推荐
  afterFilterObj: any = {
    showMore: true, //显示更多字段
    isCheck: true,
    status: ['200','201','400'],
    btns: {
      remove: true, //移除推荐
      export:true
    },
  };
  eduProcess?:Parse.Object

  constructor(
    public tbookSer: textbookServer,
    private message: NzMessageService,
    private router: Router,
    private activeRoute: ActivatedRoute // private translate:TranslateService,
  ) {
    this.user = Parse.User.current();
    // this.className = this.EduTextbook.className;
    // this.fieldsArray = this.EduTextbook.fieldsArray;
    // this.queryParams = {
    //   where: {
    //     isDeleted: { $ne: true },
    //     render: true,
    //     discard: { $ne: true },
    //     department: this.tbookSer.profile.user.department?.objectId,
    //   },
    // };
  }

  ngOnInit(): void {
    this.getProcess()
  }
  async getProcess(){
    let parentMap = await this.tbookSer.formatNode(
      this.tbookSer?.profile?.user?.department?.objectId
    );
    if (!parentMap[1]?.key) return;
    let query = new Parse.Query('EduProcess');
    query.equalTo('department', parentMap[1]?.key);
    // query.lessThanOrEqualTo('startDate', new Date());
    // query.greaterThan('deadline', new Date());
    query.notEqualTo('isDeleted', true);
    // query.notEqualTo('status', '100');
    let res = await query.first();
    this.eduProcess = res
  }
  toUrl(url: string, params?: object) {
    if (params) {
      this.router.navigate([url, params]);
    } else {
      this.router.navigate([url]);
    }
  }
  //报送
  async submitted() {
    if(this.eduProcess?.get('status') == '400'){
      this.message.warning('已提交报送，无需重复提交')
      this.toUrl(`/nav-province-contact/manage/submitted/${this.eduProcess?.id}`)
      return
    }
    this.eduProcess?.set('status', '400');
    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', '400');
      await item.save();
    }
    this.message.success('提交成功')
    this.toUrl(`/nav-province-contact/manage/submitted/${this.eduProcess?.id}`)
  }
}
