<?php

class JejakKasus extends \Phalcon\Mvc\Model
{
    public function initialize()
    {
        $this->setSchema('jaga');
        $this->setSource('jejak_kasus');
        $this->belongsTo(
            'wilayah_id',
            '\MasterWilayah',
            'id',
            ['alias' => 'wilayah']
        );
        $this->hasMany('id', 'JejakKasusAttachment', 'id_jejak_kasus');
    }

    public function toArray($columns = null)
    {
        $result = parent::toArray($columns);
        $result['attachments'] = [];

        // $result['attachments'] = $this->jejakKasusAttachment;
        foreach ($this->jejakKasusAttachment as $key => $answer_attachment) {
            $result['attachments'][] = $answer_attachment->getDetail();
        }

        return $result;
    }

    public function toArrayDetail($columns = null)
    {
        $result = parent::toArray($columns);
        
        $result['wilayah_nama'] = '';
        $wilayah = MasterWilayah::findFirst(
            [
                "id = :wilayah_id:",
                "bind" => [
                    "wilayah_id" => $result['wilayah_id']
                ]
            ]
        );
        if(!empty($wilayah)) {
            $result['wilayah_nama'] = $wilayah->name;
        }

        return $result;
    }

    public function isPublished()
    {
        return $this->published > 0;
    }

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

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

    //     foreach ($this->jejakKasusAttachment as $key => $answer_attachment) {
    //         if (!empty($answer_attachment->attachments)) {
    //             $result['attachments'] = $answer_attachment->getDetail();
    //         }
    //     }

    //     return $result;
    // }
}
