<?php 
include_once 'database/dbCrews.php';
include_once 'database/dbPersons.php';
include_once 'database/dbLog.php';
include_once 'editCrew.inc';
$crewid = $_GET['id'];
$group = substr($crewid, 9);
if ($crewid == "") {
    echo "<p>No Crew ID Supplied.  Click on \"Calendar\" above to edit crews.</p>";
} else {
    $crew = select_dbCrews($crewid);
    if (!$crew instanceof Crew) {
        echo "<p>Invalid Crew ID Supplied.  Click on \"Calendar\" above to edit crews.</p>";
    } else {
        if (!process_fill_vacancy($_POST, $crew, $group) && !process_add_volunteer($_POST, $crew, $group)) {
            if (process_unfill_crew($_POST, $crew, $group)) {
                $crew = select_dbCrews($crewid);
            } else {
                if (process_add_slot($_POST, $crew, $group)) {
                    $crew = select_dbCrews($crewid);
                } else {
                    if (process_clear_crew($_POST, $crew, $group)) {
                        $crew = select_dbCrews($crewid);
                    } else {
                        if (process_ignore_slot($_POST, $crew, $group)) {
                            $crew = select_dbCrews($crewid);
                        }
                    }
                }
            }
            $persons = $crew->get_persons();
if ($group == "" || $day == "" || $week_no == "") {
    echo "<p>Invalid schedule parameters.  Please click on the \"Master Schedule\" link above to edit a master schedule crew.</p>";
} else {
    if (retrieve_dbMasterSchedule($group . $day . $week_no) == false) {
        $result = process_set_times($_POST, $group, $day, $week_no);
        if ($result) {
            $returnpoint = "viewSchedule.php?group=" . $group;
            echo "<table align=\"center\"><tr><td align=\"center\" width=\"442\">\n\t\t\t\t\t\t\t\t<br><a href=\"" . $returnpoint . "\">\n\t\t\t\t\t\t\t\tBack to Master Schedule</a></td></tr></table>";
        } else {
            echo "<table align=\"center\" width=\"450\"><tr><td align=\"center\" colspan=\"2\"><b>\n\t\t\t\t\t\t\t\tAdding a New Master Schedule crew for " . $do_group_name($group) . ", " . do_week_name($week_no) . " " . do_day_name($day) . " " . "</b></td></tr>" . "<tr><td>\n\t\t\t\t\t\t\t\t\t<form method=\"POST\" style=\"margin-bottom:0;\">\n\t\t\t\t\t\t\t\t\t<select name=\"new_slots\">\n\t\t\t\t\t\t\t\t\t<option value=\"0\">Please select the number of slots for this crew</option>" . slots_select() . "</select><br>\n\t\t\t\t\t\t\t\t\t<br><br>\n\t\t\t\t\t\t\t\t\t<input type=\"hidden\" name=\"_submit_change_times\" value=\"1\">\n\t\t\t\t\t\t\t\t\t<input type=\"submit\" value=\"Add New Crew\" name=\"submit\">\n\t\t\t\t\t\t\t\t\t</form><br></td></tr></table>";
        }
    } else {
        // if one is there, see what we can do to update it
        if (!process_fill_vacancy($_POST, $group, $day, $week_no) && !process_add_volunteer($_POST, $group, $day, $week_no) && !process_remove_crew($_POST, $group, $day, $week_no)) {
            // try to remove the crew
            if (process_unfill_crew($_POST, $group, $day, $week_no)) {
                // try to remove a person
            } else {
                if (process_add_slot($_POST, $group, $day, $week_no)) {
                    // try to add a new slot
                } else {
                    if (process_ignore_slot($_POST, $group, $day, $week_no)) {
                        //try to remove a slot
                    }
                }
            }
            // we've tried to clear the crew, add a slot, or remove a slot;
            // so now display the crew again.
            $persons = get_persons($group, $day, $week_no);
            echo "<br><table align=\"center\" width=\"450\" border=\"1px\"><tr><td align=\"center\" colspan=\"2\"><b>\n\t\t\t\t\t\t\t\tEditing master schedule crew for " . do_group_name($group) . "<br>" . do_week_name($week_no) . " " . do_day_name($day) . "\n\t\t\t\t\t\t\t\t</b>\n\t\t\t\t\t\t\t\t<form method=\"POST\" style=\"margin-bottom:0;\">\n\t\t\t\t\t\t\t\t\t<input type=\"hidden\" name=\"_submit_remove_crew\" value=\"1\"><br>\n\t\t\t\t\t\t\t\t\t<input type=\"submit\" value=\"Remove Entire Crew\"\n\t\t\t\t\t\t\t\t\tname=\"submit\">\n\t\t\t\t\t\t\t\t\t</form>\n\t\t\t\t\t\t\t\t</td></tr>" . "<tr><td valign=\"top\"><br>&nbsp;" . do_slot_num($group, $day, $week_no) . "</td><td>\n\t\t\t\t\t\t\t\t\t<form method=\"POST\" style=\"margin-bottom:0;\">\n\t\t\t\t\t\t\t\t\t<input type=\"hidden\" name=\"_submit_add_slot\" value=\"1\"><br>\n\t\t\t\t\t\t\t\t\t<input type=\"submit\" value=\"Add Slot\"\n\t\t\t\t\t\t\t\t\tname=\"submit\" style=\"width: 250px\">\n\t\t\t\t\t\t\t\t\t</form></td></tr>";
            echo display_filled_slots($persons) . display_vacant_slots(get_total_vacancies($group, $day, $week_no)) . "</table>";