<?php
include("include/config.php");
include("include/protect.class.php");
include("include/functions.php");
$prot=new protect();
if ($prot->showPage) {
$db=new DB($globalConfig['dbase'], $globalConfig['dbhost'],$globalConfig['dbuser'], $globalConfig['dbpass']);
mysql_query("SET NAMES utf8");
if(isset($_GET['do'])){
	$action = $_GET['do'];
}
switch($action){
//-------------------------------------------------------------------------------------	
	case 'delete_priority':
	if(isset($_GET['confirm'])){
		$db->execute("delete from m_priority where id = '".(int)$_GET['id']."'");
	}
		header("location: m_priority.php");
	break;
    //-------------------------------------------------------------------------------	
	case 'insert_priority':

		if(isset($_POST['Insert'])){
			$db->execute("insert into m_priority set name='".$db->safe_string($_POST['typename'])."'");
	        header("location: m_priority.php?do=");
		}
	break;
	
	case 'edit_priority':
		$db->execute("update  m_priority set name='".$db->safe_string($_POST['typename'])."' where id ='".$_POST['id']."'");
		header("location: m_priority.php");
	break;

}
$page = $_GET['page'];
$limit = $_GET['limit'];
if (!($limit)){
$limit =25;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults = $db->query("select * from m_priority "); // the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.


$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows%$limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
 
else{
$last = $numrows;} // If last results page, last result equals total number of results.


//escape from PHP mode.



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>سیستم جامع مدیریت W200 </title>
<link href="css/dropdown/dropdown.vertical.rtl.css" media="all" rel="stylesheet" type="text/css" />
<link href="css/dropdown/themes/default/default.css" media="all" rel="stylesheet" type="text/css" />
<!--[if lt IE 7]>
<script type="text/javascript" src="js/jquery/jquery.js"></script>
<script type="text/javascript" src="js/jquery/jquery.dropdown.js"></script>
<![endif]-->



<script type="text/javascript" src="js/js_body.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<!--[if IE 5]>
<style>body {text-align: center;}#wrapper {text-align: left;}.menulist a {float: left; clear: both;}</style>
<![endif]-->
<!--[if lte IE 7]>
<style>#wrapper, #footer, #masthead, #column_wrapper {zoom: 1;}</style>
<![endif]-->

<style type="text/css">
.fields{border:#006699 2px dashed; font-size:12px; font-weight:bold; font-family:Verdana, Arial, Helvetica, sans-serif; margin:30px auto; width:600px; text-align:left}
.fields legend{ font-weight:bold; font-size:12px; color:#006699; background-color:#FFFFFF; padding:10px;}
.fields i{font-size:9px; color:#CC0000;}
.fields p{ margin-left:100px; text-align:left; line-height:20px;}
.fields div p{ margin-left:100px; text-align:left; line-height:20px;}
.fields input{margin-bottom:10px;}
table td		{font-size:10px; font-weight:bold; color:#369; padding:4px; font-family:Tahoma, Geneva, sans-serif }
.td_d{ text-align:right; direction:rtl}
th{font-size:12px}
</style>

</head>
<body id="p7bod" onload="P7_Uberlink('p7uberlink','p7bod')">

<div id="wrapper">

  <div id="masthead">
    <div class="content" align="center"><b> سیستم جامع مدیریت W200  </b></div>
  </div>
  
<div id="column_wrapper">
    <div id="left">
      <div class="content">  



<?php
if($_GET['act'] == 'delete'){
			if(!isset($_GET['confirm'])){
					echo 'آيا شما مطمئنيد؟ <br /> <a href="m_priority.php?do=delete_priority&id='.(int)$_GET['GID'].'&confirm"> بلــــه </a> | <a href="m_priority.php"> خيـــــر </a> ';
				}				
}

switch($action){	
		//-------------------------------------------------------------------------------------	
    	default :

		$sql2=$db->query("select * from m_priority where id='".$_GET['id']."'");
		$res2=$db->fetchNextArray($sql2);
		$num=$db->numRows($sql2);
		if($num){
			$act='do=edit_priority';
		}else{
			$act='do=insert_priority';
		}
	?>
        <form action="m_priority.php?<?=$act?>" method="post">
  	 <table  class="table_insert" cellpadding="3" cellspacing="3" width="90%" align="center" >
        <tr>
            <td class="td_d">نام اولویت  :</td><td class="td_d"><input type="text" name="typename" value="<?=$res2['name']?>"  /></td>
        </tr>
         <tr>
            <td colspan="2" align="center">
            <?php
			if(!$num){
              echo '<input type="submit" name="Insert" value="ذخیره" />';
			}else{
				 echo '<input type="submit" name="edit" value="ویرایش" /><input type="hidden" name="id" value="'.$res2['id'].'">';
			}
			?>
              </td>
        </tr>
        </table>
        
        </form>	
<?php


		$sql = $db->query("select * from m_priority ORDER BY id DESC LIMIT $page, $limit");
		echo '<table width="90%"  border="0" align="center" cellpadding="0" cellspacing="0"  class="table1" >';
		echo '<tr><th class="td_header">انواع اولویت ها</th><th class="td_header" align="center"> امکانات </th></tr>';
		while($res = $db->fetchNextArray($sql)){
			echo '<tr><td  align=center class="tablebattom1"> '.$res['name'].' </td>
			 		<td  align=center class="tablebattom1"> <a href="m_priority.php?act=delete&GID='.$res['id'].'"> <img border="0" src="images/dell.gif"> </a><a href="m_priority.php?id='.$res['id'].'"> <img border="0" src="images/edit.gif"> </a> </td></tr>';
		}
		echo '</table>';
		
		
echo '<p align="center" style="margin-top:20px;">';

if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\" class=\"pages\">قبلی</a>    \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
 $ppage = $limit*($i - 1);
 if ($ppage == $page){
 echo("<b class=\"pages_on\">".$i."</b> \n");} // If current page don't give link, just text.
 else{
 echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\" class=\"pages\">".$i."</a> \n");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\" class=\"pages\">بعدی</a>\n");}

echo '</p>';	
		
		
	break;

//-------------------------------------------------------------------------------------	

}
?>
      </div>
    </div>
    
    
    <div id="right">
      <div class="content">
        <h4>منو</h4>
<?php include("include/header.php");?>
      </div>
    </div>
    
    <br class="clearfloat" />
  </div>
  
  <div id="footer"><?php include("include/footer.php");?></div>
  
</div>
</body>
</html>
<?php } ?>