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 = "hy17_hds";

  item = {
    recordFlag: false,
    npcTitle: "",
    npcAudio: "",
    npcAudioName: "",
    title: "",
    questionText: "",
    questionTextAudio: "",
    questions: [{
      text: "",
      time: 10
    }],
    bgAudio: "",
    bgAudioName: "",
    audioName: ""
  };

  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 {
    if (!this.item.npcTitle) this.item.npcTitle = '';
    if (!this.item.npcAudio) this.item.npcAudio = '';
    if (!this.item.npcAudioName) this.item.npcAudioName = '';
  }



  removequestion(i) {
    this.openDelete("确定删除题目？", () => {
      this.item.questions.splice(i, 1);
      this.save();
    })
  }

  addquestion() {
    this.item.questions.push({
      text: "",
      time: 10,
    });
    this.save();
  }

  deleteBgAudio() {
    this.item.bgAudio = "";
    this.item.bgAudioName = "";
    this.save();
  }
  ngChange(i, j) {
    this.save();
  }
}