<?php

namespace Xentral\Modules\EPost\DataTable;

use ApplicationCore;
use Xentral\Components\Database\Database;
use Xentral\Components\Database\SqlQuery\SelectQuery;
use Xentral\Widgets\DataTable\Column\Column;
use Xentral\Widgets\DataTable\Column\ColumnCollection;
use Xentral\Widgets\DataTable\Feature\DebugFeature;
use Xentral\Widgets\DataTable\Feature\FeatureCollection;
use Xentral\Widgets\DataTable\Filter\CustomFilter;
use Xentral\Widgets\DataTable\Filter\FilterCollection;
use Xentral\Widgets\DataTable\Options\DataTableOptions;
use Xentral\Widgets\DataTable\Request\DataTableRequest;
use Xentral\Widgets\DataTable\Type\AbstractDataTableType;

class EPostListTable extends AbstractDataTableType
{
    public function configureOptions(DataTableOptions $options)
    {
        parent::configureOptions($options); // TODO: Change the autogenerated stub
    }

    public function configureQuery(SelectQuery $query)
    {
        parent::configureQuery($query); // TODO: Change the autogenerated stub
        $app = new ApplicationCore();
        $query
            ->from('rechnung AS a')
            ->cols([
                'a.id',
                'CONCAT(\'<input type="checkbox" name="select-\', a.id, \'">\') AS auswahl',
                'a.belegnr',
                'IF(e.id IS NULL AND false, "-", DATE_FORMAT(a.datum,\'%d.%m.%Y\')) AS datum',
                'IF(e.id IS NULL, "nicht uebertragen", e.status) AS status',
                'IF(e.id IS NULL, "-", e.datei) AS datei',
                'IF(e.id IS NULL, 
                  CONCAT(\'<a href="index.php?module=epost&action=uebertragen&rechnung=\', a.id, \'"><img src="themes/new/images/forward.png"></a>\'), 
                  CONCAT(\'<a href="index.php?module=epost&action=download&id=\', e.id, \'"><img src="themes/new/images/download.png"></a>\')
                 ) AS download',
                'a.name',
                'a.kundennummer',
                'a.land',
                'a.projekt',
                'a.zahlungsweise',
                'a.zahlungsstatus',
                'a.status AS belegstatus',
                'a.soll AS betrag',
                'p.abkuerzung AS projekt',
                'a.soll - a.ist AS differenz'
            ])
            ->leftJoin(
                'epost_files AS e',
                'a.id=e.rechnung'
            )
            ->leftJoin(
                'projekt AS p',
                'a.projekt = p.id'
            );
    }

    public function configureColumns(ColumnCollection $columns)
    {
        parent::configureColumns($columns); // TODO: Change the autogenerated stub

        $columns->add(Column::visible('auswahl', 'Auswahl'));
        $columns->add(Column::searchable('belegnr', 'Rechnung'));
        $columns->add(Column::sortable('datum', 'Vom'));
        $columns->add(Column::searchable('kundennummer', 'Kd-Nr.'));
        $columns->add(Column::searchable('name', 'Kunde'));
        $columns->add(Column::searchable('land', 'Land'));
        $columns->add(Column::searchable('projekt', 'Projekt'));
        $columns->add(Column::searchable('zahlungsweise', 'Zahlung'));
        $columns->add(Column::searchable('betrag', 'Betrag (Brutto)'));
        $columns->add(Column::searchable('zahlungsstatus', 'Zahlstatus'));
        $columns->add(Column::sortable('differenz', 'Differenz'));
        $columns->add(Column::searchable('belegstatus', 'Belegstatus'));
        $columns->add(Column::sortable('status', 'Status'));
        $columns->add(Column::sortable('datei', 'Datei'));
        $columns->add(Column::visible('download', 'Download'));
    }

    public function configureFeatures(FeatureCollection $features)
    {
        parent::configureFeatures($features); // TODO: Change the autogenerated stub
        // $features->add(new DebugFeature());
    }

    public function configureFilters(FilterCollection $filters)
    {
        $closure = function (SelectQuery $query, DataTableRequest $request) {
            $filter = $request->getParams()->getFilterValues();
            if ($filter['epost_auch_versendete'] === 'false') {
                $query->where('a.status != "versendet"');
            }
            if (!empty($filter['epost_date'])) {
                $date = new \DateTime($filter['epost_date']);
                $query->where('DATE(a.datum) = :date')
                    ->bindValue('date', $date->format('Y-m-d'));
            }
        };
        $filters->add(new CustomFilter($closure));
    }

    protected function addDefaultFeatures(FeatureCollection $featureCollection)
    {
        parent::addDefaultFeatures($featureCollection); // TODO: Change the autogenerated stub
    }

}