import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { CommonCompModule } from '../../../../services/common.modules';
import { NzEmptyModule } from 'ng-zorro-antd/empty';
import Parse from 'parse';
import { NzModalService } from 'ng-zorro-antd/modal';
@Component({
  selector: 'app-complete',
  templateUrl: './complete.component.html',
  styleUrls: ['./complete.component.scss'],
  imports: [CommonCompModule, NzEmptyModule],
  standalone: true,
})
export class CompleteComponent implements OnInit {
  @Input('eduProcess') eduProcess?: Parse.Object;
  count: number = 0;
  date: Date = new Date();
  t: any;
  deadline: string = '';
  disabled: boolean = true;
  time() {
    if (new Date().getTime() > this.date.getTime()) {
      clearTimeout(this.t);
      this.disabled = false;
      return
    }
    this.t = setTimeout(() => {
      let hs = this.date.getTime() - new Date().getTime();
      let a = 60 * 60 * 1000;
      let day: number = Math.floor(hs / (24 * a));
      let h = Math.floor((hs - day * (24 * a)) / a);
      let m = Math.floor((hs - day * (24 * a) - h * a) / (60 * 1000));
      let s = Math.floor((hs - day * (24 * a) - h * a - m * 60 * 1000) / 1000);
      this.deadline = day + '天' + h + '小时' + m + '分' + s + '秒';
      this.time();
    }, 1000);
  }
  constructor(
    private modal: NzModalService,
    private router: Router,
    private activeRoute: ActivatedRoute
  ) {}

  ngOnInit() {
    this.date = new Date(
      this.eduProcess?.get('releaseDate').getTime() + 1000 * 60 * 60 * 24
    );
    this.time();
    this.getEduTextbook();
    if(this.eduProcess?.get('status') == '400'){
      this.modal.confirm({
        nzTitle: '流程已完成',
        nzContent: `报送已提交，请退出该页面`,
        nzOkText: '确认',
        nzOkType: 'primary',
        nzCancelText:null,
        nzOkDanger: false,
        nzOnOk: async () => {
          history.back()
        }
      })
    }
  }
  //报送
  async getEduTextbook() {
    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.containedIn('status', ['200', '201']);
    this.count = await query.count();
  }
  ngOnDestroy(): void {
    //Called once, before the instance is destroyed.
    //Add 'implements OnDestroy' to the class.
    clearTimeout(this.t);
  }

  //完成报送
  complete(){
    this.modal.confirm({
      nzTitle: '完成报送',
      nzContent: `提交后，提交所有推荐教材且流程将结束不可再遴选教材`,
      nzOkText: '确认',
      nzOkType: 'primary',
      nzOkDanger: false,
      nzOnOk: async () => {
        this.eduProcess?.set('status', '400');
        await this.eduProcess?.save();
        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.containedIn('status', ['201']);
        let list = await query.find();
        for (let index = 0; index < list.length; index++) {
          const item = list[index];
          item.set('status', '400');
          await item.save();
        }
        history.go(0)
      },
      nzCancelText: '取消',
      nzOnCancel: () => console.log('Cancel'),
    });
  }
  async revoke() {
    this.modal.confirm({
      nzTitle: '撤销公示',
      nzContent: `提交后，教材将重新进入遴选中。`,
      nzOkText: '确认',
      nzOkType: 'primary',
      nzOkDanger: false,
      nzOnOk: async () => {
        this.eduProcess?.set('status', '200');
        this.eduProcess?.set('releaseDate', null);
        await this.eduProcess?.save();
        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.containedIn('status', ['201']);
        let list = await query.find();
        for (let index = 0; index < list.length; index++) {
          const item = list[index];
          item.set('status', '200');
          await item.save();
        }
        history.go(0)
      },
      nzCancelText: '取消',
      nzOnCancel: () => console.log('Cancel'),
    });
  }
}
