import { Component, EventEmitter, Input, OnDestroy, OnChanges, OnInit, Output, ApplicationRef, ChangeDetectorRef } from '@angular/core';
import { JsonPipe } from '@angular/common';
import { ComponentBase } from './ComponentBase';


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

	// 储存数据用
	saveKey = "OP69";
	// 储存对象
	item: ObjData

	init() {
		if (!this.item.question_arr) {
			this.item = new ObjData();
		}
	}
	/**
	 * 储存图片数据
	 * @param e
	 */
	onAssetUploadSuccess2(e, key, item?) {
		if (!item)
			item = this.item
		item[key] = e.url;
		this.save();
	}

	addQuestion() {
		if (this.item.question_arr.length >= 2)
			return
		let question = new Question();
		this.item.question_arr.push(question);
		this.save();
	}

	delQuestion(idx) {
		this.item.question_arr.splice(idx, 1);
		this.save();
	}

	addOption(idx) {
		let option = new Option();
		this.item.question_arr[idx].option_arr.push(option);
		this.save();
	}

	delOption(questionIdx, optionIdx) {
		this.item.question_arr[questionIdx].option_arr.splice(optionIdx, 1);
		this.save();
	}
}

export class ObjData {
	guideBegin_audio_url: string = "";
	// guideEnd_audio_url: string = "";
	guideRestart_audio_url: string = "";
	question_arr: Question[] = [];
}

export class Question {
	question_audio_url: string = "";
	question_audio_url_end: string = "";
	group_arr_1: Group = new Group();
	group_arr_2: Group = new Group();
	option_arr: Option[] = [];
}

export class Group {
	group_audio_url: string = "";
	group_text: string = ""
}

export class Option {
	option_text: string = "";
	option_pic_url: string = "";
	option_audio_url: string = "";
	option_Idx: number = 0;
}

