<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class SalesOrderResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return [

            'id' => $this->id,

            'order_no' => $this->order_no,

            'user_id' => $this->user_id,

            'created_by_name' => $this->user?->name,

            'sales_person_id' => $this->user_id,

            'client_id' => $this->client_id,

            'client_name' => $this->client?->client_name,

            'order_date' => optional($this->order_date)->format('Y-m-d'),

            'delivery_date' => optional($this->delivery_date)->format('Y-m-d'),

            'total_amount' => $this->total_amount,

            'subtotal' => $this->total_amount,

            'discount_amount' => $this->discount_amount,

            'tax_amount' => $this->tax_amount,

            'grand_total' => $this->grand_total,

            'remarks' => $this->remarks,

            'payment_terms' => $this->payment_terms,

            'status' => $this->status,

            'items' => SalesOrderItemResource::collection(
                $this->whenLoaded('items')
            ),

            'created_at' => optional($this->created_at)
                ->format('d-m-Y H:i')

        ];
    }
}