<?php

namespace App\Http\Controllers;

use App\Models\CompanyModel;
use Illuminate\Http\Request;
use Auth;
use Session;

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

class OfficeController extends Controller
{

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

        $company = CompanyModel::getCompany($request->company_id);
        if ($company) {

            $query = OfficeModel::getOfficeQuery($request);
            $grid = GridBuilder::builder()
                ->htmlid('#divOffice')
                //        ->enableAjaxGrid()
                ->recordsPerPage(10)
                //->parameters()
                ->data($query)
                ->addColumn([
                    'name' => 'office_id',
                    'title' => 'Office code',
                    'filter' => 'office_id',
                    'filter_name' => 'office_id',
                    'sort' => true,
                ])
                ->addColumn([
                    'name' => 'name',
                    'title' => 'name',
                    'filter' => 'name',
                    'filter_name' => 'name',
                    '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' => 'Nombre Representante',
                    'filter' => 'agentName',
                    'filter_name' => 'agentName',
                    'sort' => true,
                ])
                ->addColumn([
                    'name' => 'agentPhone',
                    'title' => 'Teléfono Representante',
                    'filter' => 'agentPhone',
                    'filter_name' => 'agentPhone',
                    'sort' => true,
                ])

                ->addColumn([
                    'name' => 'actions',
                    'title' => 'Acciones',
                    'html_decorator' => function ($office) use ($request) {

                        $btnEditUrl = route('officeForm', ['company_id' => $request->company_id, 'office_id' => $office->office_id]);
                        $btnDeleteUrl = route('officeDelete', $office->office_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 Office?\");' > Delete </a> &nbsp;&nbsp; ";

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

            $view = $request->keyword ? 'helpers.grid' : 'offices.index';
            return view($view, compact('grid', 'request', 'company'));
        } else {
            Session::flash('errorMessage', 'Compañia no válida');
            return redirect()->back();
        }
    }


    public function form(Request $request)
    {
        $office = OfficeModel::getOffice($request->office_id);
        return view('offices.form', compact('office', 'request'));
    }

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

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

        if ($validator->fails()) {
            return redirect()->back()
                ->withErrors($validator)
                ->withInput(); //get the data in all input formfields
        }
        $office = OfficeModel::save($data);
        if ($office) {
            Session::flash('message', 'Sede guardada');
        } else {
            Session::flash('errorMessage', 'No fue posible guardar sede');
        }
        return redirect()->route('offices', $office->company_id);
    }

    public function delete(Request $request)
    {
        $officeDelete = OfficeModel::delete($request->office_id);
        if ($officeDelete) {
            Session::flash('message', 'Sede eliminada');
        } else {
            Session::flash('errorMessage', 'Sede invalida ');
        }
        return redirect()->back();
    }
}
