<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Filament\Resources\DberpSupplierSettlementQueries\Pages;

use App\Filament\Resources\DberpSupplierSettlementQueries\DberpSupplierSettlementQueryResource;
use App\Services\DberpPermissionService;
use App\Support\DberpSettlementEntryFilamentSupport;
use Filament\Actions\Action;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Facades\Auth;

class ListDberpSupplierSettlementQueries extends ListRecords
{
    protected static string $resource = DberpSupplierSettlementQueryResource::class;

    public function getSubheading(): string|Htmlable|null
    {
        return __('dberp.resources.supplier_settlement_query.scope_hint');
    }

    protected function getHeaderActions(): array
    {
        $permissions = app(DberpPermissionService::class);
        $user = Auth::user();
        $tableFilters = is_array($this->tableFilters) ? $this->tableFilters : [];

        return [
            Action::make('supplierPayables')
                ->label(__('dberp.resources.supplier_settlement_query.link_supplier_payables'))
                ->url(DberpSettlementEntryFilamentSupport::supplierPayablesUrl($tableFilters))
                ->visible(fn (): bool => $permissions->allows($user, DberpPermissionService::VIEW_SUPPLIER_PAYABLE)),
            Action::make('receiptPaymentQuery')
                ->label(__('dberp.resources.supplier_settlement_query.link_receipt_payment_query'))
                ->url(DberpSettlementEntryFilamentSupport::receiptPaymentQueryUrl($tableFilters, 'payment'))
                ->visible(fn (): bool => $permissions->allowsAny($user, [
                    DberpPermissionService::VIEW_SUPPLIER_PAYABLE,
                    DberpPermissionService::CREATE_PAYMENT_ORDER,
                    DberpPermissionService::VIEW_WORKBENCH_OVERVIEW,
                ])),
            Action::make('settlementWriteOffs')
                ->label(__('dberp.resources.supplier_settlement_query.link_write_offs'))
                ->url(DberpSettlementEntryFilamentSupport::settlementWriteOffsUrl($tableFilters, 'payable'))
                ->visible(fn (): bool => $permissions->allows($user, DberpPermissionService::WRITE_OFF)),
            Action::make('settlementStatements')
                ->label(__('dberp.resources.supplier_settlement_query.link_statements'))
                ->url(DberpSettlementEntryFilamentSupport::settlementStatementsUrl($tableFilters, 'payable'))
                ->visible(fn (): bool => $permissions->allowsAny($user, [
                    DberpPermissionService::VIEW_SETTLEMENT_STATEMENT,
                    DberpPermissionService::CONFIRM_SETTLEMENT_STATEMENT,
                ])),
        ];
    }
}
