<?php
    /*===
        function: show_dept_details: void -> void
        purpose: expects nothing, and
            tries to use the department number that should be
            in $_POST["dept"] to query for that department's
            name and location, and display them tastefully to
            the screen. It then includes a link to the calling
            PHP to start over.
            *   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, hum_conn_sess.php

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

    function show_dept_details()
    {
        // if get here: BETTER be a dept choice!

        if (! array_key_exists("dept", $_POST))
        {
            destroy_then_exit("Must select a department");
        }

        // department choice should just be a department number --
        //    a PROBLEM if it is anything else!
        // AFTER CLASS --
        //     aha: is_numeric: from the PHP manual: returns true if argument
        //         is a number or numeric string, and returns false otherwise!
        // THAT's a more-robust choice for validating this!

        $dept_choice = $_POST["dept"];

        if (! is_numeric($dept_choice))
        {
            destroy_then_exit("Tried to submit a non-numeric department number!");
        }

        // if get here, $dept_choice should contain a number

        $conn = hum_conn_sess();

        $dept_info_str = "select dept_name, dept_loc
                          from dept
                          where dept_num = :dept_choice";

        $dept_info_stmt = oci_parse($conn, $dept_info_str);

        oci_bind_by_name($dept_info_stmt, ":dept_choice", $dept_choice);
        oci_execute($dept_info_stmt);

        // SHOULD only be ONE row in this result, but STILL must
        //     call oci_fetch to get it!

        oci_fetch($dept_info_stmt);
        $chosen_name = oci_result($dept_info_stmt, "DEPT_NAME");
        $chosen_loc = oci_result($dept_info_stmt, "DEPT_LOC");
        
        oci_free_statement($dept_info_stmt);
        oci_close($conn);
        
        ?>
        <ul>
            <li> Name: <?= $chosen_name ?> </li>
            <li> Location: <?= $chosen_loc ?> </li>
        </ul>

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