<?php

namespace Payroll\Company\Http\Controllers;

use App\Http\Controllers\AdminController as BaseController;
use Form;
use Payroll\Company\Http\Requests\CompanyRequest;
use Payroll\Company\Interfaces\CompanyRepositoryInterface;
use Payroll\Company\Models\Company;

/**
 * Admin web controller class.
 */
class CompanyAdminController extends BaseController
{
    // use CompanyWorkflow;
    /**
     * Initialize company controller.
     *
     * @param type CompanyRepositoryInterface $company
     *
     * @return type
     */
    public function __construct(CompanyRepositoryInterface $company)
    {
        $this->repository = $company;
        parent::__construct();
    }

    /**
     * Display a list of company.
     *
     * @return Response
     */
    public function index(CompanyRequest $request)
    {
        if ($request->wantsJson()) {
            return $this->getJson($request);
        }
        $this   ->theme->prependTitle(trans('company::company.names').' :: ');
        return $this->theme->of('company::admin.company.index')->render();
    }

    /**
     * Display a list of company.
     *
     * @return Response
     */
    public function getJson(CompanyRequest $request)
    {
        $pageLimit = $request->input('pageLimit');

        $companies  = $this->repository
                ->pushCriteria(app('Litepie\Repository\Criteria\RequestCriteria'))
                ->setPresenter('\\Payroll\\Company\\Repositories\\Presenter\\CompanyListPresenter')
                ->scopeQuery(function($query){
                    return $query->orderBy('id','DESC');
                })->paginate($pageLimit);
        $companies['recordsTotal']    = $companies['meta']['pagination']['total'];
        $companies['recordsFiltered'] = $companies['meta']['pagination']['total'];
        $companies['request']         = $request->all();
        return response()->json($companies, 200);

    }

    /**
     * Display company.
     *
     * @param Request $request
     * @param Model   $company
     *
     * @return Response
     */
    public function show(CompanyRequest $request, Company $company)
    {
        if (!$company->exists) {
            return response()->view('company::admin.company.new', compact('company'));
        }

        Form::populate($company);
        return response()->view('company::admin.company.show', compact('company'));
    }

    /**
     * Show the form for creating a new company.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function create(CompanyRequest $request)
    {

        $company = $this->repository->newInstance([]);

        Form::populate($company);

        return response()->view('company::admin.company.create', compact('company'));

    }

    /**
     * Create new company.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(CompanyRequest $request)
    {
        try {
            $attributes             = $request->all();
            $attributes['user_id']  = user_id('admin.web');
            $company          = $this->repository->create($attributes);

            return response()->json([
                'message'  => trans('messages.success.updated', ['Module' => trans('company::company.name')]),
                'code'     => 204,
                'redirect' => trans_url('/admin/company/company/'.$company->getRouteKey())
            ], 201);


        } catch (Exception $e) {
            return response()->json([
                'message'  => $e->getMessage(),
                'code'     => 400,
            ], 400);
        }
    }

    /**
     * Show company for editing.
     *
     * @param Request $request
     * @param Model   $company
     *
     * @return Response
     */
    public function edit(CompanyRequest $request, Company $company)
    {
        Form::populate($company);
        return  response()->view('company::admin.company.edit', compact('company'));
    }

    /**
     * Update the company.
     *
     * @param Request $request
     * @param Model   $company
     *
     * @return Response
     */
    public function update(CompanyRequest $request, Company $company)
    {
        try {

            $attributes = $request->all();

            $company->update($attributes);

            return response()->json([
                'message'  => trans('messages.success.updated', ['Module' => trans('company::company.name')]),
                'code'     => 204,
                'redirect' => trans_url('/admin/company/company/'.$company->getRouteKey())
            ], 201);

        } catch (Exception $e) {

            return response()->json([
                'message'  => $e->getMessage(),
                'code'     => 400,
                'redirect' => trans_url('/admin/company/company/'.$company->getRouteKey()),
            ], 400);

        }
    }

    /**
     * Remove the company.
     *
     * @param Model   $company
     *
     * @return Response
     */
    public function destroy(CompanyRequest $request, Company $company)
    {

        try {

            $t = $company->delete();

            return response()->json([
                'message'  => trans('messages.success.deleted', ['Module' => trans('company::company.name')]),
                'code'     => 202,
                'redirect' => trans_url('/admin/company/company/0'),
            ], 202);

        } catch (Exception $e) {

            return response()->json([
                'message'  => $e->getMessage(),
                'code'     => 400,
                'redirect' => trans_url('/admin/company/company/'.$company->getRouteKey()),
            ], 400);
        }
    }

}