<?php
require "./Cart.php";

if (!empty($AddOpt) && !empty($OptionName) && !empty($OptionType)) {
     mysql_query("INSERT INTO ItemOption (ItemID, OptionName, OptionType) VALUES ($iid, '$OptionName', '$OptionType')");
}

if (!empty($DelOpt)) {
     mysql_query("DELETE FROM ItemOption WHERE OptionID = $oid");
     mysql_query("DELETE FROM OptionValue WHERE OptionID = $oid");
}

if (!empty($AddVal)) {
     mysql_query("INSERT INTO OptionValue (OptionID, ValueName) VALUES ($oid, '$value')");
}

if (!empty($DelVal)) {
     $vlist = is_array($vid) ? implode(',',$vid) : "$vid";
     mysql_query("DELETE FROM OptionValue WHERE ValueID IN ($vlist)");
}

adminHeader("$Company","Administrative Functions");

echo "<FORM method=\"post\" action=\"$PHP_SELF?topic=itemOptions.php\">";

echo '<SELECT name=iid>';
echo '<OPTION value="">Select a product to edit Options';
$result=mysql_query("SELECT ItemID, ItemName FROM Items");
while (list($ItemID, $ItemName)=mysql_fetch_row($result)) {
     $selected = ($iid == $ItemID) ? 'SELECTED' : '';
     echo "<OPTION value=$ItemID $selected> $ItemName";
}
mysql_free_result($result);
echo '</SELECT>';

echo '<input type="submit" name="show" value=" Show Options "><hr>';

blueFont('Arial', '<br>Option Name:<br>');
echo '<input type="text" name="OptionName"> ';

echo '<select name="OptionType">';
echo '<option value=""> Select Option Type';
echo '<option value="text"> text box';
echo '<option value="select"> selector (single selection)';
echo '<option value="selectm"> selector (multiple selections)';
echo '<option value="radio"> radio button';
echo '<option value="checkbox"> check box';
echo '<option value="textarea"> text window';
echo '</select>';

echo '<input type="submit" name="AddOpt" value=" Add Option "><hr>';
echo '</FORM>';

echo '<table>';
$result=mysql_query("SELECT OptionID, OptionName, OptionType FROM ItemOption WHERE ItemID = $iid ORDER BY OptionID");
while (list($OptionID, $OptionName, $OptionType)=mysql_fetch_row($result)) {
     echo "<FORM method=\"post\" action=\"$PHP_SELF?topic=itemOptions.php\">";
     echo "<INPUT type=\"hidden\" name=\"iid\" value=\"$iid\">";
     echo "<INPUT type=\"hidden\" name=\"oid\" value=\"$OptionID\">";
     echo "<tr><td valign=top>$OptionName</td><td valign=top>";

     $deleteval = '';
     switch ($OptionType) {
     case 'text':
          echo '<input type="text">';
          break;
     case 'textarea':
          echo '<textarea rows="3" cols="40"></textarea>';
          break;
     case 'select':
          echo '<select name="vid">';
          $ov = mysql_query("SELECT ValueID, ValueName FROM OptionValue WHERE OptionID = $OptionID");
          while (list($vid, $vname) = mysql_fetch_row($ov)) {
               echo "<option value=$vid> $vname";
          }
          mysql_free_result($ov);
          echo '</select>';
          $deleteval = '<input type="submit" name="DelVal" value="delete selected value">';
          echo '<br><input type="text" name="value"> ';
          echo '<input type="submit" name="AddVal" value="add value">';
          break;
     case 'selectm':
          echo '<select name="vid[]" size="5" multiple>';
          $ov = mysql_query("SELECT ValueID, ValueName FROM OptionValue WHERE OptionID = $OptionID");
          while (list($vid, $vname) = mysql_fetch_row($ov)) {
               echo "<option value=$vid> $vname";
          }
          mysql_free_result($ov);
          echo '</select>';
          $deleteval = '<input type="submit" name="DelVal" value="delete selected value(s)">';
          echo '<br><input type="text" name="value"> ';
          echo '<input type="submit" name="AddVal" value="add value">';
          break;
     case 'checkbox':
          $ov = mysql_query("SELECT ValueID, ValueName FROM OptionValue WHERE OptionID = $OptionID");
          while (list($vid, $vname) = mysql_fetch_row($ov)) {
               echo "<input name=\"vid[]\" type=\"checkbox\" value=\"$vid\"> $vname<br>";
          }
          mysql_free_result($ov);
          $deleteval = '<input type="submit" name="DelVal" value="delete checked value(s)">';
          echo '<br><input type="text" name="value"> ';
          echo '<input type="submit" name="AddVal" value="add value">';
          break;
     case 'radio':
          $ov = mysql_query("SELECT ValueID, ValueName FROM OptionValue WHERE OptionID = $OptionID");
          while (list($vid, $vname) = mysql_fetch_row($ov)) {
               echo "<input name=\"vid\" type=\"radio\" value=\"$vid\"> $vname<br>";
          }
          mysql_free_result($ov);
          $deleteval = '<input type="submit" name="DelVal" value="delete checked value">';
          echo '<br><input type="text" name="value"> ';
          echo '<input type="submit" name="AddVal" value="add value">';
          break;
     }

     echo '</td><td valign="top">';
     if (!empty($deleteval))
          echo "$deleteval<br>";
     echo '<INPUT type="submit" name="DelOpt" value=" Remove ' . $OptionName . '"></td></tr>';
     echo '<tr><td colspan=3><hr></td></tr>';
     echo '</FORM>';
}
mysql_free_result($result);
echo '</table>';

adminFooter($Relative);
?>

