<?php

namespace App\Http\Resources;

use App\Models\AccReceivables;
use Illuminate\Http\Resources\Json\JsonResource;

class LoanCollectionResource extends JsonResource
{

    public function toArray($request)
    {
        return [
            'id'                  => $this->id,
            'loan_id'             => $this->loan_id,
            'currency_id'         => $this->currency_id,
            'payment_date'        => $this->payment_date,
            'received_date'       => $this->received_date,
            'principal_late'      => $this->principal_late??0,
            'interest_late'       => $this->interest_late??0,
            'operat_late'         => $this->operating_service_late??0,
            'maintenance_late'    => $this->maintenance_late??0,
            'received_late'       => $this->received_late??0,
            'unreceived_late'     => $this->unreceived_late??0,
            'date_late'           => $this->total_late['start_date']??'',
            'total_late'          => $this->total_late['total_late']??0,
            'total_penalty'       => $this->total_late['total_penalty']??0,
            'loan_code'           => $this->loan->loan_code,
            'client_code'         => $this->client->code,
            'client_name'         => $this->client->name,
            'client_phone'        => $this->client->phone,
            'installment_date'    => $this->loan->installment_date??'',
            'expired_date'        => $this->loan->expired_date??'',
            'term_type'           => $this->loan->term_type??'',
            'level_loan'          => $this->loan->level_loan??'',
            'loan_out'            => $this->loan->loan_out??0,
            'balance_principal'   => $this->loan->balance_principal??0,
            'period'              => $this->loan->period??'',
            'period_paid'         => AccReceivables::where('loan_id',$this->loan_id)->where('payment_status_id',1)->count()??0,
            'period_unpaid'       => AccReceivables::where('loan_id',$this->loan_id)->whereIn('payment_status_id',[2,3])->count()??0,
            'village'             => $this->client->village_id!=null?'ភូមិ'.$this->client->village_kh:'',
            'currency_name'       => $this->currency_id==1?'រៀល':'ដុល្លារ',
            'co_name'             => $this->co->name,
            'co_phone'            => $this->co->contact_number,

        ];
    }
}
