<?php

namespace App\DataTables;

use App\User;
use App\UserSenderId;
use Yajra\Datatables\Services\DataTable;

class AllSenderIDDataTable extends DataTable
{

    public function ajax()
    {
        return $this->datatables
            ->eloquent($this->query())
            ->editColumn('username',function ($query){
                return '<a href="'.route('admin.root.company.sender-id',['id'=>@$query->id]).'">'.$query->username.'</a>';
            })
            ->addColumn('action', function ($query){
                return  '<div class="btn-group">'
                        .'<a class="btn btn-success btn-sm" title="APPROVE" href="'.route('admin.users.sender_id.update',['id'=>$query->id,'status'=>1]).'"><i class="fa fa-check-circle"></i></a>'
                       .'<a class="btn btn-danger btn-sm" title="REJECT"  onclick="return   swal({ title: \'Are you sure?\',  text: \'Are you sure you want to reject this request ?\', icon: \'warning\',
                            buttons: true,  dangerMode: true,}).then((willDelete) => { if (willDelete) {
                                    swal(\'Add Comment here:\', {
                                      content: \'input\',
                                    })
                                    .then((comment) => {
                                       window.location.replace(\''.$query->id.'/update?status=\'+2+\'&comment=\'+comment+\'\')                    
                                    });
                            } else {}});">
                               <i class="fa fa-times-circle"></i></a>'

                         .'</div>';
            })
            ->addColumn('count',function ($query){
                $count = UserSenderId::query()->where([['user_id',$query->id],['status',0]])->get();
                return $count->count();
            })->rawColumns(['username','action','count'])
            ->make(true);
    }

    /**
     * Get the query object to be processed by dataTables.
     *
     * @return
     */
    public function query()
    {
        $ids = UserSenderId::query()->where('status',0)->pluck('user_id')->toArray();

        $query = User::query()->whereIn('id',$ids);


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


    public function html()
    {
        return $this->builder()
                    ->columns($this->getColumns())
                    ->ajax('')
//                    ->addAction(['width' => '80px'])
                    ->parameters($this->getBuilderParameters());
    }

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            'username',
            'count',
            'created_at',
            'updated_at',
        ];
    }

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