import { Component, OnDestroy, OnChanges, OnInit, ApplicationRef, ChangeDetectorRef, ViewChild, ElementRef } from '@angular/core';
import { ComponentBase } from './ComponentBase';
import { MetaFormCreator } from './mataFormCreator';

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

  item = {
    title: '分类钓鱼',
    imgAni: {
      ske: {},
      tex: {},
      png: {}
    },
    startAudio: "",
    audioName:'',
    questions: [
      {
        title: '左侧船',
        text: "",
        audio: "",
        audioName: "",
        options: [],
      },
      {
        title: '右侧船',
        text: "",
        audio: "",
        audioName: "",
        options: [],
      }
    ],
  };

  isVisible = false;
  deleteTitle = "是否删除题目";
  deleteCallback = function () { };
  openDelete(title, callback) {
    this.deleteCallback = callback;
    this.deleteTitle = title;
    this.isVisible = true;
  }
  handleCancel() {
    this.isVisible = false;
  }
  handleOk() {
    this.deleteCallback && this.deleteCallback();
    this.handleCancel();
  }


  init(): void {
    console.log(new MetaFormCreator().create());
  }

  removeoption(i, j) {
    this.openDelete("确定删除选项？", () => {
      this.item.questions[i].options.splice(j, 1);
      this.save();
    })
  }
  copyOption(i, j) {
    let data = this.item.questions[i].options[j];
    this.item.questions[i].options.push(JSON.parse(JSON.stringify(data)));
    this.save();
  }

  addoption(i) {
    this.item.questions[i].options.push({
      type: "txt",
      image: "",
      audio: "",
      audioName: "",
      text: "",
      right: false
    });
    this.save();
  }



  onDragonBoneSave(e, item) {
    console.log(e);
    this.save();
  }
  ngChange(i, j) {
    this.save();
  }
}