<?php



use Website\Controller\Action;



class AdminController extends Action {



    public function insertAction(){

        $this->enableLayout();

        $this->setLayout('empty');



        if(isset($_GET['uid']) && $_GET['uid'] != '')

            $this->view->mode = 'edit';

        elseif(isset($_POST['do_update'])){

            $this->view->mode = 'do_update';

        }else{

            $this->view->mode = 'new';

        }





        if($this->view->mode == 'edit'){

            // Affiche une agence pour l'éditer...



            $uid = $_GET['uid'];

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

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



        }

        elseif($this->view->mode == 'do_update'){



            // L'agence à été éditée

            $agency = new Website\Model\Agency();



            $agency->setId($_POST['id']);

            $agency->setUid($_POST['uid']);



            $agency->setName($_POST['name']);

            $agency->setPhone($_POST['phone']);

            $agency->setEmail($_POST['email']);

            $agency->setAddress($_POST['address']);

            $agency->setCp($_POST['cp']);



            $agency->setShort_description($_POST['description']);

            $agency->setLong_description($_POST['long_description']);



            $agency->setWebsite($_POST['website']);

            $agency->setFacebook($_POST['facebook']);

            $agency->setTwitter($_POST['twitter']);

            $agency->setLinkedin($_POST['linkedin']);



            $agency->setSeerus_key($_POST['seerus_key']);



            if ($_POST['lundi_is_open'] == 0){

                $lundiAmStart   = 0;

                $lundiAmEnd     = 0;

                $lundiPmStart   = 0;

                $lundiPmEnd     = 0;

            }else {

                if ($_POST['lundi_is_open'] == 2) {

                    $lundiAmStart = -1;

                    $lundiAmEnd = -1;

                    $lundiPmStart = -1;

                    $lundiPmEnd = -1;

                }else{

                    if ($_POST['lundi_has_lunch'] == 0) {

                        $lundiAmStart = $_POST['lundi_am_start_0'];

                        $lundiAmEnd = -3;

                        $lundiPmStart = -3;

                        $lundiPmEnd = $_POST['lundi_pm_end_0'];

                    }else{

                        if($_POST['lundi_am_on_rdv'] == 1){

                            $lundiAmStart = -1;

                            $lundiAmEnd = -1;

                        }else{

                            $lundiAmStart = $_POST['lundi_am_start_1'];

                            $lundiAmEnd = $_POST['lundi_am_end_1'];

                        }

                        if($_POST['lundi_pm_on_rdv'] == 1){

                            $lundiPmStart = -1;

                            $lundiPmEnd = -1;

                        }else{

                            $lundiPmStart = $_POST['lundi_pm_start_1'];

                            $lundiPmEnd = $_POST['lundi_pm_end_1'];

                        }

                    }

                }

            }



            if ($_POST['mardi_is_open'] == 0){

                $mardiAmStart   = 0;

                $mardiAmEnd     = 0;

                $mardiPmStart   = 0;

                $mardiPmEnd     = 0;

            }else {

                if ($_POST['mardi_is_open'] == 2) {

                    $mardiAmStart = -1;

                    $mardiAmEnd = -1;

                    $mardiPmStart = -1;

                    $mardiPmEnd = -1;

                }else{

                    if ($_POST['mardi_has_lunch'] == 0) {

                        $mardiAmStart = $_POST['mardi_am_start_0'];

                        $mardiAmEnd = -3;

                        $mardiPmStart = -3;

                        $mardiPmEnd = $_POST['mardi_pm_end_0'];

                    }else{

                        if($_POST['mardi_am_on_rdv'] == 1){

                            $mardiAmStart = -1;

                            $mardiAmEnd = -1;

                        }else{

                            $mardiAmStart = $_POST['mardi_am_start_1'];

                            $mardiAmEnd = $_POST['mardi_am_end_1'];

                        }

                        if($_POST['mardi_pm_on_rdv'] == 1){

                            $mardiPmStart = -1;

                            $mardiPmEnd = -1;

                        }else{

                            $mardiPmStart = $_POST['mardi_pm_start_1'];

                            $mardiPmEnd = $_POST['mardi_pm_end_1'];

                        }

                    }

                }

            }



            if ($_POST['mercredi_is_open'] == 0){

                $mercrediAmStart   = 0;

                $mercrediAmEnd     = 0;

                $mercrediPmStart   = 0;

                $mercrediPmEnd     = 0;

            }else {

                if ($_POST['mercredi_is_open'] == 2) {

                    $mercrediAmStart = -1;

                    $mercrediAmEnd = -1;

                    $mercrediPmStart = -1;

                    $mercrediPmEnd = -1;

                }else{

                    if ($_POST['mercredi_has_lunch'] == 0) {

                        $mercrediAmStart = $_POST['mercredi_am_start_0'];

                        $mercrediAmEnd = -3;

                        $mercrediPmStart = -3;

                        $mercrediPmEnd = $_POST['mercredi_pm_end_0'];

                    }else{

                        if($_POST['mercredi_am_on_rdv'] == 1){

                            $mercrediAmStart = -1;

                            $mercrediAmEnd = -1;

                        }else{

                            $mercrediAmStart = $_POST['mercredi_am_start_1'];

                            $mercrediAmEnd = $_POST['mercredi_am_end_1'];

                        }

                        if($_POST['mercredi_pm_on_rdv'] == 1){

                            $mercrediPmStart = -1;

                            $mercrediPmEnd = -1;

                        }else{

                            $mercrediPmStart = $_POST['mercredi_pm_start_1'];

                            $mercrediPmEnd = $_POST['mercredi_pm_end_1'];

                        }

                    }

                }

            }



            if ($_POST['jeudi_is_open'] == 0){

                $jeudiAmStart   = 0;

                $jeudiAmEnd     = 0;

                $jeudiPmStart   = 0;

                $jeudiPmEnd     = 0;

            }else {

                if ($_POST['jeudi_is_open'] == 2) {

                    $jeudiAmStart = -1;

                    $jeudiAmEnd = -1;

                    $jeudiPmStart = -1;

                    $jeudiPmEnd = -1;

                }else{

                    if ($_POST['jeudi_has_lunch'] == 0) {

                        $jeudiAmStart = $_POST['jeudi_am_start_0'];

                        $jeudiAmEnd = -3;

                        $jeudiPmStart = -3;

                        $jeudiPmEnd = $_POST['jeudi_pm_end_0'];

                    }else{

                        if($_POST['jeudi_am_on_rdv'] == 1){

                            $jeudiAmStart = -1;

                            $jeudiAmEnd = -1;

                        }else{

                            $jeudiAmStart = $_POST['jeudi_am_start_1'];

                            $jeudiAmEnd = $_POST['jeudi_am_end_1'];

                        }

                        if($_POST['jeudi_pm_on_rdv'] == 1){

                            $jeudiPmStart = -1;

                            $jeudiPmEnd = -1;

                        }else{

                            $jeudiPmStart = $_POST['jeudi_pm_start_1'];

                            $jeudiPmEnd = $_POST['jeudi_pm_end_1'];

                        }

                    }

                }

            }



            if ($_POST['vendredi_is_open'] == 0){

                $vendrediAmStart   = 0;

                $vendrediAmEnd     = 0;

                $vendrediPmStart   = 0;

                $vendrediPmEnd     = 0;

            }else {

                if ($_POST['vendredi_is_open'] == 2) {

                    $vendrediAmStart = -1;

                    $vendrediAmEnd = -1;

                    $vendrediPmStart = -1;

                    $vendrediPmEnd = -1;

                }else{

                    if ($_POST['vendredi_has_lunch'] == 0) {

                        $vendrediAmStart = $_POST['vendredi_am_start_0'];

                        $vendrediAmEnd = -3;

                        $vendrediPmStart = -3;

                        $vendrediPmEnd = $_POST['vendredi_pm_end_0'];

                    }else{

                        if($_POST['vendredi_am_on_rdv'] == 1){

                            $vendrediAmStart = -1;

                            $vendrediAmEnd = -1;

                        }else{

                            $vendrediAmStart = $_POST['vendredi_am_start_1'];

                            $vendrediAmEnd = $_POST['vendredi_am_end_1'];

                        }

                        if($_POST['vendredi_pm_on_rdv'] == 1){

                            $vendrediPmStart = -1;

                            $vendrediPmEnd = -1;

                        }else{

                            $vendrediPmStart = $_POST['vendredi_pm_start_1'];

                            $vendrediPmEnd = $_POST['vendredi_pm_end_1'];

                        }

                    }

                }

            }



            if ($_POST['samedi_is_open'] == 0){

                $samediAmStart   = 0;

                $samediAmEnd     = 0;

                $samediPmStart   = 0;

                $samediPmEnd     = 0;

            }else {

                if ($_POST['samedi_is_open'] == 2) {

                    $samediAmStart = -1;

                    $samediAmEnd = -1;

                    $samediPmStart = -1;

                    $samediPmEnd = -1;

                }else{

                    if ($_POST['samedi_has_lunch'] == 0) {

                        $samediAmStart = $_POST['samedi_am_start_0'];

                        $samediAmEnd = -3;

                        $samediPmStart = -3;

                        $samediPmEnd = $_POST['samedi_pm_end_0'];

                    }else{

                        if($_POST['samedi_am_on_rdv'] == 1){

                            $samediAmStart = -1;

                            $samediAmEnd = -1;

                        }else{

                            $samediAmStart = $_POST['samedi_am_start_1'];

                            $samediAmEnd = $_POST['samedi_am_end_1'];

                        }

                        if($_POST['samedi_pm_on_rdv'] == 1){

                            $samediPmStart = -1;

                            $samediPmEnd = -1;

                        }else{

                            $samediPmStart = $_POST['samedi_pm_start_1'];

                            $samediPmEnd = $_POST['samedi_pm_end_1'];

                        }

                    }

                }

            }



            if ($_POST['dimanche_is_open'] == 0){

                $dimancheAmStart   = 0;

                $dimancheAmEnd     = 0;

                $dimanchePmStart   = 0;

                $dimanchePmEnd     = 0;

            }else {

                if ($_POST['dimanche_is_open'] == 2) {

                    $dimancheAmStart = -1;

                    $dimancheAmEnd = -1;

                    $dimanchePmStart = -1;

                    $dimanchePmEnd = -1;

                }else{

                    if ($_POST['dimanche_has_lunch'] == 0) {

                        $dimancheAmStart = $_POST['dimanche_am_start_0'];

                        $dimancheAmEnd = -3;

                        $dimanchePmStart = -3;

                        $dimanchePmEnd = $_POST['dimanche_pm_end_0'];

                    }else{

                        if($_POST['dimanche_am_on_rdv'] == 1){

                            $dimancheAmStart = -1;

                            $dimancheAmEnd = -1;

                        }else{

                            $dimancheAmStart = $_POST['dimanche_am_start_1'];

                            $dimancheAmEnd = $_POST['dimanche_am_end_1'];

                        }

                        if($_POST['dimanche_pm_on_rdv'] == 1){

                            $dimanchePmStart = -1;

                            $dimanchePmEnd = -1;

                        }else{

                            $dimanchePmStart = $_POST['dimanche_pm_start_1'];

                            $dimanchePmEnd = $_POST['dimanche_pm_end_1'];

                        }

                    }

                }

            }



            $agency->setMonday_am_start($lundiAmStart);

            $agency->setMonday_am_end  ($lundiAmEnd);

            $agency->setMonday_pm_start($lundiPmStart);

            $agency->setMonday_pm_end  ($lundiPmEnd);



            $agency->setThuesday_am_start($mardiAmStart);

            $agency->setThuesday_am_end  ($mardiAmEnd);

            $agency->setThuesday_pm_start($mardiPmStart);

            $agency->setThuesday_pm_end  ($mardiPmEnd);



            $agency->setThursday_am_start($mercrediAmStart);

            $agency->setThursday_am_end  ($mercrediAmEnd);

            $agency->setThursday_pm_start($mercrediPmStart);

            $agency->setThursday_pm_end  ($mercrediPmEnd);



            $agency->setWednesday_am_start($jeudiAmStart);

            $agency->setWednesday_am_end  ($jeudiAmEnd);

            $agency->setWednesday_pm_start($jeudiPmStart);

            $agency->setWednesday_pm_end  ($jeudiPmEnd);



            $agency->setFriday_am_start($vendrediAmStart);

            $agency->setFriday_am_end  ($vendrediAmEnd);

            $agency->setFriday_pm_start($vendrediPmStart);

            $agency->setFriday_pm_end  ($vendrediPmEnd);



            $agency->setSaturday_am_start($samediAmStart);

            $agency->setSaturday_am_end  ($samediAmEnd);

            $agency->setSaturday_pm_start($samediPmStart);

            $agency->setSaturday_pm_end  ($samediPmEnd);



            $agency->setSunday_am_start($dimancheAmStart);

            $agency->setSunday_am_end  ($dimancheAmEnd);

            $agency->setSunday_pm_start($dimanchePmStart);

            $agency->setSunday_pm_end  ($dimanchePmEnd);



            try{

                $agency->save();

/*              var_dump($agency);

                var_dump(basename($_FILES['logo']['name']));*/

                $this->view->message = "success";

            }catch(\Exception $e){

                $this->view->message = $e->getMessage();

            }



        }

    }



}