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(e => {
                        const selectArr =  e.option.answerdesc.split(",");
                        e.quesDesc = JSON.parse(e.quesDesc);
                        if (e.quesType == 3 && e.option) {
                            e.quesDesc.forEach(data=>{
                                if( selectArr.indexOf(data.index +"") != -1 ){
                                    data.select = true;
                                }else{
                                    data.select = false;
                                }
                            });
                        }
                    });
                })
        });

    }

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

}
