import { Component, OnDestroy, OnChanges, OnInit, ApplicationRef, ChangeDetectorRef, ViewChild, ElementRef } from '@angular/core';
import { ComponentBase } from './ComponentBase';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.css']
})
export class FormComponent extends ComponentBase implements OnInit, OnChanges, OnDestroy {
  // 储存数据用
  saveKey = "dg_video";

  item = {
    dataArr: []
  };

  constructor(public nzMessageService: NzMessageService, public appRef: ApplicationRef, public changeDetectorRef: ChangeDetectorRef) {
    super(appRef, changeDetectorRef);
  }

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

  onVideoUploaded(e, index) {
    this.item.dataArr[index].video_url = e.url
    this.save();
  }

  /**
   * 删除卡片
   */
  onRemoveData(index) {
    let data = this.item.dataArr[index]
    if (data) {
      this.item.dataArr.splice(index, 1)
      this.save()
    }
  }

  /** 添加卡片 */
  onAddData() {
    this.item.dataArr.push({
      title: "",
      cover: "",
      video_url: ""
    })
    this.save()
  }


}