<?php
session_start();

if (! isset($_SESSION['handle'])) {
    $_SESSION['handle'] = sprintf('%09s', ( round(microtime(true)*10) % 1000000000));
    $_SESSION['date'] = date('c');
    $_SESSION['complete'] = false;
}

$redirect = NULL;
if (isset($_REQUEST['form'])) {
    switch ($_REQUEST['form']) {
        case 'step1':
            $number = NULL;
            if (isset($_REQUEST['phone']) && trim($_REQUEST['phone']) != '') {
                $number = $_REQUEST['phone'];
            }
            if (! $number) {
                $redirect = 'index.php?error&num=';
                break;
            }

            require_once('vendor/autoload.php');

            function format_number($number) {
                $phoneNumberUtil = \libphonenumber\PhoneNumberUtil::getInstance();
                try {
                    $phoneNumber = $phoneNumberUtil->parse($number, 'DE');
                } catch (Exception $e) {
                    return NULL;
                }
                if ($phoneNumberUtil->isValidNumber($phoneNumber)) {
                    return $phoneNumberUtil->format($phoneNumber, \libphonenumber\PhoneNumberFormat::NATIONAL);
                }
                return NULL;
            }
            $number = format_number($number);

            $_SESSION['phone'] = $number;
            setcookie('NUMBER', $number, time() + 24*3600*500, "");

            if (! $_SESSION['phone']) {
                header('Location: index.php?error&num='.urlencode($_REQUEST['phone']));
                break;
            }   

            $redirect = 'address.php';
            break;

        case 'step2':
            $fields = array("fname", "lname", "address", "zip", "city");
            foreach ($fields as $f) {
                if (isset($_REQUEST[$f])) {
                    $_SESSION[$f] = trim($_REQUEST[$f]);
                } else {
                    if (! isset($_SESSION[$f])) {
                        $_SESSION[$f] = '';
                    }
                }
            }
            $redirect = 'gitterbox.php';
            break;

        case 'step3':
            if (isset($_REQUEST['keinobst'])) {
                $_SESSION['lieferart'] = 'kommtnoch';
            }
            if (isset($_REQUEST['lieferart'])) {
                if ($_REQUEST['lieferart'] == 'gitterbox') {
                    $_SESSION['lieferart'] = 'gitterbox';
                    $_SESSION['gbcount'] = $_REQUEST['gbcount'];
                }
                if ($_REQUEST['lieferart'] == 'anhaenger') {
                    $_SESSION['lieferart'] = 'anhaenger';
                    $_SESSION['kennz'] = $_REQUEST['kennz'];
                }
            }
            $redirect = 'gebrauchte.php';
            break;

         case 'step4':
            if (isset($_REQUEST['kartonsja'])) {
                $_SESSION['kartons'] = true;
            } 
            if (isset($_REQUEST['kartonsnein'])) {
                $_SESSION['kartons'] = false;
            }

            $redirect = 'auftrag.php';
            break;
         case 'step5':
            if (isset($_REQUEST['5er'])) {
                $_SESSION['neue'] = '5er';
            }
            if (isset($_REQUEST['10er'])) {
                $_SESSION['neue'] = '10er';
            }
            if (isset($_REQUEST['submit']) && isset($_REQUEST['sonstiges']) && $_REQUEST['sonstiges'] != '') {
                $_SESSION['neue'] = 'sonstiges';
                $_SESSION['sonstiges'] = $_REQUEST['sonstiges'];
            }


            $redirect = 'frischsaft.php';
            break;

         case 'step6':
            if (isset($_REQUEST['frischsaftnein'])) {
                $_SESSION['frischsaft'] = 0;
            } elseif (isset($_REQUEST['frischsaft'])) {
                $_SESSION['frischsaft'] = (int) $_REQUEST['frischsaft'];
            }

            $redirect = 'summary.php';
            break;
        case 'step7':
            if (!isset($_REQUEST['agb'])) {
                $redirect = 'summary.php?error';
                break;
            }

            $_SESSION['complete'] = true;
            $redirect = 'finish.php?handle='.$_SESSION['handle'];
            break;

    }
}


$filename = '../data/'.date('Y').'/'.$_SESSION['handle'].'.json';
if (!file_exists(dirname($filename))) {
    mkdir(dirname($filename), 0700, true);
}

$json = json_encode($_SESSION);
file_put_contents($filename, $json);

header('Location: '.$redirect);