/home/mip/mip/app/Modules/Client/Controllers/Subject/SubjectController.php
<?php

namespace QxCMS\Modules\Client\Controllers\Subject;

use DB;
use Auth;
use Datatables;

use Illuminate\Http\Request;
use QxCMS\Http\Controllers\Controller;

use QxCMS\Modules\Client\Requests\Subject\SubjectRequest;
use QxCMS\Modules\Client\Repositories\Subject\SubjectRepositoryInterface as Subject;
use QxCMS\Modules\Client\Repositories\Settings\Roles\PermissionRepositoryInterface as Permission;

class SubjectController extends Controller
{
	protected $subject;
	protected $permission;
	protected $auth;
	protected $prefix_name = '';

	public function __construct(Subject $subject, Permission $permission)
	{
		$this->subject = $subject;
		$this->permission = $permission;
		$this->auth = Auth::guard('client');
		$this->prefix_name = config('modules.client');
        $this->getDetails($this->subject->getModuleId());
	}

	public function permissions()
    {

        return $this->permission->getPermission($this->subject->getModuleId());
    }

    public function index() 
    {
    	$this->authorize('activated', $this->permissions());
    	$data['permissions'] = $this->permissions();
        $data['userLogs'] = $this->subject->getUserLogs($this->subject->getModuleId());
        return view('Client::subject.index', $data);    	
    }
    public function create()
    {
        $this->authorize('create', $this->permissions());        
        $data['permissions'] = $this->permissions();
        return view('Client::subject.create', $data);
    }

    public function store(SubjectRequest $request)
    {
        $this->authorize('create', $this->permissions());
        $input = $request->all();
        $subject = $this->subject->create($input); 
        session()->flash('success', 'Successfully added.');

        return redirect($this->prefix_name.'/subject');
    }

    public function show($id)
    {
        $this->authorize('activated', $this->permissions());
        return $id;
    }

    public function edit($hashid)
    {
        $this->authorize('update', $this->permissions());
        $id = decode($hashid);

        $data['subject'] = $this->subject->with(['principal', 'template', 'fieldOfficer', 'editor'])->find($id);
        $data['permissions'] = $this->permissions();
        $data['userLogs'] = $this->subject->getUserLogs($this->subject->getModuleId(), $id);

        return view('Client::subject.edit', $data);
    }

    public function update(SubjectRequest $request, $hashid)
    {
        $this->authorize('update', $this->permissions());
        $id = decode($hashid);
        $subject = $this->subject->update($id, $request->all());
        return redirect($this->prefix_name.'/subject');
    }

    public function destroy($hashid)
    {
        $this->authorize('delete', $this->permissions());
        $id = decode($hashid);
        return $this->subject->delete($id); 
    }

  
    public function getDatatablesIndex(Request $request)
    {        

        $permissions = $this->permissions();
        $subjects = $this->subject->datatablesIndex();

        return Datatables::of($subjects)
            ->editColumn('status', function($model){
                if($model->status == 'Close') return '<span class="label label-danger">Close</span>';
                return '<span class="label label-success">Open</span>';
            })
            ->editColumn('created_at', function($model){
                return $model->created_at->format('M d, Y');
            })
            ->addColumn('details', function($model){
                $html_out = "";
                $html_out .= "<font size=\"4\"><b>Subject:&nbsp;</b>".$model->name."</font>";

                if(isset($model->principal->name)) {
                    $html_out .= "<br/><b>Client:&nbsp;</b><a href=\"".route(config('modules.client').'.settings.principals.overview', hashid($model->principal_id))."\">".$model->principal->name.'</a>';
                }

                if(isset($model->template->title)) {
                    $html_out .= "<br/><b>Questionnaire:&nbsp;</b><a href=\"".route(config('modules.client').'.questionnaire.question.index', hashid($model->template_id))."\">".$model->template->title."</a>";
                }

                if(!empty($model->interview_date))
                {
                    $html_out .= "<br/><b>Interview Date:&nbsp;</b>".$model->interview_date;
                }

                if(!empty($model->completion_date))
                {
                    $html_out .= "<br/><b>Interview Date Completion:&nbsp;</b>".$model->completion_date;
                }

                return $html_out;
            })
            ->addColumn('assigned_to', function($model) {
                $html_out = "";
                if(isset($model->fieldOfficer->name))
                {
                    $html_out .= "<b>Field Officer:</b>&nbsp;<font size=\"3\">".$model->fieldOfficer->name."</font>";
                }
                if(isset($model->editor->name))
                {
                    $html_out .= "<br /><b>Editor:</b>&nbsp;<font size=\"3\">".$model->editor->name."</font>";
                }
                return $html_out;
            })


            ->addColumn('action', function ($model) use ($permissions) {
                $html_out = '';
                
                if($this->auth->user()->can('update', $permissions)) {
                    $html_out .= '<a href="'.url($this->prefix_name.'/subject/'.$model->hashid.'/edit').'" class="btn btn-xs btn-flat btn-warning" data-toggle="tooltip" data-placement="top" title="Edit"><i class="fa fa-pencil"></i></a>';
                }
                if($this->auth->user()->can('delete', $permissions)) {
                    $html_out .= '&nbsp;&nbsp;<a href="#delete-'.$model->hashid.'" class="btn btn-xs btn-flat btn-danger" id="btn-delete" data-action="'.url($this->prefix_name).'/subject/'.$model->hashid.'" data-toggle="tooltip" data-placement="top" title="Delete"><i class="fa fa-trash-o"></i></a>';
                }
                
                return $html_out;
            })
            ->filterColumn('details', function($query, $keyword) {
                $query->whereRaw("name like ?", ["%{$keyword}%"]);
            })
            ->orderColumn('details', 'interview_date $1, completion_date $1')
            ->make(true);
    }

}