<?php



use Website\Controller\Action;

use \Website\Model\Auth;



class AccountController extends Action {



    public function init()

    {

        parent::init();

        if (!$this->editmode && !\Website\Model\Auth::isLogged()) $this->redirect('/');

    }





    public function myAgencyAction()

    {



        $this->enableLayout()->setLayout('clean');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/clean.css');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/picedit.min.css');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/account.css');

        $this->view->headScript()->appendFile('https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js');

        $this->view->headScript()->appendFile('/website/views/layouts/js/picedit.min.js');

        $this->view->headScript()->appendFile('/pimcore/static/js/lib/ckeditor/ckeditor.js');





        $formName = new \Website\Tool\AgencyNameForm();

        $formLogo = new \Website\Tool\AgencyLogoForm();

        $formAddress = new \Website\Tool\AgencyAddressForm();

        $formWebsites = new \Website\Tool\AgencyWebsitesForm();

        $formDescription = new \Website\Tool\AgencyDescriptionForm();



        if (Auth::CurrentAgencyUid()){

            $agency = \Website\Model\Agency::getByUid(Auth::CurrentAgencyUid());



            $formName->getElement('agency_name')->setValue($agency->getName());

            $formAddress->getElement('email_contact')->setValue($agency->getEmail());

            $formAddress->getElement('address')->setValue($agency->getAddress());

            $formAddress->getElement('zip')->setValue($agency->getCp());

            $formAddress->getElement('phone')->setValue($agency->getPhone());

            $formWebsites->getElement('link_website')->setValue($agency->getWebsite());

            $formWebsites->getElement('link_facebook')->setValue($agency->getFacebook());

            $formWebsites->getElement('link_twitter')->setValue($agency->getTwitter());

            $formWebsites->getElement('link_linkedin')->setValue($agency->getLinkedin());

            $formDescription->getElement('short_description_fr')->setValue($agency->getShort_description_fr());

            $formDescription->getElement('long_description_fr')->setValue($agency->getLong_description_fr());

            $formDescription->getElement('short_description_Nl')->setValue($agency->getShort_description_nl());

            $formDescription->getElement('long_description_Nl')->setValue($agency->getLong_description_nl());

            $formDescription->getElement('short_description_En')->setValue($agency->getShort_description_en());

            $formDescription->getElement('long_description_En')->setValue($agency->getLong_description_en());

        }







        if($this->_request->isPost()){



            if(!Auth::CurrentAgencyUid()){

                $agency = new Website\Model\Agency;



                $uid = \Website\Tool\RealAdvice::generateUid();

                $agency->setUid($uid);



                if(\Website\Tool\RealAdvice::isFromBelgium()) $agency->setCountry("Belgique");

                if(\Website\Tool\RealAdvice::isFromFrance()) $agency->setCountry("France");



                $user = \Zend_Auth::getInstance()->getStorage()->read();

                if ($user instanceof Auth){

                    $user->setAgency_uid($uid);

                    $user->save();

                    \Zend_Auth::getInstance()->getStorage()->write($user);

                }

            }



            if(count($_FILES)){

                try{

                    $uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/upload/' . $agency->getUid() . '/' ;



                    if (!file_exists($uploaddir))

                        mkdir($uploaddir, 0777, true);



                    foreach($_FILES as $file)

                        if(move_uploaded_file($file['tmp_name'], $uploaddir . 'logo.png'))

                            $files[] = $uploaddir .$file['name'];



                }catch(\Exception $e){

                    throw $e;

                }

            }



            if($this->getParam('submitname')){

                if($formName->isValid($_POST)){

                    $agency->setName($formName->getValue('agency_name'));

                    $agency->save();

                }

            }



            if($this->getParam('submitaddress')){

                if($formAddress->isValid($_POST)){

                    $formAddress->updated = true;

                    $agency->setName($formAddress->getValue('email_contact'));

                    $agency->setName($formAddress->getValue('address'));

                    $agency->setName($formAddress->getValue('zip'));

                    $agency->setName($formAddress->getValue('city'));

                    $agency->setName($formAddress->getValue('phone'));

                    $agency->save();

                }

            }



            if($this->getParam('submitwebsites')){

                if($formWebsites->isValid($_POST)){

                    $agency->setWebsite($formWebsites->getValue('link_website'));

                    $agency->setFacebook($formWebsites->getValue('link_facebook'));

                    $agency->setTwitter($formWebsites->getValue('link_twitter'));

                    $agency->setLinkedin($formWebsites->getValue('link_linkedin'));

                    $agency->save();

                }

            }



            if($this->getParam('submitdescription')){

                if($formDescription->isValid($_POST)){

                    $agency->setShort_description_fr($formDescription->getValue('short_description_fr'));

                    $agency->setLong_description_fr($formDescription->getValue('long_description_fr'));

                    $agency->setShort_description_nl($formDescription->getValue('short_description_Nl'));

                    $agency->setLong_description_nl($formDescription->getValue('long_description_Nl'));

                    $agency->setShort_description_en($formDescription->getValue('short_description_En'));

                    $agency->setLong_description_en($formDescription->getValue('long_description_En'));

                    $agency->save();

                }

            }



        }

