<?php
class  MbeshippingMbeuapModuleFrontController  extends ModuleFrontController
{
    public function postProcess()
    {
        try {
            $mbeSelectedUap = Tools::getValue('mbeSelectedUap');

            if ($mbeSelectedUap !== -1) {
                $mbeUapList = Tools::getValue('mbeUapList');

                $mbeUapList = unserialize(base64_decode($mbeUapList));

                $uap = array_search($mbeSelectedUap, array_column($mbeUapList, 'PublicAccessPointID'));

                $selUap = array();
                $selUap['PublicAccessPointID'] = $mbeUapList[$uap]['PublicAccessPointID'];
                $selUap['ConsigneeName'] = $mbeUapList[$uap]['ConsigneeName'];
                $selUap['AddressLine'] = $mbeUapList[$uap]['AddressLine'];
                $selUap['PoliticalDivision2'] = $mbeUapList[$uap]['PoliticalDivision2'];
                $selUap['PostcodePrimaryLow'] = $mbeUapList[$uap]['PostcodePrimaryLow'];

                /*if (_PS_VERSION_ >= 1.7) {
                    $this->context->cookie->__set('mbe_mdp', base64_encode(serialize($selUap)));

                    $this->context->cookie->write();*/

                    if (session_status() == PHP_SESSION_ACTIVE) {
                        $_SESSION['mbe_mdp'] = base64_encode(serialize($selUap));
                    } elseif (session_status() == PHP_SESSION_NONE) {
                        session_start();
                        $_SESSION['mbe_mdp'] = base64_encode(serialize($selUap));
                    } else {
                        setcookie('mbe_mdp', base64_encode(serialize($selUap)));
                    }
              /*  }
                else {
                    setcookie("mbe_mdp", base64_encode(serialize($selUap)));
                }*/
            } else {
                /*if (_PS_VERSION_ >= 1.7) {
                    $this->context->cookie->__unset('mbe_mdp');
                }
                else
                {
                    setcookie("mbe_mdp", "", time()-3600);
                }*/
                if (session_status() == PHP_SESSION_NONE) {
                    session_start();
                }

                if (session_status() == PHP_SESSION_ACTIVE && isset($_SESSION['mbe_mdp'])) {
                    unset($_SESSION['mbe_mdp']);
                } elseif (isset($_COOKIE['mbe_mdp'])) {
                    unset($_COOKIE['mbe_mdp']);
                }
            }


            die;
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }
}