<?php

namespace App\DataTables;

use App\Company;
use App\Role;
use App\User;
use Illuminate\Support\Facades\Auth;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Services\DataTable;

class ResellerUserDataTable extends DataTable
{

    public function dataTable($query)
    {
       return datatables()
            ->eloquent($query)
            ->addColumn('price', function ($row){
                return $row->users->credit->price ?? null;
            })
            ->addColumn('balance', function ($row){
                return !empty($row->users->credit) ? $row->users->credit->new_bal: '';
            })
            ->addColumn('credit_used', function ($row){
                return !empty($row->users->credit) ? $row->users->credit->credit_used:'';
            })
            ->addColumn('is_locked', function ($row){
                return $row->users->is_locked ?? null ? 'Yes' : 'No';
            })
            ->addColumn('currency', function ($row){
                return $row->users->credit->currency ?? '';
            }) ->addColumn('updated_at', function ($row){
                return !empty($row->users->credit) ? $row->users->credit->updated_at->format('d M y, H:i'):'';
            })
            //
            ->addColumn('action', function ($row){
                return '<div class="btn-group">' .
                '<a title="Edit User" class="btn btn-sm btn-primary"  href="' . route('reseller.users.edit_user', ['id'=>$row->users->id]).'"><i class="fa fa-pencil"></i></a>' .
                '<a title="Delete User" class="btn btn-sm btn-danger" href="' . route('reseller.users.delete_user', ['id'=>$row->users->id]) . '"' .
                'onclick="return confirm(\'Do you want to delete this user?\')"><i class="fa fa-trash"></i></a>' .
                '<a title="Make Payment"  style="background-color:#007E33; color:white" class="btn btn-sm" href="' . route('reseller.users.payments', ['group_id'=>$row->users->id]).'"><i class="fa fa-money"></i></a>' .
                '<a title="Signup client on mail package" style="background-color:#3E4551; color:white" class="btn btn-sm" href="'.route('reseller.users.payments.email', $row->users->id).'"><i class="fa fa-envelope"></i></a>' .
                '<a title="Signup client on social media package" style="background-color:orange; color:white" class="btn btn-sm" href="'.route('reseller.users.payments.social', $row->users->id).'"><i class="fa fa-laptop"></i></a>' .
                '<a title="User Network Price" class="btn btn-sm btn-info" href="' . route('reseller.users.networks', ['user_id'=>$row->users->id]).'"><i class="fa fa-wifi"></i></a>' .
                '<a title="'.($row->users->is_locked ? 'Unlock' : 'Lock').' User" class="btn btn-sm btn-warning" href="' . route('reseller.users.lock_user', ['id'=>$row->users->id]) . '"' .
                'onclick="return confirm(\'Do you want to '.($row->users->is_locked ? 'unlock' : 'lock').' this user?\')"><i class="fa fa-lock"></i></a>' .
                '</div>';
            });

    }

    public function query()
    {

        $query = Company::query()->whereHas('users',function ($user){
            $user->where('created_by',Auth::user()->username);
        })->with('users');


        return $this->applyScopes($query);
    }

    /**
     * Optional method if you want to use html builder.
     *
     */
    public function html()
    {
        return $this->builder()
            ->columns($this->getColumns())
            ->setTableAttributes(['style'=>'font-size:13px;', 'cellspacing'=> '0'])
            ->ajax('')
            ->addAction(['width' => '260px'])
            ->dom('Bfrtip')
            ->buttons(
                Button::make('excel'),
                Button::make('csv'),
                Button::make('print')
            );
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'users.username'=>['title'=>'username'],
//            'full_name'=> ['title'=>'Contact Person'],
            'users.organisation'=>['title'=>'Organisation'],
            'price' => ['title'=>'SMS Price', '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?'],
//            'updated_at' => ['title'=>'Last Update', 'searchable'=>false, 'orderable'=>false],
        ];
    }

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