<?php

namespace App\Http\Resources;

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

class CollectionLoanResource extends JsonResource
{

    private function funTermType($value){
        if($value==1){
            return 'ថ្ងៃ';
        }else if($value==2){
            return 'សប្ដាហ៍';
        }else if($value==3){
            return 'កន្លះខែ';
        }else if($value==4){
            return 'ខែ';
        }
    }

    public function toArray($request)
    {
        return [
            'id'                  => $this->id,
            'loan_id'             => encrypt_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,
            'operating_service_late'=> $this->operating_service_late??0,
            'maintenance_late'    => $this->maintenance_late??0,
            'received_late'       => $this->received_late??0,
            'unreceived_late'     => $this->unreceived_late??0,
            'count_unreceived'    => $this->count_unreceived??0,
            'count_late'          => $this->count_late??'',
            '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,
            // 'guarantor_1'         => $this->client->guarantor_1->first()->name??'',
            // 'phone_guarantor_1'   => $this->client->guarantor_1->first()->phone??'',
            // 'guarantor_2'         => $this->client->guarators->first()->name??'',
            // 'phone_guarantor_2'  => $this->client->guarators->first()->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??'',
            'co_name'             => $this->loan->co->name??'',
            'loan_out'            => $this->loan->loan_out??0,
            'period'              => $this->loan->period??0,
            'total_loan_balance'  => $this->loan->term_type==1?$this->loan->loan_balance:$this->loan->balance_principal+($this->loan->total_interest/$this->loan->period)*2,
            'balance_principal'   => $this->loan->balance_principal??0,
            'dept_period'         => $this->loan->period - $this->num+1,
            'village'             => $this->client->village_id!=null?'ភូមិ'.$this->client->village_kh:'',
            'term_type_name'      => $this->funTermType($this->loan->term_type),
            'count_period_unpaid' => AccReceivables::where('loan_id',$this->loan_id)->whereIn('payment_status_id',[2,3])->count()??0,
            'total_period'        => AccReceivables::where('loan_id',$this->loan_id)->whereIn('payment_status_id',[1,2,3])->count()??0,
            'last_received_date'  => AccReceivables::where('loan_id',$this->loan_id)->whereIn('payment_status_id',[1,2])->max('received_date')??'',
            'branch_name'         => $this->branchs->name_km??'',
        ];
    }
}
