<?php
    /*===
        function: build_dept_form: void -> void
        purpose: expects nothing, and
            tries to build a dynamic form allowing the user
            to choose one of the current departments using the
            widget type that should be in $_POST["widg_choice"]
            *   if anything unexpected happens, it complains and then exits,
                including destroying the current session and including a
                link to hopefully-conveniently start over

        requires: destroy_then_exit.php

        last modified: 2024-04-17
    ===*/

    function build_dept_form()
    {
        // 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"] = $_POST["widg_choice"];

        // now build a form to choose a department using the chosen
        //     widget type

        ?>
        <form method="post"
              action="<?= htmlentities($_SERVER['PHP_SELF'],
                                       ENT_QUOTES) ?>">
            <fieldset>
                <fieldset>
                    <legend> Choose department: </legend>
                    
        <?php

        // now add the department choices using the chosen form widget

        if ($widg_choice == "radio")
        {
            build_dept_radio();
        }
        elseif ($widg_choice == "select")
        {
            build_dept_drop_down();
        }
        else
        {
            destroy_then_exit("Illegal widget choice");
        }

        // now finish the form
        ?>
                </fieldset>

                <div class="sub">
                    <input type="submit" value="Submit" />
                </div>
            </fieldset>
        </form>
        <?php
    }
?>