<?php
namespace DummyNamespace;

use Illuminate\Http\Request;

use DummyRootNamespaceHttp\Requests;
use DummyRootNamespaceHttp\Controllers\Controller;
use App\{{ModelName}};
use Validator;
use Yajra\DataTables\Facades\DataTables;
use Storage;
use Illuminate\Support\Facades\File;
use App\Http\Controllers\ResponseController;

class DummyClass extends Controller
{
    
    public $Now;
    public $Response;
    public function __construct(){
        parent::__construct();
        $this->Now=date('Y-m-d H:i:s');
        $this->Response=new ResponseController();
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return View('{{ModelName}}_list');
    }
    
    /**
     * 
     * @return type 
     */
    public function All()
    {
        ${{ModelName}}={{ModelName}}::query();
        {{with}}
        return Datatables::of(${{ModelName}})->addColumn('Select', function(${{ModelName}}) { return '<input class="flat {{ModelName}}_record" name="{{ModelName}}_record"  type="checkbox" value="'.${{ModelName}}->id.'" />';})
                ->addColumn('actions', function (${{ModelName}}) {
                $column='<a href="'.route('{{ModelName}}view',${{ModelName}}->id).'"  class="'.config('view.view_classes')['button'].'"><i class="'.config('view.view_classes')['icon'].'"></i> View</a>';
                $column.='<a href="'.route('{{ModelName}}edit',${{ModelName}}->id).'"  class="'.config('view.edit_classes')['button'].'"><i class="'.config('view.edit_classes')['icon'].'"></i> Edit</a>';
                $column.='<a href="javascript:void(0)" data-url="'.route('{{ModelName}}delete',${{ModelName}}->id).'" class="delete '.config('view.delete_classes')['button'].'"><i class="'.config('view.delete_classes')['icon'].'"></i> Delete</a>';
                return $column;})->{{EditColumns}}rawColumns({{RawColumnsReplace}})->make(true);
    }

    private function validateCreateOrUpdate(Request $request){
        return Validator::make($request->all(), {{RulesArray}});
    }
    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function CreateOrUpdate(Request $request)
    {
        try {
            $validationResult = $this->validateCreateOrUpdate($request);
            if($validationResult->fails()){
                return $this->Response->prepareResult(400,null,$validationResult->errors(),null,'ajax',null,'{{ModelName}} Could not be  Saved');
            }
            if($request['id'] !=''):
                ${{ModelName}} = {{ModelName}}::where('id',$request['id'])->first();    
                {{form_fields}}
                ${{ModelName}}->save();
                return $this->Response->prepareResult(200,${{ModelName}},[],'{{ModelName}} Saved successfully ','ajax');
            else:
                ${{ModelName}}=new {{ModelName}}();    
                {{form_fields}}
                ${{ModelName}}->save();
                return $this->Response->prepareResult(200,${{ModelName}},[],'{{ModelName}} Created successfully ','ajax');
            endif;
        } catch (Exception $exc) {
                return $this->Response->prepareResult(400,null,[],null,'ajax','{{ModelName}} Could not be  Saved');
        }

        
        
    }

    /**
     * Show the viw the specified resource.
     *
     * @param  int  $ID
     * @return \Illuminate\Http\Response
     */
    public function view($ID)
    {
        try {
                $data={{ModelName}}::where('id',$ID)->firstOrFail();        
                return $this->Response->prepareResult(200,$data,[],null,'view','{{ModelName}}_view');
            } catch (\Exception $exc) {
                 return $this->Response->prepareResult(400,[],null,'view','Could not get This item');
        }
    }
    
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $ID
     * @return \Illuminate\Http\Response
     */
    public function edit($ID)
    {
        try {
                $data={{ModelName}}::where('id',$ID)->firstOrFail();                
                return $this->Response->prepareResult(200,$data,[],null,'view','{{ModelName}}_edit');
            } catch (\Exception $exc) {
                 return $this->Response->prepareResult(400,[],null,'view','Could not get This item');
        }
    }
    
    /**
     * Show the form for add the specified resource.
     *
     * @param  int  $ID
     * @return \Illuminate\Http\Response
     */
    public function add()
    {
        try {
                $data=new \stdClass();                
                return $this->Response->prepareResult(200,$data,[],null,'view','{{ModelName}}_add');
            } catch (\Exception $exc) {
                 return $this->Response->prepareResult(400,[],null,'view','Could not get This item');
        }
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $ID
     * @return \Illuminate\Http\Response
     */
    public function Delete($ID)
    {
        try {
                {{ModelName}}::where('id',$ID)->delete();
                return  $this->Response->prepareResult(200,[],'{{ModelName}} Item deleted Successfully','ajax');
            } catch (\Exception $exc) {
        }        return $this->Response->prepareResult(400,[],null,'ajax','{{ModelName}} Item Could be not deleted');
    }
    
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $ID
     * @return \Illuminate\Http\Response
     */
    public function DeleteMultiple(Request $request)
    {
        try {
                {{ModelName}}::whereIn('id',$request->selected_rows)->delete();
                return  $this->Response->prepareResult(200,[],'{{ModelName}} Item/s deleted Successfully','ajax');
            } catch (\Exception $exc) {
        }        return $this->Response->prepareResult(400,[],null,'ajax','{{ModelName}} Item/s Could be not deleted');
    }
    
    /**
     * Upload Attachment Or Image
     */
    protected function Upload(Request $request,$FieldName)
    {
        $path='';
        $Image = $request->file($FieldName);
        if($Image):
            $Extension = $Image->getClientOriginalExtension();
            $path = $Image->getFilename() . '.' . $Extension;
            Storage::disk('files_folder')->put($path, File::get($request->file($FieldName)));
        endif;
        return $path;
    }
}
