<?php
    session_start();
?>

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">

<!--
    Trying to demo PHP, sessions, and OCI in one example - now
        implementing a state diagram version with a "BRANCH-BACK"

    Implementing dept-details VERSION 2 state diagram, at:
    https://nrs-projects.humboldt.edu/~st10/s24cs328/328lect13-1/dept-details-state-diagram.pdf

    last modified: 2024-04-17

    can run from:
    https://nrs-projects.humboldt.edu/~st10/s24cs328/328lect13-1/dept-details-2.php
-->

<head>
    <title> Department Details </title>
    <meta charset="utf-8" />

    <?php
        ini_set('display_errors', 1);
        require_once("choose_widget.php");
        require_once("build_dept_form-2.php");
        require_once("build_dept_drop_down.php");
        require_once("build_dept_radio.php");
        require_once("show_dept_details-2.php");

        require_once("hum_conn_sess.php");
        require_once("destroy_then_exit.php");
    ?>

<link href="https://nrs-projects.humboldt.edu/~st10/styles/normalize.css"
          type="text/css" rel="stylesheet" />

</head>

<body>

    <h1> Department Details </h1>

    <?php
    // I decided to use a next_state session key to help
    //     navigate through my application

    if (! array_key_exists("next_state", $_SESSION))
    {
        choose_widget();
        $_SESSION["next_state"] = "build_dept_form_orig";
    }

    // when I am coming from state choose_widget to build_dept_form

    elseif ($_SESSION["next_state"] == "build_dept_form_orig")
    {
        // if get here, better have submitted
        //    a widget preference

        if (! array_key_exists("widg_choice", $_POST))
        {
            destroy_then_exit("Should have given a widget preference");
        }

        // if get here, widget choice should be available;
        //     using this to determine widget type, not to store or
        //     display -- so will handle any unexpected values with an
        //     else branch

        $widg_choice = $_POST["widg_choice"];

        // and save this choice for later use

        $_SESSION["widg_choice"] = $widg_choice;

        build_dept_form($widg_choice);
        $_SESSION["next_state"] = "show_dept_details";
    }

    // when I am coming to state build_dept_form from show_dept_details

    elseif ( ($_SESSION["next_state"] == "is-user-done")
             && (array_key_exists("another_dept", $_POST)) )
    {
        // get previously-saved widget preference

        $widg_choice = $_SESSION["widg_choice"];

        build_dept_form($widg_choice);
        $_SESSION["next_state"] = "show_dept_details";
    }

    // handling state show_dept_details (which always follows build_dept_form)

    elseif ($_SESSION["next_state"] == "show_dept_details")
    {
        show_dept_details();

        // how do you know when session should be ended?
        //     in this case, it depends which button the
        //     user clicks

        $_SESSION["next_state"] = "is-user-done";
    }

    // handling when user has asked to end this logical session

    elseif ( ($_SESSION["next_state"] == "is-user-done")
             && (array_key_exists("no_more_dept", $_POST)) )
    {
        session_destroy();
        ?>
        <p> Session concluded. </p>
        <p> <a href="<?= htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES) ?>">
            Start Over </a> </p>
        <?php            
    }

    else  // that I hope to never reach!!
    {
        session_destroy();
        ?>
        <p> <strong> YIKES! should not have gotten here! </strong> </p>

        <p> Session concluded. </p>
        <p> <a href="<?= htmlentities($_SERVER['PHP_SELF'], ENT_QUOTES) ?>">
            Start Over </a> </p>
        <?php            
    }
    ?>

    <footer>
    <hr />
    <p>
        Validate by pasting .xhtml copy's URL into<br />
        <a href="https://validator.w3.org/nu">
            https://validator.w3.org/nu
        </a>
        or  
        <a href="https://html5.validator.nu/">
            https://html5.validator.nu/
        </a>
    </p>
    </footer>
</body>
</html>