<?php

namespace App\Http\Resources;

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

class ShowPaymentResource extends JsonResource
{

    public function toArray($request)
    {

        $today = date('Y-m-d');
        $total_penalty = 0;
        $payment_date = Carbon::parse(date('Y-m-d', strtotime($this->payment_date)));
        $diff_date    = Carbon::parse(date('Y-m-d', strtotime($today)));

        $interval   = $payment_date->diff($diff_date);
        $total_late = intval($interval->format("%R%a"));
        if($total_late>0) {
            if($this->currency_id==1) {
                $total_penalty = round($total_late*1500,-2,PHP_ROUND_HALF_ODD);
            }
            else{
                $total_penalty = round($total_late*0.375,-2,PHP_ROUND_HALF_ODD);
            }
        }
        else{
            $total_penalty=0;
        }
        $balance_principal_new = $this->loan_balance+$this->payment_per_month;
        $penalty_pay_off       = round(0.2*$this->loan_balance,-2,PHP_ROUND_HALF_ODD);

        $fast_slow = '';
        if($this->payment_status_id==1 || $this->payment_status_id==2){
            $interval = $payment_date->diff($this->received_date);
            $fast_slow = intval($interval->format("%R%a"));
        } else{
            $fast_slow = '';
        }

        return [
            'id'                   => $this->id,
            'loan_id'              => $this->loan_id,
            'loan_code'            => $this->loan->loan_code,
            'client_name'          => $this->client->name,
            'client_code'          => $this->client->code,
            'client_phone'         => $this->client->phone,
            'photo'                => $this->client->photo,
            'client_id'            => $this->client_id,
            'currency_id'          => $this->currency_id,
            'co_name'              => $this->co->name,
            'payment_date'         => $this->payment_date,
            'received_date'        => $this->received_date,
            'loan_out'             => $this->loan->loan_out??0,
            'total_loan_balance'   => $this->loan->loan_balance??0,
            'balance_principal'    => $this->loan->balance_principal??0,
            'principal'            => $this->principal,
            'operating_service'       => $this->operating_service,
            'maintenance_per_month'=> $this->maintenance_per_month,
            'interest_per_month'   => $this->interest_per_month,
            'payment_per_month'    => $this->payment_per_month,
            'received'             => $this->received,
            'penalty'              => $this->penalty??0,
            'unreceived'           => $this->unreceived,
            'loan_balance'         => $this->loan_balance,
            'open_close'           => $this->open_close,
            'payment_method'       => $this->payment_method,
            'balance_principal_new'=> $balance_principal_new,
            // 'total_penalty'        => $total_penalty,
            'fast_slow'            => $fast_slow??'',
            'total_penalty'        => 0,
            'payment_status_id'    => $this->payment_status_id?$this->payment_status_id:'',
            'status_title'         => $this->payment_status->title?$this->payment_status->title:'',
            'color_code'           => $this->payment_status->color?$this->payment_status->color:'',
            'total_late'           => $total_late,
            'waiv_penalty'         => $this->waiv_penalty,
            'unpaid_penalty'       => $this->unpaid_penalty,
            'level_loan'           => $this->loan->level_loan??'',
            'currency_name'        => $this->currency_id==1?'រៀល':'ដុល្លារ',
            'address'              => $this->client->village_id!=null?$this->client->village->full_name_km:'',
            'details'              => AccReceivableDetailResource::collection($this->accReceivableDetails),
            'penalty_pay_off'      => $penalty_pay_off??0,
        ];
    }
}
