<?php

namespace Modules\Travel\Transformers;

use Illuminate\Http\Resources\Json\JsonResource;

class TravelPlanResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id'                    => $this->id,
            'employee'              => [
                                        'id'          => $this->employee_id,
                                        'name'        => @$this->employee->name,
                                        'department'  => @$this->employee->department->title,
                                        'designation' => @$this->employee->designation->title,
                                        'avatar'      => uploaded_asset(@$this->employee->avatar_id),
                                    ],
            'from_date'             => $this->from_date,
            'to_date'               => $this->to_date,
            'from_location'         => $this->from_location,
            'to_location'           => $this->to_location,
            'purpose'               => $this->purpose,
            'amount'                => $this->amount,
            'signature'             => route('generate-travel-plan-signature', $this->id),
            'signature_date'        => $this->signature_date,
            'status'                => @$this->status->name,
            'approve'               => [
                'approved_by'       => @$this->approvedBy->name,
                'approved_at'       => $this->approved_at ? date('Y-m-d h:i A', strtotime($this->approved_at)) : null,
                'approved_remark'   => $this->approved_remark,
            ],
            'reject'                => [
                'rejected_by'       => @$this->rejectedBy->name,
                'rejected_at'       => $this->rejected_at ? date('Y-m-d h:i A', strtotime($this->rejected_at)) : null,
                'rejected_remark'   => $this->rejected_remark,
            ],
        ];
    }
}
