<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use Session;

// app includes
use App\Services\GridBuilder;
use App\Models\CompanyModel;

class CompanyController extends Controller
{

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index(Request $request)
    {

        $query = CompanyModel::getCompanyQuery($request);
        $grid = GridBuilder::builder()
            ->htmlid('#divCompany')
            //        ->enableAjaxGrid()
            ->recordsPerPage(10)
            //->parameters()
            ->data($query)
            ->addColumn([
                'name' => 'company_id',
                'title' => 'Company code',
                'filter' => 'company_id',
                'filter_name' => 'company_id',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'name',
                'title' => 'name',
                'filter' => 'name',
                'filter_name' => 'name',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'nit',
                'title' => 'NIT',
                'filter' => 'nit',
                'filter_name' => 'nit',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'email',
                'title' => 'Email',
                'filter' => 'email',
                'filter_name' => 'email',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'phone',
                'title' => 'Teléfono',
                'filter' => 'phone',
                'filter_name' => 'phone',
                'sort' => true,
            ])
            ->addColumn([
                'name' => 'agentName',
                'title' => 'Representante',
                'html_decorator' => function ($company) {
                    return "{$company->agentName}<br />{$company->agentPhone}";
                },
                'encode' => false,
                'style' => ' class="text-nowrap" '
            ])
            ->addColumn([
                'name' => 'offices',
                'title' => 'Oficinas',
                'html_decorator' => function ($company) {
                    return $company->offices()->count();
                },
                'encode' => false,
                'style' => ' class="text-nowrap" '
            ])
            ->addColumn([
                'name' => 'actions',
                'title' => 'Acciones',
                'html_decorator' => function ($company) use ($request) {

                    $btnEditUrl = route('companyForm', $company->company_id);
                    $btnDeleteUrl = route('companyDelete', $company->company_id);
                    $btnEdit = " <a href='$btnEditUrl' class='btn btn-primary cold-color' > Edit </a> &nbsp;&nbsp; ";
                    $btnDelete = " <a href='$btnDeleteUrl' class='btn btn-danger cold-color' onclick='return confirm(\"¿Confirm delete Company?\");' > Delete </a> &nbsp;&nbsp; ";
                    $btnOfficesUrl = route('offices', $company->company_id);
                    $btnOffices = " <a href='$btnOfficesUrl' class='btn btn-primary cold-color' > Oficinas </a> &nbsp;&nbsp; ";

                    return " {$btnOffices} {$btnEdit} {$btnDelete}";
                },
                'encode' => false,
                'style' => ' class="text-nowrap" '
            ])
            ->build();


        $view = $request->keyword ? 'helpers.grid' : 'companies.index';
        return view($view, compact('grid'));
    }


    public function form(Request $request)
    {
        $company = CompanyModel::getCompany($request->company_id);
        return view('companies.form', compact('company'));
    }

    public function save(Request $request)
    {
        $data = $request->all();

        $validator = CompanyModel::getValidator($data);

        if ($validator->fails()) {
            return redirect()->back()
                ->withErrors($validator)
                ->withInput(); //get the data in all input formfields
        }
        $company = CompanyModel::save($data);
        if ($company) {
            Session::flash('message', 'Compañia guardada');
        } else {
            Session::flash('errorMessage', 'No fue posible guardar la compañia');
        }
        return redirect()->route('companies');
    }

    public function delete(Request $request)
    {
        $companyDelete = CompanyModel::delete($request->company_id);
        if ($companyDelete) {
            Session::flash('message', 'Compañia eliminada');
        } else {
            Session::flash('errorMessage', 'Compañia invalida ');
        }
        return redirect()->back();
    }
}
