<?php

namespace apexl\Io\modules\invoiceDisplay\components;

use apexl\Io\includes\Hook;
use apexl\Io\includes\Routes;
use apexl\Io\modules\display\components\BasicLink;
use apexl\Io\modules\display\components\CardEntityFilteredDisplayTable;
use apexl\Io\modules\display\components\ColWrapper;
use apexl\Io\modules\display\components\ContentTitle;
use apexl\Io\modules\display\components\DisplayTable;
use apexl\Io\modules\display\components\EntityFilteredDisplayTable;
use apexl\Io\modules\display\components\FormComponent;
use apexl\Io\modules\display\components\genericComponents;
use apexl\Io\modules\display\components\SimpleButton;
use apexl\Io\modules\user\entities\roleEntity;
use apexl\Io\modules\user\entities\userEntity;
use apexl\Io\modules\user\services\currentUser;
use apexl\Io\modules\user\services\Permissions;

class dashboardTiles
{
    public static function userInvoicesTile($userEntity=null, $startDate=null, $endDate=null)
    {
        $helpComponents = [
            (new ContentTitle())->addClass('text-black text-start help-modal-content-title')->addTitle('A list of customer invoiceS'),
            (new DisplayTable())->addClass('text-black text-start help-modal-display-table')->addRows([
                ['Invoice Date', 'Date the Invoice was raised.'],
                ['Invoice No.', 'Reference'],
                ['Customer', 'Customer email address'],
                ['Type', 'Whether this invoice was for a Purchase or Subscription'],
                ['Total', 'Total value of invoice, excluding VAT'],
                ['VAT', 'VAT due on the goods/services purchased'],
                ['Paid', 'Whether the invoice has been paid (yes/no)'],
                ['Last Payment Attempt', 'The last time an attempt was made to collect payment for this invoice'],
                //['Abandoned?', 'Whether this invoice has been marked as abandoned'],
            ])
        ];

        $params = [];
        if (isset($userEntity->email)) $params['userEmail'] = $userEntity->email;
        if ($startDate instanceof \DateTime) $params['startDate'] = $startDate->format('Y-m-d');
        if ($endDate instanceof \DateTime) $params['endDate'] = $endDate->format('Y-m-d');

        $wrapper = (new ColWrapper())
            ->addClass('col-12')
            ->addComponent((new CardEntityFilteredDisplayTable())
                ->addTitle('Latest Invoices')
                ->setFilters('orderBy[]=invoice_date&orderBy[]=DESC')
                ->src(Routes::getRoutePattern('invoice.data.basic').'?'. http_build_query($params))
                ->displayPagination(true, false, true, 'inside')
                ->displayShortTotals(false)
                ->addProperty('refreshButton', '<i class="fa-light fa-arrows-rotate"></i>')
                ->hideIdColumn()
                ->setRefreshTag('user-invoices-table')
                ->addHeaderModal('Latest Invoices Table', $helpComponents)
                ->addDropMenuItem('provider.invoices.view', 'view', '', 'get', '_blank')
            );

        $currentUser = currentUser::getCurrentUser();
        if ($currentUser->isAllowed('administer') && isset($userEntity->id) && $userEntity->id) {
            $wrapper->addComponent(
                (new SimpleButton())
                    ->addRoute(Routes::getRoutePattern('provider.invoices.backfill', ['userId' => $userEntity->id]))
                    ->addText('Backfill Provider Invoices')
                    ->addClass('btn btn-primary float-end mb-5')
                    ->addProperty('asynch', 'POST')
            );
        }

        return $wrapper;
    }

}