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 { NzUploadChangeParam } from 'ng-zorro-antd/upload';
import { NzUploadModule } from 'ng-zorro-antd/upload';
import { NzModalService } from 'ng-zorro-antd/modal';
import {
  FormControl,
  FormGroup,
  NonNullableFormBuilder,
  Validators,
} from '@angular/forms';
interface opinionType {
  name: string;
  unit: string;
  birth: Date | any;
  nationality: string;
  reviewFile: string;
}
@Component({
  selector: 'app-attachment',
  imports: [
    CommonCompModule,
    ReactiveFormsModule,
    NzSelectModule,
    NzRadioModule,
    NzGridModule,
    NzCheckboxModule,
    NzTableModule,
    NzUploadModule,
  ],
  standalone: true,
  templateUrl: './attachment.component.html',
  styleUrls: ['./attachment.component.scss'],
})
export class AttachmentComponent implements OnInit {
  @Input('eduTextbook') editFrom: any;
  @Input('maxWidth') maxWidth: number = 0;
  @Output() state: EventEmitter<any> = new EventEmitter<any>();
  @Output() save: EventEmitter<any> = new EventEmitter<any>();
  validateForm: FormGroup<{
    textbookTypes: FormControl<Array<string> | any>; //教材电子版
    textbookFiles: FormControl<Array<string> | any>; //上传教材文件
    links: FormControl<string>; //教材数字内容链接地址、账号
    selfResults:FormControl<object|any>, //图书编校质量自查结果记录表
    unitMaterial:FormControl<object|any>, //申报单位承诺意见材料
  }> = this.fb.group({
    textbookTypes: ['', [Validators.required]],
    textbookFiles: ['', [Validators.required]],
    links: ['', [Validators.required]],
    selfResults: ['', [Validators.required]],
    unitMaterial: ['', [Validators.required]],
  });
  checkOptionsOne = [
    { label: '纸质教材', value: '纸质教材' },
    { label: '电子教材 / 资源', value: '电子教材 / 资源' },
  ];
  //所有作者政治审查意见
  opinions: Array<opinionType> = [
    {
      name: '', //作者
      unit: '', //单位
      birth: '', //出生年月
      nationality: '', //国籍
      reviewFile: '', //作者政治审查表/作者签名
    },
  ];
  //所有作者签名
  authorSign: Array<opinionType> = [
    {
      name: '', //作者
      unit: '', //单位
      birth: '', //出生年月
      nationality: '', //国籍
      reviewFile: '', //作者政治审查表/作者签名
    },
  ];
  constructor(
    private fb: NonNullableFormBuilder,
    private msg: NzMessageService,
    private modal: NzModalService
  ) {}
  ngOnInit() {}
  submitForm(event?: string): void {
    console.log(this.validateForm.value);
    if (this.validateForm.valid) {
      console.log(this.validateForm.value);
    } else {
      Object.values(this.validateForm.controls).forEach((control) => {
        if (control.invalid) {
          control.markAsDirty();
          control.updateValueAndValidity({ onlySelf: true });
        }
      });
      this.msg.warning('请填写完整信息');
    }
    if (event == 'pre') {
      this.state.emit('pre');
    }
    if (event == 'save') {
      this.modal.success({
        nzTitle: '您已填写完成',
        nzContent: '<p>已保存并且至空间</p>',
        nzOnOk: () => console.log('Info OK')
      });
    }
    if(event == 'complete')
    this.modal.success({
      nzTitle: '您已填写完成',
      nzContent: '<p>已保存并且至空间</p>',
      nzOnOk: () => console.log('Info OK')
    });
  }
  changeCode() {}
  getCode(e: any) {}
  //添加作者信息
  onPush(type: string, idx: number) {
    switch (type) {
      case 'opinions':
        this.opinions.splice(idx + 1, 0, {
          name: '', //作者
          unit: '', //单位
          birth: '', //出生年月
          nationality: '', //国籍
          reviewFile: '', //作者政治审查表/作者签名
        });
        break;
      case 'authorSign':
        this.authorSign.splice(idx + 1, 0, {
          name: '', //作者
          unit: '', //单位
          birth: '', //出生年月
          nationality: '', //国籍
          reviewFile: '', //作者政治审查表/作者签名
        });
        break;
    }
  }
  //删除作者信息
  onDel(type: string, idx: number) {
    switch (type) {
      case 'opinions':
        this.opinions.splice(idx, 1);
        break;
      case 'authorSign':
        this.authorSign.splice(idx, 1);
        break;
    }
  }
  handleChange(info: NzUploadChangeParam): void {
    if (info.file.status !== 'uploading') {
      console.log(info.file, info.fileList);
    }
    if (info.file.status === 'done') {
      this.msg.success(`${info.file.name} file uploaded successfully`);
    } else if (info.file.status === 'error') {
      this.msg.error(`${info.file.name} file upload failed.`);
    }
  }
}
