<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\CuotaCond as CuotaCondResources;

class Condominio extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        $a_cond = [
            'idCond'                => strval($this->idCond),
            'idE'                   => strval($this->idE),
            'typeEmisionCond'       => $this->typeEmisionCond,
            'anoCond'               => $this->anoCond,
            'mesCond'               => $this->mesCond,
            'refCond'               => $this->refCond,
            'idTCond'               => strval($this->idTCond),
            'idTC'                  => strval($this->idTC),
            'tcEmision_dateVen'     => isset($this->dateVenTasa)?date("Y-m-d",strtotime($this->dateVenTasa)):'',
            'tcEmision_valor'       => $this->valorTC,
            'tcEmision_date'        => isset($this->dateValorTC)?date("Y-m-d",strtotime($this->dateValorTC)):'',
        ];

        if(isset($this->countCuotas)){
            $a_cond = array_merge($a_cond, ['countCuotas' => $this->countCuotas==0?'':$this->countCuotas]);
        }
        if(isset($this->condItems)){
            $a_cond = array_merge($a_cond, ['condItems' => $this->condItems]);
        }
        if(isset($this->cuotasCond)){
            $a_cond = array_merge($a_cond, ['cuotasCond' => CuotaCondResources::collection($this->cuotasCond)]);
        }

        if(isset($this->datose)){
            $datose = json_decode($this->datose);
            $a_entidad = [
                'codE'      => isset($datose->cod->value)?$datose->cod->value:'',
                'nameE'     => isset($datose->name->value)?$datose->name->value:'',
            ];
            $a_cond = array_merge($a_cond, $a_entidad);
        }

        return $a_cond;

    }
}