        $this->view->formName = $formName;

        $this->view->formLogo = $formLogo;

        $this->view->formAddress = $formAddress;

        $this->view->formWebsites = $formWebsites;

        $this->view->formDescription = $formDescription;

        $this->view->agency = $agency;

    }



    public function sendSurveyAction()

    {



        $this->enableLayout()->setLayout('clean');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/clean.css');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/account.css');

    }



    public function seerusDashboardAction(){

        if (Auth::CurrentAgencyUid()){

            $agency = \Website\Model\Agency::getByUid(Auth::CurrentAgencyUid());

        }



        $this->view->agency = $agency;

    }



    public function linkAgencyAction(){

        $this->enableLayout()->setLayout('clean2');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/clean.css');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/account.css');



        $linkForm = new \Website\Tool\ConnectAgencyForm();

        $this->view->conectForm = $linkForm;





        $linkForm->_email->setValue(Auth::CurrentEmail());



        if($this->_request->isPost()) {



            if ($linkForm->isValid($_POST)) {

                if(!is_null($_POST["submit_search"]) && !$linkForm->getValue('search') == ''){

                    if ($linkForm->getValue('search'))



                    $list = array();

                    $r = \Website\Model\Agency::search(array("name" => $linkForm->getValue('search')));



                    if (count($r)) {

                        foreach ($r as $a) {

                            $list[$a->getUid()] = $a->getName();

                        }

                        $linkForm->_agency->addMultiOptions($list);

                    } else {

                        $linkForm->_agency->clearMultiOptions();

                    }

                }

                if(!is_null($_POST["submit_link"])){



                    $body ='';



                    foreach($_POST as $name => $value){

                        $body.= '<b>' . $name . ' :</b><textarea> ' .$value . '</textarea><br>';

                    }



                    $subject = '/!\ Important : Un user veut administrer une agence RA/!\\';

                    $recipients = array('samy@seerus.com', 'julie@seerus.com', 'quentin@seerus.com' , 'nicolas@seerus.com', 'geoffroy@realadvice.be');



                    $mail = Pimcore\Tool::getMail($recipients, $subject);

                    $mail->setBodyHtml($body);

                    $mail->send();



                    $this->view->success = true;



                }

            }





        }



        $this->view->conectForm = $linkForm;







    }



    public function surveyInfoAction(){

        $this->enableLayout()->setLayout('clean2');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/clean.css');

        $this->view->headLink()->appendStylesheet('/website/views/layouts/css/account.css');



        $user = Auth::getCurrentUser();



        if ($user instanceof Auth){

            if ($user->getAgency_uid() == '') return;

            $survey = \Website\Model\Agency::getByUid($user->getAgency_uid());

            if($survey instanceof \Website\Model\Agency){

                $this->view->survey = $survey;

            }

        }

    }

}