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 { NzUploadModule } from 'ng-zorro-antd/upload';
import { NzModalService } from 'ng-zorro-antd/modal';
import { CompUploadComponent } from '../../../app/comp-upload/comp-upload.component';
import { DatePipe } from '@angular/common';

import { textbookServer } from '../../../services/textbook';
import { ActivatedRoute } from '@angular/router';
import Parse from 'parse';

@Component({
  imports: [
    CommonCompModule,
    ReactiveFormsModule,
    NzSelectModule,
    NzRadioModule,
    NzGridModule,
    NzCheckboxModule,
    NzTableModule,
    NzUploadModule,
    CompUploadComponent,
    DatePipe,
  ],
  standalone: true,
  selector: 'app-upload-pdf',
  templateUrl: './upload-pdf.component.html',
  styleUrls: ['./upload-pdf.component.scss'],
})
export class UploadPdfComponent implements OnInit {
  eduTextbook?: Parse.Object;

  /**上传签名页 */
  authorSignPDF: any = {
    name: '',
    url: '',
  };

  /**申报单位承诺意见 */
  unitMaterial: any = {
    name: '',
    url: '',
  };

  constructor(
    public tbookSer: textbookServer,
    private msg: NzMessageService,
    private modal: NzModalService,
    private activeRoute: ActivatedRoute
  ) {}

  ngOnInit() {
    this.activeRoute.paramMap.subscribe(async (params) => {
      let id = params.get('id');
      let query = new Parse.Query('EduTextbook');
      query.equalTo('objectId', id);
      this.eduTextbook = await query.first();
      console.log(this.eduTextbook);
      this.authorSignPDF = this.eduTextbook?.get('authorSignPDF') || {
        name: '',
        url: '',
      };
      this.unitMaterial = this.eduTextbook?.get('unitMaterial') || {
        name: '',
        url: '',
      };
      if (
        Parse.User.current()?.id !=
        (this.eduTextbook?.get('user').id ||
          this.eduTextbook?.get('user').objectId)
      ) {
        window.alert('非创建用户，不可编辑');
        history.back();
      } else if (
        this.eduTextbook?.get('authorSignPDF')?.url &&
        this.eduTextbook?.get('unitMaterial')?.url
      ) {
        window.alert('已提交，禁止编辑');
        history.back();
      }
    });
  }

  async saveEduTextbook() {
    if (!this.eduTextbook) {
      this.msg.error('教材不存在');
      return;
    }
    if(!this.authorSignPDF?.url || !this.unitMaterial?.url){
      this.msg.warning('请上传完成相应附件');
      return
    }
    this.authorSignPDF &&
      this.eduTextbook?.set('authorSignPDF', this.authorSignPDF);
    this.unitMaterial &&
      this.eduTextbook?.set('unitMaterial', this.unitMaterial);
    await this.eduTextbook?.save();
    this.msg.success('提交成功');
    setTimeout(() => {
      this.cancel();
    }, 1500);
    return;
  }

  upload(e: any, type: string) {
    console.log(e);
    let file = e[e?.length - 1 || 0];
    if (type == 'unitMaterial' || type == 'authorSignPDF') {
      this[type].url = file?.url;
      this[type].name = file?.name;
    }
  }
  cancel() {
    history.back();
  }
}
