<?php
  ## GET ALL LOCATIONS
  //$allLocations = '';
 // $locArray = array();
if (!isset($account)) {
    echo "<p>No account selected</p>";
    exit;
}

    /*
    $q = "SELECT * from `locations` WHERE `account_id`=$account;";
    if ($res = mysql_query($q, $link)) {
        while ($row = mysql_fetch_assoc($res)) {
          $id = $row['id'];
          $selected = ($id == $locID) ? 'selected="selected"' : '';
          $allLocations .= '<option ' . $selected . ' value="' . $id . '">' . $row['name'] . '</option>' . "\n";
          $locArray[$row['id']] = $row;
        }
      }
    */
    /*
      ## GET LOCATION INFO
      if ($locID) {
        $start_time      = $locArray[$locID]['start_time'];
        $hourMin         = substr($start_time,0,2);
        $minuteMin       = substr($start_time,2,2);
        $end_time	     = $locArray[$locID]['end_time'];
        $hourMax         = substr($end_time,0,2);
        $minuteMax       = substr($end_time,2,2);
        $appt_increments = $locArray[$locID]['appt_increments'];
      }
    */
    /*
      ## GET ALL EMAILS FOR LOCATION
      $allEmails = '';
      if ($locID) {
        $q = "SELECT * FROM `emails` WHERE `location_id`=$locID;";
        if ($res = mysql_query($q, $link)) {
          while ($row = mysql_fetch_assoc($res)) {
            $id = $row['id'];
            $selected = ($eid) ? ($id == $eid) ? 'selected="selected"' : '' :  '';
            $allEmails .= '<option ' . $selected . ' value="' . $id . '">' . $row['emailName'] . '</option>' . "\n";
          }
        }
        }
        ## GET THE SERVICES, STAFF & APPOINTMENTS
        $services = '';
        $servicesArray = array();
        $staff = '';
        $staffArray = array();
        $apptList = '';
        $apptArray = array();

        if ($locID) {
            $svcQuery = 'SELECT * from `services` WHERE `location_id` = ' . $locID . ';';
            $svcRes = mysql_query($svcQuery, $link);
            while ($row = mysql_fetch_assoc($svcRes)) {
            $servicesArray[$row["id"]] = $row;
        }
        $stfQuery = 'SELECT * from `staff` WHERE `location_id` = ' . $locID . ';';
        $stfRes = mysql_query($stfQuery, $link);
        while ($row = mysql_fetch_assoc($stfRes)) {

        $staffArray[$row["id"]] = $row;
        }
      }
    */
      ## GET THE SPECIFIC EMAIL & APPTS IF SPECIFIED

    /*
      if ($eid) {
        //echo 'eid: ' . $eid . '<br />';
        $q = "SELECT * FROM `emails` WHERE `id`='$eid';";
        if ($res = mysql_query($q, $link)) {
          $row = mysql_fetch_assoc($res);
          $savedEmailName  = $row['emailName'];
          $savedHeadline   = $row['headline'];
          $savedHeaderText = $row['headerText'];
        }
        $q = "SELECT * FROM `appointments` WHERE `email_id`='$eid' ORDER BY `date`,`time`;";
        if ($res = mysql_query($q, $link)) {
          while ($row = mysql_fetch_assoc($res)) {
            $staffID = $row['staff_id'];
            $serviceID = $row['service_id'];
            $apptList .= '<div class="apptLink"><span class="appt-id">' . $row['id'] . '</span>' . substr($row['date'],0,5) . ' ' . $row['time'] . ' | ' .
            $servicesArray[$serviceID]['name'] . ' | ' . $staffArray[$staffID]['name'] .
            ' <a href="manager.php?location=' .
            $locID . '&email_id=' . $eid . '&appt_id=' . $row['id'] .'" class="edit">edit</a>' .
            ' <a href="manager.php?location=' .
            $locID . '&email_id=' . $eid . '&formAction=deleteAppointment&appt_id=' .
            $row['id'] . '" class="delete">delete</a></div>' . "\n";
            $apptArray[$row['id']] = $row;
          }
        }
        if ($appt_id) {
          $savedApptDate     = $apptArray[$appt_id]['date'];
          $savedApptTime     = $apptArray[$appt_id]['time'];
          $savedApptService  = $apptArray[$appt_id]['service_id'];
          $savedApptStaff    = $apptArray[$appt_id]['staff_id'];
        }
      }
    $apptSvc   = (isset($savedApptService)) ? $savedApptService : '';
    $apptStaff = (isset($savedApptStaff)) ? $savedApptStaff : '';
    //build services options
    foreach ($servicesArray as $sid =>$svcArr) {
      $selected = ($apptSvc == $sid) ? 'selected="selected"' : '';
      $services .= '<option ' . $selected . ' value= "' . $sid . '">' . $svcArr["name"] . '</option>' . "\n";
    }

    //build staff options
    foreach ($staffArray as $sid =>$stfArr) {
      $selected = ($apptStaff == $sid) ? 'selected="selected"' : '';
      $staff .= '<option ' . $selected . ' value= "' . $sid . '">' . $stfArr["name"] . ' - ' . $stfArr["category"] . '</option>' . "\n";
    }

    $apptsByDate = array();
    foreach ($apptArray as $appt) {
     $date = $appt['date'];
     $time = $appt['time'];
     $m    = (int) substr($date,0,2);
     $d    = (int) substr($date,3,2);
     $y    = (int) substr($date,6,4);
     $h    = (int) substr($time,0,2);
     $mi    = (int) substr($time,3,2);
     $timestamp = mktime($h, $mi, 0, $m, $d, $y);
     $displayDate = date("l, F jS", $timestamp);
     $appt['prettyTime'] = date("g:ia", $timestamp);
     $appt['serviceName'] = $servicesArray[$appt['service_id']]['name'];
     $appt['staffName'] = $staffArray[$appt['staff_id']]['name'];
      $appt['staffLevel'] = $staffArray[$appt['staff_id']]['category'];
     $apptsByDate[$displayDate][] = $appt;

    }
    */
