<?php

namespace App\DataTables\USSD;

use App\User;
use App\UssdService;
use App\UssdServiceType;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Services\DataTable;

class USSDServiceDatatable extends DataTable
{


    /**
     * USSDServiceDatatable constructor.
     */
    public function __construct()
    {
        $this->printColumns = ['Name', 'Code', 'Service Type'];
        $this->exportColumns = $this->printColumns;
        $this->excludeFromExport = ['Name'];
    }

    public function ajax()
    {
        return datatables()
            ->eloquent($this->query())
            ->addColumn('Service Type', function ($row) {
                return $row->ussd_service_type->name;
            })
            ->addColumn('Name', function ($row) {
                return $row->name;
            })
            ->addColumn('Code', function ($row) {
                return $row->code;
            })
            ->addColumn('action', function ($row) {
                $default = route('service.response.report', ['id' => $row->id]);
                $sales_route = route('service.sales.report', $row->id);
                $type = $row->ussd_service_type->name ?? null;
                $responses = '<a title="USSD RESPONSES" class="btn mr-5 btn-sm btn-primary" href="' . $default . '"><i class="fa fa-book"></i></a>';


                return '<div class="btn-group">' .
                    '<a title="View" class="btn mr-5 btn-sm btn-success" href="' . route('service.show', ['id' => $row->id]) . '"><i class="fa fa-desktop"></i></a>' .
                    '<a title="Dashboard" class="btn mr-5 btn-sm btn-primary" href="' . route('service.report.dashboard', ['id' => $row->id]) . '"><i class="fa fa-line-chart"></i></a>' .
                    '<a title="Report" class="btn mr-5 btn-sm btn-info" href="' . (in_array($type, ['Collection', 'Sales']) ? $sales_route : $default) . '"><i class="fa fa-archive"></i></a>' .
                    (in_array($type, ['Collection', 'Sales']) ? $responses : null) .
                    '<a title="Payment Subscription" class="btn mr-5 btn-sm btn-warning ' . (auth()->user()->under_reseller() ? 'disabled' : '') . '"
                     href="' . route('service.ussd.payment.subscription', ['id' => $row->id]) . '"><i class="fa fa-money"></i></a>' .
                    '<a title="Delete " class="btn btn-sm btn-danger" href="' . route('service.delete', ['id' => $row->id]) . '"' .
                    'onclick="return confirm(\'Do you want to delete this Service?\')"><i class="fa fa-trash"></i></a>' .
                    '</div>';
            })->make(true);

    }

    /**
     * Get the query object to be processed by dataTables.
     *
     */
    public function query()
    {
        $query = UssdService::query()->where('sms_username', auth()->user()->username);
        return $this->applyScopes($query);
    }

    /**
     * Optional method if you want to use html builder.
     *
     * @return \Yajra\Datatables\Html\Builder
     */
    public function html()
    {
        return $this->builder()
                    ->columns($this->getColumns())
                    ->ajax('')
                    ->addAction(['width' => '210px'])
                    ->dom('Bfrtip')
                    ->buttons(
                        Button::make('export'),
                        Button::make('print'),
                        Button::make('reset'),
                        Button::make('reload')
                    );
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'Name',
            'Code',
            'Service Type' => ['title' => 'Service Type']
        ];
    }

    /**
     * Get filename for export.
     *
     * @return string
     */
    protected function filename()
    {
        return 'ussd_ussdservicedatatables_' . time();
    }
}
