import { Component, OnInit } from '@angular/core';
import { Input, Output, EventEmitter } from '@angular/core';
import { CommonCompModule } from '../../../../../services/common.modules';
import { NzSelectModule } from 'ng-zorro-antd/select';
import { ReactiveFormsModule } from '@angular/forms';
import { NzRadioModule } from 'ng-zorro-antd/radio';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzGridModule } from 'ng-zorro-antd/grid';
import { NzCheckboxModule } from 'ng-zorro-antd/checkbox';
import { NzTableModule } from 'ng-zorro-antd/table';
import { NzModalService } from 'ng-zorro-antd/modal';
import Parse from 'parse';
// import {
//   FormControl,
//   FormGroup,
//   NonNullableFormBuilder,
//   Validators,
// } from '@angular/forms';
import { textbookServer } from '../../../../../services/textbook';
import { CreatedService } from '../../../../../services/created.service'

interface course {
  date: Date | any,
  wordage: number | any,
  num: number | any,
  sumNum: number | any,
  accolade: string
}


@Component({
  selector: 'app-three',
  templateUrl: './content.component.html',
  styleUrls: ['./content.component.scss'],
  standalone: true,
  imports: [
    CommonCompModule,
    ReactiveFormsModule,
    NzSelectModule,
    NzRadioModule,
    NzGridModule,
    NzCheckboxModule,
    NzTableModule,
    ContentComponent
  ],
})
export class ContentComponent implements OnInit {

  @Input('eduTextbook') eduTextbook: any;
  @Input('eduTextbookVolume') eduTextbookVolume: Parse.Object | any;


  @Input('maxWidth') maxWidth: number = 0;
  @Output() state: EventEmitter<any> = new EventEmitter<any>();
  @Output() save: EventEmitter<any> = new EventEmitter<any>();

  //申报教材建设历程
  courses: Array<course> = [
    {
      date: '',//出版时间
      wordage: '',//字数
      num: '',//重印次数
      sumNum: '',//本版总印数
      accolade: ''//获奖励情况
    },
  ];
  constructor(
    public tbookSer: textbookServer,
    private modal: NzModalService,
    private msg: NzMessageService,
    private creatSev: CreatedService
  ) { }

  ngOnInit() {
    this.refersh()
  }
  ngAfterViewInit(): void {
  }
  async refersh() {
    await this.getEduTextbookVolume()
    if (this.eduTextbookVolume?.id) {
      this.creatSev.type = this.eduTextbookVolume.get('type')
      this.creatSev.typeNumber = this.eduTextbookVolume.get('typeNumber')
      this.courses = this.eduTextbookVolume.get('courses') || this.courses
    }
  }

  /**获取扩展分册记录 */
  async getEduTextbookVolume() {
    let id = this?.eduTextbookVolume?.id || this?.eduTextbookVolume?.objectId;
    console.log('传递的分册id' + id)
    if (id) {
      let query = new Parse.Query('EduTextbookVolume')
      this.eduTextbookVolume = await query.get(id)
      console.log(this.eduTextbookVolume);
    }
  }

  //添加作者信息
  onPush(idx: number) {
    if (this.courses?.length >= 15) {
      this.msg.warning('最多添加15条');
      return
    }
    this.courses.splice(idx + 1, 0, {
      date: '',
      wordage: '',
      num: '',
      sumNum: '',
      accolade: ''
    });
  }
  //删除作者信息
  onDel(idx: number) {
    if (this.courses?.length == 1) return
    this.courses.splice(idx, 1);
  }

  async submitForm(): Promise<boolean> {
    let coursesVrifly = !this.courses.some((item) =>
      Object.values(item).some((val) =>  val !== 0 && (val == undefined || val == ''))
    );
    this.eduTextbookVolume?.set('courses', this.courses);
    await this.eduTextbookVolume?.save();
    return coursesVrifly;
  }
}
