<?php

namespace App\DataTables;

use App\User;
use App\UserSenderId;
use Yajra\DataTables\Html\Button;
use Yajra\DataTables\Services\DataTable;

class AllSenderIDDataTable extends DataTable
{

    public function dataTable($query)
    {
       return datatables()
            ->eloquent($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(['action','username']);

    }

    /**
     * Get the query object to be processed by dataTables.
     *
     * @return
     */
    public function query()
    {

        $query = User::query()->whereHas('sender_ids',function ($sender){
            $sender->where('status',0);
        });


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


    public function html()
    {
        return $this->builder()
                    ->columns($this->getColumns())
                    ->ajax('')
                    ->dom('Bfrtip')
                    ->buttons(
                        Button::make('export'),
                        Button::make('print'),
                        Button::make('reset'),
                        Button::make('reload')
                         );
    }

    /**
     * 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();
    }
}
