<?php

namespace Packages\{package}\{module}\Controllers;

use Packages\Core\Backend\AbstractBackendController;
use View;
use Input;
use Redirect;
use Response;

class {controller} extends AbstractBackendController {
    
    public function __construct()
    {

    }
    
    public function index()
    {
        return View::make('{module}::index');
    }
    
    public function json()
    {
        $columns = [

        ];
        
        $data = $this->xRepository->getDynamicTableData(function($r) { 
            return $r->getPaginatedX();
        }, $columns, function($row) {
            return $this->makeDTOptions([
                [ 
                    'url' => route('backend_', ['id' => $row->getKey()]),
                    'name' => trans('GlobalTranslations::common.edit'),
                    'icon' => 'icon-edit',
                ],
                [ 
                    'url' => route('backend_', ['id' => $row->getKey(), '_token' => csrf_token()]),
                    'name' => trans('GlobalTranslations::common.delete'),
                    'icon' => 'icon-remove',
                    'attributes' => [
                        'data-dialog' => 'delete-confirmation'
                    ],
                ],
            ]); 
        });

        return Response::jsonp($data);
    }
    
    public function manage($id = null)
    {
        $data = $this->xRepository->findOrRedirect();

        return View::make('{module}::manage', compact('data'));
    }
    
    public function processManage($id = null)
    {
        $input = Input::all();

        $this->xFormValidator->validate($input);

        try {
            $this->xRepository->updateOrNewX();
        } catch(\Exception $e) {
            return $this->redirectError('backend_', $e);
        }
        
        return $this->redirectSuccess('backend_');
    }
    
    public function delete($id)
    {
        try {
            $this->xRepository->deleteX($id);
        } catch(\Exception $e) {
            return $this->redirectError(null, $e);
        }

        return $this->redirectSuccess();
    }
   
}
