<?php

namespace App\Livewire;

use App\Models\IpdCharge;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\Views\Column;

class IpdChargeTable extends LivewireTableComponent
{
    public $ipdDiagnosisId;

    //    public $showButtonOnHeader = true;
    //    public $buttonComponent = 'ipd_charges.add-button';
    public $showFilterOnHeader = false;

    protected $model = IpdCharge::class;

    protected $listeners = ['refresh' => '$refresh', 'resetPage'];

    // public function resetPage($pageName = 'page')
    // {
    //     $rowsPropertyData = $this->getRows()->toArray();
    //     $prevPageNum = $rowsPropertyData['current_page'] - 1;
    //     $prevPageNum = $prevPageNum > 0 ? $prevPageNum : 1;
    //     $pageNum = count($rowsPropertyData['data']) > 0 ? $rowsPropertyData['current_page'] : $prevPageNum;

    //     $this->setPage($pageNum, $pageName);
    // }

    public function configure(): void
    {
        $this->setPrimaryKey('id')
            ->setDefaultSort('ipd_charges.created_at', 'desc')
            ->setQueryStringStatus(false);
    }

    public function mount(int $ipdDiagnosisId): void
    {
        $this->ipdDiagnosisId = $ipdDiagnosisId;
    }

    public function placeholder()
    {
        return view('livewire.listing-skeleton');
    }

    public function columns(): array
    {
        return [
            Column::make(__('messages.ipd_patient_charges.date'), 'date')
                ->view('ipd_charges.columns.date')
                ->sortable()
                ->searchable(),
            Column::make(__('messages.ipd_patient_charges.charge_type_id'), 'charge_type_id')
                ->view('ipd_charges.columns.charge_type')
                ->sortable()
                ->searchable(),
            Column::make(__('messages.ipd_patient_charges.charge_category_id'), 'chargecategory.name')
                ->view('ipd_charges.columns.charge_category')
                ->sortable()
                ->searchable(),
            Column::make(__('messages.ipd_patient_charges.charge_id'), 'charge_id')
                ->view('ipd_charges.columns.code')
                ->sortable()->searchable(),
            Column::make(__('messages.ipd_patient_charges.standard_charge'), 'standard_charge')
                ->view('ipd_charges.columns.standard_charge')
                ->sortable()->searchable(),
            Column::make(__('messages.ipd_patient_charges.applied_charge'), 'applied_charge')
                ->view('ipd_charges.columns.applied_charge')
                ->sortable()->searchable(),
            Column::make(__('messages.common.action'), 'id')
                ->view('ipd_charges.columns.action'),
        ];
    }

    public function builder(): Builder
    {
        return IpdCharge::with(['chargecategory', 'charge'])->where('ipd_patient_department_id',
            $this->ipdDiagnosisId)
            ->select('ipd_charges.*');
    }
}
