<?php

namespace LivraisonParSecteurs\Controller\backOffice;

use LivraisonParSecteurs\LivraisonParSecteurs;
use LivraisonParSecteurs\Model\LpsAreaCity;
use LivraisonParSecteurs\Model\LpsAreaCityQuery;
use LivraisonParSecteurs\Model\LpsAreaSchedule;
use Propel\Runtime\Map\TableMap;
use Propel\Runtime\Propel;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Thelia\Controller\Admin\BaseAdminController;
use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Form\Exception\FormValidationException;
use Thelia\Tools\URL;

/**
 * Class CitiesController
 * @package LivraisonParSecteurs\Controller
 */
class CitiesController extends BaseAdminController
{

    /*
     *    Remove a city from an area delivery tour
     */
    public function removeCity()
    {
        // Check current user authorization
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, LivraisonParSecteurs::getModuleCode(), AccessManager::UPDATE))
            return $response;

        $areaCity_id = $this->getRequest()->get('area-cities_id');
        $query = LpsAreaCityQuery::create()->findById($areaCity_id);
        if ($query === null)
            $error_msg = "City not found by Id in this area";
        else
        {
            $con = Propel::getConnection();
            $con->beginTransaction();
            $query->delete($con);
            $con->commit();
        }

        if ($this->getRequest()->request->get("success_url") == null) {
            return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/LivraisonParSecteurs"));
        } else {
            return new RedirectResponse(URL::getInstance()->absoluteUrl($this->getRequest()->request->get("success_url")));
        }
    }


    /*
     * Add a new delivered city in an area
     */
    public function addCity()
    {
        // Check current user authorization
        if (null !== $response = $this->checkAuth(AdminResources::MODULE, LivraisonParSecteurs::getModuleCode(), AccessManager::UPDATE))
            return $response;

        $con = Propel::getConnection();
        $con->beginTransaction();

        $error_msg = "";
        $addForm = $this->createForm("lps-area-cities-add", "form");
        try {
            $form = $this->validateForm($addForm, "POST");
            $data = $form->getData();

            $query = new LpsAreaCity();
            $query->fromArray($data, TableMap::TYPE_FIELDNAME);
            $query->setIdArea($data['area_id']);
            $query->save($con);
            $con->commit();

        } catch (FormValidationException $ex) {
            $error_msg = $this->createStandardFormValidationErrorMessage($ex);

            if (false !== $error_msg) {
                $this->setupFormErrorContext(
                    $this->getTranslator()->trans("City add"),
                    $error_msg,
                    $addForm,
                    $ex
                );

                return $this->generateErrorRedirect($addForm);
            }
        }

        if ($data['success_url'] == null) {
            return new RedirectResponse(URL::getInstance()->absoluteUrl("/admin/module/LivraisonParSecteurs"));
        } else {
            return $this->generateSuccessRedirect($addForm);
        }

    }

}