<?php

class KorsupgahPeriode extends \Phalcon\Mvc\Model
{
    public $id;
    public $nama;
    public $keterangan;
    public $started_at;
    public $ended_at;
    public $created_at;
    public $updated_at;
    public $deleted_at;
    public $created_by;
    public $updated_by;
    public $bpkp_kemdagri_verif_started_at;
    public $bpkp_kemdagri_verif_ended_at;
    public $korsupgah_verif_started_at;
    public $korsupgah_verif_ended_at;
    public $pic_verif_started_at;
    public $pic_verif_ended_at;

    public function initialize()
    {
        $this->setSchema('jaga');
        $this->setSource('korsupgah_periode');
        $this->hasMany('id', 'KorsupgahSurvey', 'id_periode');
    }

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

        $result['survey_list'] = $this->korsupgahSurvey;

        return $result;
    }

    /**
     * Cek apakah saat ini user verifikator bpkp kemdagri boleh melakukan verifikasi
     * @return bool
     * @throws Exception
     */
    public function verifyTimeValidForBpkpKemdagri() {
        $isAllowed = true;
        if (!empty($this->bpkp_kemdagri_verif_started_at) && !empty($this->bpkp_kemdagri_verif_ended_at)) {
            $start = new DateTime($this->bpkp_kemdagri_verif_started_at);
            $end = new DateTime($this->bpkp_kemdagri_verif_ended_at);
            $now = new DateTime();

            if ($now < $start || $now > $end) {
                $isAllowed = false;
            }
        }
        return $isAllowed;
    }

    /**
     * @return bool
     * @throws Exception
     */
    public function verifyTimeValidForKorsupgah() {
        $isAllowed = true;
        if (!empty($this->korsupgah_verif_started_at) && !empty($this->korsupgah_verif_ended_at)) {
            $start = new DateTime($this->korsupgah_verif_started_at);
            $end = new DateTime($this->korsupgah_verif_ended_at);
            $now = new DateTime();

            if ($now < $start || $now > $end) {
                $isAllowed = false;
            }
        }
        return $isAllowed;
    }

    /**
     * Cek apakah saat ini user verifikator PIC Korsup boleh melakukan verifikasi
     * @return bool
     * @throws Exception
     */
    public function verifyTimeValidForPic() {
        $isAllowed = true;
        if (!empty($this->pic_verif_started_at) && !empty($this->pic_verif_ended_at)) {
            $start = new DateTime($this->pic_verif_started_at);
            $end = new DateTime($this->pic_verif_ended_at);
            $now = new DateTime();

            if ($now < $start || $now > $end) {
                $isAllowed = false;
            }
        }
        return $isAllowed;
    }
}
