import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import { AppService } from "../../../../service/appHttpService";
import {Storage} from "@ionic/storage";
import {SurveyResultPage} from "../survey-result/survey-result";

@IonicPage()
@Component({
    selector: 'page-survey-preview',
    templateUrl: 'survey-preview.html',
})
export class SurveyPreviewPage {

    item;
    userId;
    survey;
    contentList;

    constructor(public navCtrl: NavController, public navParams: NavParams,
                public appService: AppService, public storage: Storage) {
    }

    ionViewDidLoad() {
        this.item = this.navParams.get('item');
        this.getInfo()
    }

    getInfo() {
        this.storage.get('user').then((res) => {
            this.userId = res.id;
            const data = {
                userId: this.userId,
                id: this.item.id
            };
            this.appService.ObserverHttpPost('/wisdomgroup/modules/question/showOnApp', data)
                .subscribe((res) => {
                    this.survey = res.json().data;
                    this.contentList = res.json().data.ques.datalist;
                    this.contentList.forEach(res=>{
                        res.quesDesc = JSON.parse(res.quesDesc);
                        if(res.quesType == 3){
                            res.option.answerdesc = res.option.answerdesc.split(',');
                        }
                    });
                })
        });

    }

    goToResult() {
        this.navCtrl.push('SurveyResultPage', {item: this.survey});
    }

}
