<?php

class StranasTargetAnswer extends \Phalcon\Mvc\Model
{
    const STATUS_INIT = 0;
    const STATUS_VERIFIED = 1;

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("jaga");
        $this->setSource("stranas_survey_answer");
        $this->belongsTo("id_survey", "StranasSurvey", "id");
        $this->belongsTo("id_question", "StranasTarget", "id");
        $this->hasMany("id", "StranasSurveyAnswerAttachment", "id_answer");
        $this->hasMany("id", "StranasSurveyAnswerComment", "id_answer");
        // $this->belongsTo("created_by", "UmUser", "um_id");
        $this->belongsTo("created_by", "UmUser", "um_id", ['alias' => 'verificator']);
        $this->hasMany('id', 'StranasTargetAnswerWithAttachment', 'id_answer');
        // $this->hasMany('id', 'StranasTargetAnswerCommentWithAttachment', 'id_answer');
        // $this->hasMany('id', 'StranasTargetAnswerCommentWithAttachment', 'id_answer');
    }

    public function toArray($columns = null)
    {
        $result = parent::toArray($columns);
        $result['attachments'] = $this->StranasTargetAnswerWithAttachment;
        $result['answer_by'] = empty($this->verificator) ? null : $this->verificator->toArraySimple();
        // $result['answer_by'] = $this->UmUser;
        // $result['comments'] = $this->StranasTargetAnswerCommentWithAttachment;

        return $result;
    }

    // public function getDetail()
    // {
    //     $result = $this->toArray();
    //     $result['verificator'] = empty($this->verificator) ? null : $this->verificator->toArraySimple();

    //     return $result;
    // }

    // public function getDetail()
    // {
    //     $result = $this->toArray();

    //     $result['attachments'] = [];
    //     $result['attachments_verification'] = [];
    //     $result['comments'] = [];

    //     foreach ($this->stranasSurveyAnswerAttachment as $key => $answer_attachment) {
    //         if (!empty($answer_attachment->attachments)) {
    //             $key = $answer_attachment->tipe == StranasSurveyAnswerAttachment::TIPE_JAWABAN
    //                 ? 'attachments'
    //                 : 'attachments_verification';
    //             $result[$key][] = $answer_attachment->getDetail();
    //         }
    //     }

    //     foreach ($this->StranasSurveyAnswerComment as $key => $comment) {
    //         $result['comments'][] = $comment->getDetail();
    //     }

    //     $result['verificator'] = empty($this->verificator) ? null : $this->verificator->toArraySimple();

    //     return $result;
    // }
}