<?php

namespace App\Livewire\Reports;

use App\Models\Account;
use App\Models\Party;
use App\Models\Setting;
use Livewire\Component;
use Livewire\Attributes\Layout;

#[Layout('layouts.app')]
class AccountsReport extends Component
{
    public $party_id = '';
    
    public $columns = [
        'party' => true,
        'currency' => true,
        'account_number' => true,
        'name' => true,
        'notes' => true,
        'is_active' => true,
        'created_at' => true,
    ];

    public function toggleColumn($column)
    {
        if (array_key_exists($column, $this->columns)) {
            $this->columns[$column] = !$this->columns[$column];
        }
    }

    public function render()
    {
        if (!auth()->user()->can('view records')) {
            abort(403);
        }

        $parties = Party::orderBy('name')->get();
        
        $query = Account::with(['party', 'currency'])->orderBy('id', 'asc');
        
        if ($this->party_id) {
            $query->where('party_id', $this->party_id);
        }
        
        $accounts = $query->get();
        $settings = Setting::pluck('value', 'key')->toArray();

        return view('livewire.reports.accounts-report', [
            'accounts' => $accounts,
            'parties' => $parties,
            'settings' => $settings,
        ]);
    }
}
