<?php

namespace modules\custom\models;

use Yii;
use yii\helpers\{Html, Arrayhelper, Url};
use Yiisoft\Json\Json;
/**
 * This is the model class For table "activities_items".
 *
 * @property string $id
 * @property string $language
 * @property string $created_by
 * @property string $updated_by
 * @property string $deleted_by
 * @property string $relatedid
 * @property string $product
 * @property string $serviceid
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $deleted_at
 * @property string $status
 * @property string $type
 */
class Asset_data extends \common\models\DynamicModel
{

    public static function localattributes(){
        return [
            'last_service' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "Last Service",),
                'input' => 'default',
            ],
            'first_actual' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "First Actual Service",),
                'input' => 'default',
            ],
            'first_service' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "First Service",),
                'input' => 'default',
            ],
            'next_service' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "Next Service",),
                'input' => 'default',
            ],
            'last_service_id' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "Last Service ID",),
                'input' => 'default',
            ],
            'last_service_age' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "Time Since Last Service",),
                'input' => 'default',
            ],
            'service_due' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "Next Service Due",),
                'input' => 'default',
            ],
            'no_of_services' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "No Of Services",),
                'input' => 'default',
            ],
            'mail_sent' => [
                'rules' => [
                    ['string']
                ],
                'label' => Yii::t('app', "Mail Sent",),
                'input' => 'default',
            ],
        ];
    }

    public static function Data($data){
		$output = new self();
		$content = is_string($data) ? Json::decode($data, 0) : $data;
    	if (is_array($content) == true){
           $output->attributes = $content;
        } elseif(!empty($content)) {
            foreach ($output->attributes as $name => $name){
                property_exists($content, $name) ? $output->$name = $content->$name : false;
            }
        }
        foreach($output->attributes as $field => $data){
            $format = str_contains($data, '/') ? 'd/m/Y' : 'Y-m-d';
            $date = !empty($data) ? \Datetime::createfromformat($format, $data) : null;
            $output->$field = !empty($date) ? $date->format('d/m/Y') : null;
        }
        return $output;
	}
		

    public function Dates(Assetsbe $asset){
		$purchasedate = formatDate($asset->purchasedate);
		$fs = json_decode($asset->getFirstservice());
		$ls = json_decode($asset->getLastservice());
		$this->first_actual = formatDate(!empty($fs?->date_of_service) ?  $fs?->date_of_service : $this->first_actual );
        $this->no_of_services = $asset->getNoofservices();
		if($ls && !empty($ls?->date_of_service)) {
          	$this->last_service = formatDate($ls?->date_of_service);
			$this->next_service = match(strlen($ls->date_of_next_service)){
                10 => formatDate($ls->date_of_next_service, 'd/m/Y'),
                7 => formatDate(substr($ls->date_of_service, 0,2).'/'.$ls->date_of_next_service, 'd/m/Y'),
                default => null,
            };            
		} else {
            $this->first_service = formatDate($this->first_service);
            $this->last_service = formatDate($this->last_service);
			$this->next_service = !empty($this->next_service) ?  formatDate($this->next_service) : null;
		}
		if((empty($this->first_service) || empty($this->next_service) && !empty($purchasedate)) ){
			$pdate = strtotime(str_replace('/','-', $purchasedate));
            if(!empty(trim($pdate))){
                $years = $asset->assettype == 'lifejacket' ? 2 : ($asset->assettype == 'liferaft' ? 3 : 2);
                $first = date('Y-m-d', strtotime("+$years years", $pdate));
                $this->first_service = formatDate($first);
                $this->next_service = formatDate($first);
            }
		}
        return $this;
    }
}

