<?php

class KorsupgahSurvey extends \Phalcon\Mvc\Model
{

    public function initialize()
    {
        $this->setSchema("jaga");
        $this->setSource("korsupgah_survey");
        $this->belongsTo("id_periode", "KorsupgahPeriode", "id");
        $this->belongsTo("id_template", "KorsupgahTemplate", "id");
        $this->belongsTo("id_instansi", "Instansi", "id");
        $this->belongsTo("id_pengampu", "KorsupgahPengampu", "id");
        $this->hasMany("id", "KorsupgahAnswer", "id_survey");
    }

    public function getDetail($simpleAnswer = false)
    {
        $result = $this->toArray();
        $result['periode'] = $this->korsupgahPeriode;
        $result['instansi'] = $this->instansi;

        if(array_key_exists('ids_sub_indikator_json', $this)) $this->korsupgahTemplate->ids_sub_indikator_json = $this->ids_sub_indikator_json;
        $result['template'] = $this->korsupgahTemplate->getDetail();
        $result['answers'] = [];
        $padTarget = KorwilPadTarget::findFirst([
            "conditions" => "id_instansi =" . $this->instansi->id
        ]);
        $asetTarget = KorwilAsetTarget::findFirst([
            "conditions" => "id_instansi =" . $this->instansi->id
        ]);
        $result['pad_target'] = $padTarget;
        $result['aset_target'] = $asetTarget;
        $answer = $this->korsupgahAnswer;

        foreach ($answer as $key => $a) :
            $result['answers'][] = $simpleAnswer ? $a->toShortArray() : $a->toArray();
        endforeach;

        return $result;
    }

    public function overview()
    {
        $result = $this->toArray();
        $result['instansi'] = $this->instansi;
        $result['periode'] = $this->korsupgahPeriode;
        $result['template'] = $this->korsupgahTemplate;

        return $result;
    }
}
