<?php

namespace App\DataTables;

use App\Role;
use App\User;
use Illuminate\Support\Facades\DB;
use Yajra\Datatables\Services\DataTable;

class NetworkCostPriceDataTable extends DataTable
{
    /**
     * Display ajax response.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function ajax()
    {
        return $this->datatables
            ->queryBuilder($this->query())
            ->addColumn('action', function ($row) {
                return '<div class="btn-group">' .
                '<a title="Edit Record" class="btn btn-sm btn-warning" href="' . route('admin.setting.cost.price.edit', ['id'=>$row->id]).'"><i class="fa fa-pencil"></i></a>' .
                '<a title="Delete Record" class="btn btn-sm btn-danger" href="' . route('admin.setting.cost.price.del', ['id'=>$row->id]) . '"' .
                'onclick="return confirm(\'Do you want to delete this Record?\')"><i class="fa fa-trash"></i></a>' .
                '</div>';
            })
            ->make(true);
    }

    /**
     * Get the query object to be processed by dataTables.
     *
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
     */
    public function query()
    {

        if($this->request()->get('type')) {
            $type = $this->request()->get('type');
            $roleId = strtolower($type) == 'reseller' ?
                Role::where('name', 'reseller')->firstOrFail()->id :
                Role::where('name', 'user')->firstOrFail()->id;

            $query = DB::table('users')
                ->join('credits', 'credits.user_id', '=', 'users.id')
                ->where('users.role_id', '=', $roleId)
                ->select(DB::raw('users.id, users.username as username, users.organisation as org, credits.price as price,  credits.new_bal as balance, full_name as name'))
                ->orderBy('users.full_name', 'desc');

        } else {
            $query = DB::table('users')->where('id',0);
        }

        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' => '80px'])
                    ->parameters([
                        'dom' => 'Bfrtip',
                        'buttons' => ['csv', 'excel', 'pdf', 'print', 'reload'],
                        'order' => [[1, 'desc']]
                    ]);
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'username',
            'name',
            'org',
            'price' => ['title' => 'Default Price', 'searchable'=>false, 'orderable'=>false],
            'balance' => ['searchable'=>false, 'orderable'=>false],
        ];
    }

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