<?php namespace Visiosoft\ProfileModule\Http\Controller\Admin;

use Anomaly\Streams\Platform\Entry\Contract\EntryInterface;
use Anomaly\Streams\Platform\Model\Profile\ProfileAdressEntryModel;
use Illuminate\Http\Request;
use Visiosoft\LocationModule\City\Contract\CityRepositoryInterface;
use Visiosoft\ProfileModule\Adress\Form\AdressFormBuilder;
use Visiosoft\ProfileModule\Adress\Table\AdressTableBuilder;
use Anomaly\Streams\Platform\Http\Controller\AdminController;

class AdressController extends AdminController
{

    /**
     * Display an index of existing entries.
     *
     * @param AdressTableBuilder $table
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function index(AdressTableBuilder $table)
    {
        $table->setColumns(array_merge($table->getColumns(), [
            'city' => [
                'value' => function (EntryInterface $entry, CityRepositoryInterface $cityRepository) {
                    return $cityRepository->find($entry->city)->name;
                },
            ],
        ]));

        return $table->render();
    }

    /**
     * Create a new entry.
     *
     * @param AdressFormBuilder $form
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function create(AdressFormBuilder $form)
    {
        $form->setOption('heading', "visiosoft.module.profile::field");

        return $form->render();
    }

    /**
     * Edit an existing entry.
     *
     * @param AdressFormBuilder $form
     * @param        $id
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function edit(AdressFormBuilder $form, $id)
    {
        $form->setOption('heading', "visiosoft.module.profile::field");

        return $form->render($id);
    }

    public function adressUpdate(AdressFormBuilder $form,Request $request,$id)
    {
        $error = $form->build()->validate()->getFormErrors()->getMessages();
        if(!empty($error))
        {
            return $this->redirect->back();
        }
        $New_value = $request->all();
        unset($New_value['_token'],$New_value['action']);
        ProfileAdressEntryModel::find($id)->update($New_value);
        $message = [];
        $message[] = trans('visiosoft.module.profile::message.adress_success_update');
        if($request->get('action') == "save_create")
        {
            return redirect('admin/profile/adress/create')->with('success', $message);
        }
        return $this->redirect->back()->with('success', $message);
    }
}
