import {Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef} from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';



@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent implements OnInit, OnChanges, OnDestroy {

  // 储存数据用
  saveKey = 'ww_sequence';
  // 储存对象
  item;
  picArray: Array<{
    controlInstance: string,
    progress: number,
    uploadStatus: number,
    uploading: boolean,
    res_id: number
  }>;

  constructor(private appRef: ApplicationRef,
              private nzMessageService: NzMessageService,
              private changeDetectorRef: ChangeDetectorRef) {

  }


  ngOnInit() {

    this.item = {
      contentObj: {}
    };

    // 获取存储的数据
    (window as any).courseware.getData((data) => {

      if (data) {
        this.item = data;
      }
      this.picArray  = [];
      if (Array.isArray(this.item.contentObj.pics) && this.item.contentObj.pics.length > 0) {
        for (const p of this.item.contentObj.pics) {
          this.addPictureField({
            controlInstance: `${p}`,
            res_id: p
          });
        }
      } else {
        this.addPictureField();
      }

      this.init();
      this.changeDetectorRef.markForCheck();
      this.changeDetectorRef.detectChanges();
      this.refresh();

    }, this.saveKey);

  }


  ngOnChanges() {
  }

  ngOnDestroy() {
  }



  init() {

  }

  //
  // /**
  //  * 储存图片数据
  //  * @param e
  //  */
  // onImageUploadSuccess(e, key) {
  //
  //     this.item[key] = e.url;
  //     this.save();
  // }

  // /**
  //  * 储存音频数据
  //  * @param e
  //  */
  // onAudioUploadSuccess(e, key) {
  //
  //   this.item[key] = e.url;
  //   this.save();
  // }



  /**
   * 储存数据
   */
  save() {
    (window as any).courseware.setData(this.item, null, this.saveKey);
    this.refresh();
  }

  /**
   * 刷新 渲染页面
   */
  refresh() {
    setTimeout(() => {
      this.appRef.tick();
    }, 1);
  }


  addPictureField(control?: any, e?: MouseEvent): void {
    // debugger;
    if (e) {
      e.preventDefault();
    }
    // const sts = this.checkForm();
    // if (!sts) {
    //   return;
    // }
    const id = this.picArray.length ; // (this.controlArray.length > 0) ? this.controlArray[ this.controlArray.length - 1 ] : 0;
    if (id >= 9) {
      this.nzMessageService.info('you can only add nine item!');
      return;
    }
    if (!control) {
      control = {
        controlInstance: `${id}`,
        res_id: 0
      };
    }

    const index = this.picArray.push(control);

  }

  removeField(i: any, e: MouseEvent): void {
    e.preventDefault();
    if (this.picArray.length > 1) {
      const index = this.picArray.indexOf(i);
      this.picArray.splice(index, 1);
      // console.log(this.controlArray);
      this.savedata();
    }
  }

  onImageUploadSuccess(control, info) {
    // console.log(control, info)
    control.res_id = info.url;
    this.savedata();
  }
  savedata() {
    this.item.contentObj.pics = [];
    for (const idx in this.picArray) {
      this.item.contentObj.pics[idx] = this.picArray[idx].res_id;
    }
    this.save();
  }



}