<?php

namespace App\DataTables;

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

class ResellerUserDataTable extends DataTable
{
    /**
     * Display ajax response.
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function ajax()
    {
        return $this->datatables
            ->eloquent($this->query())
            ->addColumn('price', function ($row){
                return $row->users->credit->price ?? "";
            })
            ->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 ?? "" ? '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){
                if($row->users)
                return '<div class="btn-group">' .
                '<a style="background-color: #ffcb05; color: #000;" 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 style="background-color: #ef173e; color: #ffffff;" 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 style="background-color: #4d4d4d; color: #ffffff;" title="Make Payment" class="btn btn-sm btn-info" href="' . route('reseller.users.payments', ['group_id'=>$row->users->id]).'"><i class="fa fa-money"></i></a>' .
                '<a style="background-color: #000; color: #ffcb05;" 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 style="background-color: #ffcb05; color: #000;" 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>';
            })
            ->make(true);
    }

    public function query()
    {



        $query = Company::query()
            ->join('users','users.id','companies.user_id')
            ->join('company_statuses','companies.id','company_statuses.company_id')
            ->where([
                ['company_statuses.status', true],
                ['users.created_by', Auth::user()->username],
                ['users.role_id', Role::query()->where('name', 'user')->first()->id]
              ])
            ->select('companies.*','users.username','users.full_name','organisation');



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

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            
            'username'=>['name'=>'users.username'],
            'full_name'=> ['title'=>'Contact Person','name'=>'users.full_name'],
            'organisation'=>['name'=>'users.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();
    }
}