<?
include "header.php";
if(isset($_POST['task'])) { $task = $_POST['task']; } elseif(isset($_GET['task'])) { $task = $_GET['task']; } else { $task = "main"; }
$plans = mysql_query("SELECT * FROM ims_plans");
$activated_plans = mysql_query("SELECT * FROM ims_plans WHERE status = 'on'");


if($task == "update_upgrade") {
$plan_upgrade = $_POST['plan_upgrade'];
mysql_query("UPDATE ims_admin SET plan_upgrade='$plan_upgrade'");
$admin_info = mysql_fetch_assoc(mysql_query("SELECT * FROM ims_admin"));
}


echo $header;
echo "
<b>Pricing Plans Manager</b><br>
Use this page to create and manage your plans. Click on a plan's name to view its particulars.<br>
Plans cannot be modified once they are created, nor can they be deleted if they still have users in them.<br>
Plan titles will be displayed in a select box on your signup page. Plan titles and descriptions are<br> both
displayed on your users' upgrade page. Because of this, we recommend that you include the<br> monthly price
and basic account info in your plan title.
<br>
";

if(mysql_num_rows($plans) == 0) { 
echo "<br><font color='#FF0000'><b>WARNING: You currently have zero pricing plans. Users cannot signup to your IMS!</b></font><br>";
$planerror = "yes";
} elseif(mysql_num_rows($activated_plans) == 0) {
echo "<br><font color='#FF0000'><b>WARNING: All your plans are set to OFF. Users cannot signup to your IMS!</b></font><br>";
}

echo "
<br>
<table cellpadding='0' cellspacing='0'>
<form action='plans.php' method='POST'>
<tr>
<td>
<input type='checkbox' name='plan_upgrade' value='yes'"; if($admin_info[plan_upgrade] == "yes") { echo " CHECKED"; } echo "> Allow users to automatically upgrade their account?<br>
</td><td>&nbsp;&nbsp;
<input type='submit' class='button' value='Update'>
<input type='hidden' name='task' value='update_upgrade'>
</tr>
</tr>
</form>
</table>

<br>

<table width='517' cellspacing='0' cellpadding='0' style='border: 1px solid #AAAAAA; background: #FFFFFF;'>
";
if($admin_info[demo_feature] == "on") {
$custom_users = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE plan='0' AND email <> '$admin_info[demo_email]' AND is_approved='yes'"));
} else {
$custom_users = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE plan='0' AND is_approved = 'yes'"));
}

$plancount = 0;
while($plan_info = mysql_fetch_assoc($plans)) { 
$users = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE plan='$plan_info[p_id]' AND is_approved='yes'"));
$pend_users = mysql_num_rows(mysql_query("SELECT * FROM ims WHERE plan='$plan_info[p_id]' AND is_approved <> 'yes'"));
if($plan_info[status] == "on") { $status = "ON"; } else { $status = "OFF"; }
$plancount++;
echo "
<tr>
<td style='padding: 5px;"; if($plancount > 1) { echo " border-top: 1px solid #AAAAAA;"; } echo "'>
<a href='#' onclick='getplan($plan_info[p_id])'><b>$plan_info[plan]</a></b>&nbsp; (Users: $users &nbsp;-&nbsp;"; if($admin_info[approve_deny] == "yes") { echo " Pending Users: $pend_users &nbsp;-&nbsp;"; } echo " Status: $status)

<table width='500' cellpadding='0' cellspacing='0'>
<tr><td style='padding-top: 2px;'>$plan_info[plan_description]</td></tr>
</table>

</td>
</tr>
";
}

if($custom_users == 1) { 
$customblurb = "There is 1 user currently on a custom plan."; 
} else {
$customblurb = "There are $custom_users users currently on custom plans."; 
}

echo "
<tr>
<td style='padding: 5px;'"; if($planerror != "yes") { echo " style='border-top: 1px solid #AAAAAA;'"; } echo "><b>Custom Plans:</b> $customblurb</td>
</tr>
</table>
<br>
<table cellpadding='0' cellspacing='0' width='519'>
<tr>
<td NOWRAP valign='bottom'>
<input class='button' type='button' value='Create New Plan' onclick='addplan()'>
</td>
</table>";
echo $footer;
?>