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

<!--
    CS 328 - Week 11 Lecture 1 - postback to request info
        for a specific employee

    by: Sharon Tuttle
    last modified: 2025-04-08

    you can run this using the URL:

    https://nrs-projects.humboldt.edu/~st10/s25cs328/328lect11-1/328lect11-1-empl-request.php

    note:
    validated by:
    *   getting an .xhtml version of the executed result with the
        FORM by copying the resulting source from a
        browser's "show source" for this document with the form
        and pasting it into a file ending with -1.xhtml on nrs-projects

    *   getting an .xhtml version of the executed result with the
        FORM's RESPONSE by copying the resulting source from a
        browser's "show source" for this document with the form's response
        and pasting it into a file ending with -2.xhtml on nrs-projects

    *   validating those two .xhtml versions using one of our usual
        validator links
-->

<head>
    <title> Employee Request </title>
    <meta charset="utf-8" />

    <?php
        // enable error reporting (for now, at least) for this
        //     particular PHP document

        ini_set('display_errors', 1);
        error_reporting(E_ALL);

        // get the function hum_conn_no_login

        require_once("hum_conn_no_login.php");
    ?>
    
    <link href="https://nrs-projects.humboldt.edu/~st10/styles/normalize.css"
          type="text/css" rel="stylesheet" />
</head>

<body>
    <h1> Employee Request </h1>
    
    <?php
    // either generate a form to specify a desired employee
    //     or respond with the selected employee's desired information

    if ($_SERVER["REQUEST_METHOD"] == "GET")
    {
        // if I am here, generate a form with a dynamic select/drop-down
        //     of employees
        
        ?>

        <form method="post"
              action="<?= htmlentities($_SERVER["PHP_SELF"],
                                       ENT_QUOTES) ?>">
            <label for="empl_choice"> Select an employee: </label>
            <select id="empl_choice" name="empl_chosen">
            <?php

            $conn = hum_conn_no_login();

            // because I wrote hum_conn_no_login to already fail and
            //    exit if the connection could not be made,
            //    if I get here, I should have connected

            $empl_query_str = "select empl_num, empl_last_name
                               from empl
                               order by empl_last_name";

            $empl_stmt = oci_parse($conn, $empl_query_str);

            oci_execute($empl_stmt, OCI_DEFAULT);

            // make an option element for each employee

            while (oci_fetch($empl_stmt))
            {
                // EITHER style of oci_result 2nd argument is fine!!!!!

                $curr_empl_num = oci_result($empl_stmt, 1);
                $curr_empl_name = oci_result($empl_stmt, "EMPL_LAST_NAME");
                ?>

                <option value="<?= $curr_empl_num ?>"> <?= $curr_empl_name ?> - <?= $curr_empl_num ?> </option>
                <?php
            }
            
            oci_free_statement($empl_stmt);
            oci_close($conn);
            ?>

            </select> <br />
            <input type="submit" />
        </form>
        <?php
    }

    // else, get info for selected employee

    else
    {
        // get value submitted for select/drop down, stripping
        //    any tags a rogue submitter might have included
        //    (employee numbers should not have angle brackets in them!)

        $chosen_empl = strip_tags($_POST["empl_chosen"]);

        $conn = hum_conn_no_login();

        $empl_info_str = "select job_title, hiredate, salary
                          from empl
                          where empl_num = :chosen_empl";

        $empl_info_stmt = oci_parse($conn, $empl_info_str);

        // GIVE each bind variable a value!!!!!

        oci_bind_by_name($empl_info_stmt, ":chosen_empl", $chosen_empl);

        oci_execute($empl_info_stmt, OCI_DEFAULT);

        // AHA!! FOUND THE IN-CLASS ERROR!!!!
        // Remember: you MUST *fetch* the selected row before you
        //    can access its contents! Even for a select returning
        //    just one row, like this one!!!

        oci_fetch($empl_info_stmt);

        // again: just showing that oci_result can use either the
        //    1-based position of the desired projected expression,
        //    OR the ALL-UPPERCASE version of that desired projected
        //    expression, as its 2nd argument -- either is fine!!

        ?>

        <p> For selected employee <?= $chosen_empl ?>: </p>
        <ul>
            <li> hire date: <?= oci_result($empl_info_stmt, "HIREDATE") ?> </li>
            <li> salary: $<?= oci_result($empl_info_stmt, 3) ?> </li>
            <li> job title: <?= oci_result($empl_info_stmt, 1) ?> </li>     
        </ul>

        <p> <a href="<?= htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES) ?>">
            Request info for another employee</a> </p>

        <?php
        oci_free_statement($empl_info_stmt);
        oci_close($conn);
    }
    require_once("328footer-plus-end.html");
    ?>