<?php

namespace App\Http\Resources;

use App\Http\Resources\GuarantorResource;
use Illuminate\Http\Resources\Json\JsonResource;

class CollateralDetailCustomResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */

    // status_type = 1,2 ដីតូប
    // status_type = 3,4 ប្រភេទយានយន្ដ (២)
    //status_type = 5 គ្រឿងអលង្ការ និងសម្ភារៈមានតម្លៃ

    public function toArray($request)
    {
        if($request->status_type == 1){
             return $this->CustomLand();
        }else if($request->status_type == 3){
            return $this->CustomVehicle();
        }else{
            return $this->CustomValuablematerial();
        }
    }

    public function CustomLand(){
        $guarantor = $this->client->guarator_1->first();
        return [
            'id'                        => $this->id,
            'collateral_id'             => $this->collateral_id,
            'title'                     => $this->collateral->title??'',
            'number'                    => $this->number,
            'created_at'                => $this->created_at,
            'model_type'                => $this->model_type,
            'pawn_type'                 => $this->pawn_type,
            'date_made'                 => $this->date_made,
            'issued_by'                 => $this->issued_by,
            'size'                      => $this->size,
            'location'                  => $this->location,
            'status'                    => $this->status,
            'north'                     => $this->north,
            'west'                      => $this->west,
            'south'                     => $this->south,
            'east'                      => $this->east,
            'authority_price'           => $this->authority_price,
            'price_urgent'              => $this->price_urgent,
            'other_price'               => $this->other_price,
            'price_market'              => $this->price_market,
            'price_market'              => $this->price_market,

            'client'                    => $this->customClientField()??'',
            'guarantor_1'               => new GuarantorResource($guarantor)??'',

        ];

    }

    public function CustomVehicle(){
        return [
            'id'                => $this->id,
            'collateral_id'     => $this->collateral_id,
            'title'             => $this->collateral->title??'',
            'model_type'        => $this->model_type,
            'pawn_type'         => $this->pawn_type,
            'number_plate'      => $this->number_plate,
            'color'             => $this->color,
            'latabat'           => $this->latabat,
            'body_number'       => $this->body_number,
            'engine_number'     => $this->engine_number,
            'cylinder_size'     => $this->cylinder_size,
            'year'              => $this->year,
            'date_made'         => $this->date_made,
            'price_market'      => $this->price_market,
            'price_urgent'      => $this->price_urgent,
            'other_price'       => $this->other_price,
            'client'            => $this->customClientField()??'',
            'guarantor_name'    => $this->client->guarator_1->first()->name??''
        ];
    }

    public function CustomValuablematerial(){
        return [
            'id'               => $this->id,
            'collateral_id'    => $this->collateral_id,
            'title'            => $this->collateral->title??'',
            'model_type'       => $this->model_type,
            'pawn_type'        => $this->pawn_type,
            'color'            => $this->color,
            'price_market'     => $this->price_market,
            'number_plate'     => $this->number_plate,
            'head_land'        => $this->head_land,
            'client'           => $this->customClientField()??'',
        ];
    }

    public function customClientField(){
        return [

            'cleint_id'        => $this->client->id,
            'code'             => $this->client->code,
            'occupation'             => $this->client->occupation,
            'branch_id'        => $this->client->branch_id,
            'name'             => $this->client->name,
            'name_en'          => $this->client->name_en,
            'gender'           => $this->client->gender,
            'phone'            => $this->client->phone,
            'type_document'    => $this->client->type_document,
            'id_card'          => $this->client->id_card,
            'issued_by'        => $this->client->issued_by,
            'relationship'     => $this->client->relationship,
            'date_of_birth'    => $this->client->date_of_birth,
            'place_of_birth'   => $this->client->place_of_birth,
            'create_card_date' => $this->client->create_card_date,
            'work_place'       => $this->client->work_place,
            'address'          => $this->client->address,
            'client_province_kh'        => $this->client->province->name_km??'',
            'client_district_kh'        => $this->client->district->name_km??'',
            'client_commune_kh'         => $this->client->commune->name_km??'',
            'client_village_kh'         => $this->client->village->name_km??'',

        ];
    }
}
