<?php

namespace ModulesGarden\Servers\OpenXchange\App\UI\Client\DeleteRequests\Pages;

use ModulesGarden\Servers\OpenXchange\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\DataTable\DataTable;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\DataTable\DataProviders\DataProvider;
use ModulesGarden\Servers\OpenXchange\App\UI\Client\DeleteRequests\Buttons\CancelRequestButton;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
use ModulesGarden\Servers\OpenXchange\App\Models\MailboxDeleteList;

class DeleteRequests extends DataTable implements ClientArea
{
    protected $id    = 'accounts';
    protected $name  = 'accounts';
    protected $title = null;


       /**
     * labels for statuses
     */
    const STATUS_LABEL = [
        'success' => 'success',
        'warning' => 'warning'
    ];
    /**
     * load columns
     */
    protected function loadHtml()
    {
        $this
        ->addColumn((new Column('id'))
            ->setOrderable(DataProvider::SORT_DESC)
            ->setSearchable(true, Column::TYPE_INT))
        ->addColumn((new Column('mailbox_name'))
            ->setOrderable()
            ->setSearchable(true))
        ->addColumn((new Column('days_to_delete'))
            ->setOrderable()
            ->setSearchable(true))
        ->addColumn((new Column('created_at'))
            ->setOrderable()
            ->setSearchable(true))
        ->addColumn((new Column('delete_date'))
            ->setOrderable()
            ->setSearchable(true));
    }

    /**
     * load buttons
     */
    public function initContent()
    {
        $this->addActionButton(new CancelRequestButton());
    }

    /**
     * load data
     */
    public function loadData()
    {
        $serviceId      = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
        $deleteRequests = MailboxDeleteList::getRequests($serviceId);

        $dataProv = new ArrayDataProvider();
        $dataProv->setDefaultSorting('id', 'ASC')->setData($deleteRequests);
        $this->setDataProvider($dataProv);
    }

    public function replaceFieldCreated_at($key, $row)
    {
        $deleteDate = (new \DateTime($row['created_at']))
            ->format('Y-m-d');

        return $deleteDate;
    }

    public function replaceFieldDelete_date($key, $row)
    {
        $deleteDate = (new \DateTime($row['created_at']))
            ->modify('+' . (int) $row['days_to_delete'] . ' days')
            ->format('Y-m-d');

        return $deleteDate;
    }
}
