import { Component, OnInit, 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 { CompUploadComponent } from '../../../../../app/comp-upload/comp-upload.component';
import {
  FormControl,
  FormGroup,
  NonNullableFormBuilder,
  Validators,
} from '@angular/forms';
import Parse from 'parse';
import { DatePipe } from '@angular/common';

interface author {
  name: string;
  unit: string;
  birth: Date | any;
  nationality: string;
  job: string;
  title: string;
  mobile: string;
  email: string;
  work: string;
  // signature?: string;
  examine?: string;
  // otherEditor?:string
}
@Component({
  selector: 'app-author-file',
  templateUrl: './author-file.component.html',
  styleUrls: ['./author-file.component.scss'],
  imports: [
    CommonCompModule,
    ReactiveFormsModule,
    NzSelectModule,
    NzRadioModule,
    NzGridModule,
    NzCheckboxModule,
    NzTableModule,
    DatePipe,
    CompUploadComponent,
  ],
  standalone: true,
  providers: [DatePipe],
})
export class AuthorFileComponent implements OnInit {
  @Input('eduTextbook') eduTextbook: Parse.Object | any;
  @Input('maxWidth') maxWidth: number = 0;
  @Input('eduTextbookVolume') eduTextbookVolume: Parse.Object | any;

  //作者信息
  authorList: Array<author> = [
    {
      name: '',
      unit: '',
      birth: new Date('December 1, 1975 00:00:00'),
      nationality: '',
      job: '',
      title: '',
      mobile: '',
      email: '',
      work: '',
      // signature: '',
      examine: '',
      // otherEditor:''
    },
  ];
  otherEditor?:string //其他作者附件

  constructor(
    private msg: NzMessageService
  ) {}
  ngOnInit() {
    let id = this?.eduTextbookVolume?.id || this?.eduTextbookVolume?.objectId;
    if (id) {
      this.getEduTextbookVolume(id);
    }
  }
  showUpload:boolean = false
  async getEduTextbookVolume(id: string) {
    let query = new Parse.Query('EduTextbookVolume');
    query.equalTo('objectId', id);
    let r = await query.first();
    this.eduTextbookVolume = r;
    this.authorList =
      this.eduTextbookVolume.get('authorList') || this.authorList;
    this.otherEditor = this.eduTextbookVolume.get('otherEditor')
    console.log(this.otherEditor);
    this.showUpload = true
  }
  upload(e: any, type: string, index?: any) {
    let file = e[0];
    if (type == 'examine') {
      this.authorList[index][type] = file?.url;
    }else if(type == 'otherEditor'){
      this.otherEditor = file?.url;
    }
    // console.log(this.authorList);
    // console.log(this.otherEditor);
  }
  async submitForm(): Promise<boolean> {
    // console.log(this.otherEditor);
    let coursesVrifly = !this.authorList.some((item:any) =>
      Object.keys(item).some((key) => key != 'otherEditor' && key != 'signature' && (item[key] == '' || item[key] == undefined))
    );
    this.eduTextbookVolume?.set('authorList', this.authorList);
    this.eduTextbookVolume?.set('otherEditor', this.otherEditor||null);
    await this.eduTextbookVolume?.save();
    return coursesVrifly;
  }
  openFile(url: string) {
    console.log(url);
    window.open(url);
  }
  //删除作者信息
  onDel(idx: number) {
    if (this.authorList.length <= 1) {
      this.msg.warning('至少保留一项')
      return;
    }
    this.authorList.splice(idx, 1);
  }
}
