<?php

namespace App\DataTables;

use App\User;
use Yajra\DataTables\Services\DataTable;

class SingleUserDataTable extends DataTable
{

    public function dataTable($query)
    {
       return datatables()
            ->eloquent($query)
            ->addColumn('price', function ($row){
                return $row->credit ? $row->credit->price : '';
            })
            ->addColumn('balance', function ($row){
                return $row->credit ? $row->credit->new_bal : '';
            })
//            ->addColumn('credit_alloted', function ($row){
//                return $row->credit ? $row->credit->credit_alloted : '';
//            })
            ->addColumn('credit_used', function ($row){
                return $row->credit ? $row->credit->credit_used : '';
            })
            ->addColumn('is_locked', function ($row){
                return $row->is_locked ? 'Yes' : 'No';
            })
            ->addColumn('updated_at', function ($row){
                if ($row->credit && $row->credit->updated_at != null)
                    return $row->credit->updated_at->format('d M y, H:i');
                return null;
            })
            ->addColumn('role', function ($row){
                $role = ucfirst($row->role->name);
                if($role == 'User') {
                    $role = 'Client';
                }
                return $role;
            })
            //
            ->addColumn('action', function ($row){
                return '<div class="btn-group">' .
                    '<a title="Edit User" class="btn btn-sm btn-primary" href="' . route('admin.users.edit_user', ['id'=>$row->id]).'"><i class="fa fa-pencil"></i></a>' .

                    '<a title="Make Payment" class="btn btn-sm btn-info" href="' . route('admin.users.payments', ['id'=>$row->id]).'"><i class="fa fa-money"></i></a>' .
                    '<a title="User Network Price" class="btn btn-sm btn-warning" href="' . route('admin.users.networks', ['user_id'=>$row->id]).'"><i class="fa fa-wifi"></i></a>' .
//                '<a title="'.($row->is_locked ? 'Unlock' : 'Lock').' User" class="btn btn-sm btn-warning" href="' . route('admin.users.lock_user', ['id'=>$row->id]) . '"' .
//                'onclick="return confirm(\'Do you want to '.($row->is_locked ? 'unlock' : 'lock').' this user?\')"><i class="fa fa-lock"></i></a>' .
                    '<a title="'.($row->is_locked ? 'unlock user' : 'lock user').'" class="btn btn-sm '.($row->is_locked ? 'btn-danger' : 'btn-success').'" onclick="return confirm(\'Do you want to '.($row->is_locked ? 'unlock' : 'lock').' this user?\')"
                    href="' . route('admin.users.lock_user', ['id'=>$row->id]) . '"><i class="fa fa-lock"></i></a>' .
                    '<a title="Delete User" class="btn btn-sm btn-danger" href="' . route('admin.users.delete_user', ['id'=>$row->id]) . '"' .
                    'onclick="return confirm(\'Do you want to delete this user?\')"><i class="fa fa-trash"></i></a>' .
                    '</div>';
            });

    }

    /**
     * Get the query object to be processed by dataTables.
     *
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Support\Collection
     */
    public function query()
    {
        $id = session()->get('c_user');
        $query = User::query()->where('id',$id);

        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' => '120px'])
                    ->parameters($this->getBuilderParameters());
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'username',
            'full_name'=> ['title'=>'Contact Person'],
            'organisation',
            'role'=> ['title'=>'Role', 'searchable'=>false, 'orderable'=>false],
            'price' => ['title'=>'SMS Price', 'searchable'=>false, 'orderable'=>false],
//            'credit_alloted'=> ['title'=>'Credits Alloted', 'searchable'=>false, 'orderable'=>false],
            'credit_used'=> ['title'=>'Credits Used', 'searchable'=>false, 'orderable'=>false],
            'balance'=> ['title'=>'Balance', 'searchable'=>false, 'orderable'=>false],
            'currency'=> ['title'=>'Currency', 'searchable'=>false, 'orderable'=>false],
            'is_locked'=> ['title'=>'Locked?'],
        ];
    }

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