<?php

namespace App\DataTables;

use Carbon\Carbon;
use App\UssdCollection;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Services\DataTable;

class UssdCollectionResponseDatatable extends DataTable
{


    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->addColumn('question_id', function ($query) {
                return "";
            });


    }


    /**
     * Get the query object to be processed by dataTables.
     *
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
     */
    public function query()
    {
        $serviceID=$this->getServiceId();

        $query = UssdCollection::query()->where('serviceID', $serviceID)->orderBy('created_at','DESC');
        if ($this->request()->get('from_date') && $this->request()->get('to_date')) {

            $from_date_str = $this->request()->get('from_date');
            $to_date_str = $this->request()->get('to_date');

            $from_date = Carbon::parse(Carbon::parse($from_date_str)->toDateString());
            $to_date = Carbon::parse(Carbon::parse($to_date_str)->addDay()->toDateString());
            $to_date->subSecond();

            if ($from_date->lte($to_date)) {
                $query = $query->where([
                    ['created_at', '>=', $from_date->toDateTimeString()],
                    ['created_at', '<=', $to_date->toDateTimeString()],
                ]);
            }
        }
            return $this->applyScopes($query);
    }


    /**
     * Gets the ussd service id selected by the user
     * @return mixed
     */
    private function getServiceId()
    {
        //get the ussd service from the uri
        $pathInfo = request()->getPathInfo(); //returns ie "/service/4/response/report"
        $pathParts = explode('/', $pathInfo); // places the ussd service id ie 4 at index 2
        //        [ 0 => "",
        //            1 => "service",
        //            2 => "4",
        //            3 => "response",
        //            4 => "report"
        //        ]
        return $pathParts[2];
    }

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

    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'name' => ['title' => 'Contributor'],
            'msisdn' => ['title' => 'Phone Number'],
            'category' => ['title' => 'Type'],
            'amount',
            'status',
            'created_at'
        ];
    }

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