/home/mip/mip/app/Modules/Client/Controllers/Products/ProductController.php
<?php

namespace QxCMS\Modules\Client\Controllers\Products;

use DB;
use Auth;
use Datatables;
use Session;
use File;
use Storage;

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

use QxCMS\Modules\Client\Requests\Products\ProductRequest;
use QxCMS\Modules\Client\Repositories\Settings\Roles\PermissionRepositoryInterface as Permission;
use QxCMS\Modules\Client\Repositories\Products\ProductRepositoryInterface as Product;
use QxCMS\Modules\Client\Repositories\Products\ProductBrandRepositoryInterface as ProductBrand;
use QxCMS\Modules\Client\Repositories\Products\ProductCategoryRepositoryInterface as ProductCategory;

class ProductController extends Controller
{
	protected $product;
	protected $permission;
	protected $auth;
	protected $prefix_name = '';

	public function __construct(Permission $permission, Product $product, ProductBrand $productBrand, ProductCategory $productCategory)
	{
		$this->product = $product;
        $this->productBrand = $productBrand;
        $this->productCategory = $productCategory;

		$this->permission = $permission;
		$this->auth = Auth::guard('client');
		$this->prefix_name = config('modules.client');
        $this->getDetails($this->product->getModuleId());
	}

	public function permissions()
    {

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

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

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

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

    public function show($id)
    {
        //
    }

    public function edit($hashid)
    {
        $this->authorize('update', $this->permissions());
        $id = decode($hashid);
        $data['permissions'] = $this->permissions();
        $data['product'] = $this->product->findById($id);
        $data['userLogs'] = $this->product->getUserLogs($this->product->getModuleId(), $id);

        return view('Client::product.edit', $data, $this->getFormOptions());
    }

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

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

  
    public function getProductData(Request $request)
    {
        $product = $this->product->select('*')->with('brand', 'category');

        $user = auth()->user();
        $permissions = $this->permissions();
        return Datatables::of($product)
            ->filter(function ($query) use ($request) {
                if ($request->has('name')) {
                    $query->where('name', 'like', "%{$request->get('name')}%");
                }
                if ($request->has('category_id')) {
                    $query->where('category_id', 'like', "%{$request->get('category_id')}%");
                }
                if ($request->has('brand_id')) {
                    $query->where('brand_id', 'like', "%{$request->get('brand_id')}%");
                }
            })
            ->editColumn('information', function($row) {
                if($row->category == null) $html = "<p><b>Category: </b>".'N/A'.'';
                else $html = "<p><b>Category: </b>".$row->category->name.'';

                if($row->brand == null) $html.= "<br><b>Brand: </b>".'N/A'.'';
                else $html.= "<br><b>Brand: </b>".$row->brand->name.'</p>';
                return $html;
            })
            ->addColumn('image', function ($product) use ($user){
                if($product->image <> ''){
                    return '<img src="'.Storage::disk($user->client->storage_type)->url($user->client->root_dir.'/products/'.$product->image).'" alt="Not available" width="75px" height="70px" style="border-radius: 50%;"/>';
                }else{
                    return 'Not Available';
                }
            })
            ->addColumn('others', function ($product) {
                $html = "<p><b>Price: </b>".$product->price."";
                $html.= "<br/><b>Year: </b>".$product->year."";
                $html.= "<br/><b>Status: </b>".$product->status."</p>";
                return $html;
            })
            ->addColumn('action', function ($product) use ($permissions){
                $html = '';
                if($this->auth->user()->can('update', $permissions)) {
                    $html .= '<a href="'.url('client').'/products/edit/'.$product->hashid.'"><button type="button" data-toggle="tooltip" title="Update" class="btn btn-warning btn-xs btn-flat edited"><span class="glyphicon glyphicon-pencil"></span></button></a>';
                }
                if($this->auth->user()->can('delete', $permissions)) {
                    $html .= '&nbsp;<button type="button" data-toggle="tooltip" title="Delete" class="btn btn-danger btn-xs btn-flat deleted" data-action="'.url('client').'/products/destroy/'.$product->hashid.'"><span class="glyphicon glyphicon-trash"></span></button>';
                }
                return $html;
            })
            ->make(true);
    }

    protected function getFormOptions()
    {
        $data['brand'] = $this->productBrand->productBrandList();
        $data['category'] = $this->productCategory->productCategoryList();
        $data['products'] = $this->product->all();
        $data['status'] = Array('Publish' => 'Publish', 'Draft' => 'Draft', 'Inactive' => 'Inactive');
        return $data;
    }

}