<?php
include("include/config.php");
include("include/protect.class.php");
include("include/jdf.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");
?>

<!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>مدلها </title>
<link rel="stylesheet" type="text/css" href="fancybox/jquery.fancybox-1.3.1.css" media="screen" />
<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript" src="fancybox/jquery.fancybox-1.3.1.js"></script>
<?php include("maintemplate/mainheader.php");?>
<script language="JavaScript" src="maintemplate/js/gen_validatorv4.js" type="text/javascript" xml:space="preserve"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<script type="text/javascript" charset="utf-8">

	function getkind (){
		var kind = $("#kind").val();
		
		$(document).ready(function (){
			
			
			$("#loading").fadeIn("slow",function(){
});
			$.ajax({
				type: "POST",
				url: "send_ajax_chitasoft.php?act=brands",
				dataType: "html",
				data: "kind="+kind,
				success: function(data){
					$("#brands").html(data);
					 $("#loading").fadeOut("slow");
				}
			
			});
 
        
       	
    
		});
	}
	
</script>
</head>
<body>
<div id="wpwrap">
<div id="wpcontent">
	<?php include("include/menutop.php");?> 
<div id="wpbody">
	<?php include("include/header.php");?>
    <div style="overflow:hidden;" id="wpbody-content">
        <div id="screen-meta">
            <div class="wrap">
                <div id="icon-index" class="icon32"><br></div>
                <h2>مدلها</h2>
               
                <div id="dashboard-widgets-wrap">
                    <div id="dashboard-widgets" class="metabox-holder">
                     <div class="postbox-container" style="width:98%;">
                                <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">
                                   <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span>  جست و جو</span></h3>
                             <div align="center" style="padding:5px;">
                                               
<form action="models.php?search" method="post" name="form2" enctype="multipart/form-data">
<div style="float:right">
<tr class="even" valign="top">
<th scope="row"><label for="name">نام مدل</label></th>
<td><input name="name"  id="name"  size="45" value="<?php echo $_POST['name']?>" dir="ltr" class="regular-text" type="text">

</td>
</tr>
</div>
<input name="search" class="button-primary" value="جست و جو" type="submit"> 
</form>    
                                                </div>

                              </div>
                            </div>
                        </div>
                        <?php if ($_POST['search']){?>
                        <div class="postbox-container" style="width:98%;">
                                <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">
                                   <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span> لیست نتایج جست و جو</span></h3>
                             <div align="center" style="padding:5px;">
                                               
<table id="rounded-corner"dir="rtl">

  <thead>
			<tr>
            	<th scope="col" id="rounded-q5"></th>
                <th scope="col" id="rounded-q5">نام</th>
				<th scope="col" id="rounded-q5">زمان افزودن</th>
                <th scope="col" id="rounded-q5">ویرایش</th>
                <th scope="col" id="rounded-q5"> حذف</th>
			</tr>
		</thead>
		<tbody>
<?php	

if($_POST['name'] !='' and isset($_POST['name'])){
$name = $_POST['name'];
}else{
	$name = 'asdkalsdkasdalds;asda;sda;sd;';
	}
if($_POST['code'] !='' and isset($_POST['code'])){
$code = $_POST['code']; }

	    $e_product = $db->query("select * from models where name like '%".trim($name)."%' limit 50");
		while($row= $db->fetchNextArray($e_product))
		{
				if($data['visited'] == 0){
			$sytle = 'style="color:#390"';
		}else{
			$style = '';
		}
		if(($i % 2) == 0) {
			$class = 'tableRow2';
		}else{
			$class = 'tableRow1';
		}
			echo '<tr class="'.$class.'" onmouseover="this.className=\'tableRowOver\'" onmouseout="this.className=\''.$class.'\'">';
			
?>
					
<form action="convert">
        <td width="20"><?php echo $row['id'];?></td>  
        <td ><?php echo $row['name'];?></td>  
        <td ><?php $sql1 = $db->query("select * from brands where id = '".(int)$row['brands']."'");
			$res1 = $db->fetchNextArray($sql1);
			echo $res1['name'];
			?></td > 
        <td width="60"><a href="models.php?AID=<?php echo $row['id'];?>"><img src="images/pdo.png"></a></td>  
        <td width="60"><a href="models.php?del=true&AID=<?php echo $row['id'];?>"><img src="images/cros.gif" ></a></td>
			<?php }
	?>
</form>
    		
		</tbody>
  </table>
                   
                                                </div>

                              </div>
                            </div>
                        </div>
                        <?php } 
                                           
	if(isset($_GET['del'])){
		if(isset($_GET['AID']) && ($_GET['AID'] != '')){
			
		$db->execute("delete from models where id = '".(int)$_GET['AID']."'");

		
		echo debug_message('مدل با موفقیت حذف شد','2','98');
		
		echo '<form action="models.php" method="POST">
		<p align="center"><input type="submit" id="button" value="بازگشت"></p>
		</form>';	
		}
}
?>
                     <?php 
                if(isset($_GET['insert'])){
							
					
		$db->execute("insert models set
		 name = '".$db->safe_string($_POST['name'])."',
		 brands = '".$db->safe_string($_POST['brands'])."',
		 review='".addslashes($_POST['review'])."',
		 kind = '".$db->safe_string($_POST['kind'])."',
		 date = NOW()
		 ");
			
		echo debug_message('مدل جدید با موفقیت اضافه شد...','2','98');
		
		echo '<form action="models.php" method="POST">
		<p align="center"><input type="submit" id="button" value="بازگشت"></p>
		</form>';	
		
		}elseif(isset($_GET['update'])){
			
			 $db->execute("update models set
				 name = '".$db->safe_string($_POST['name'])."',
		 brands = '".$db->safe_string($_POST['brands'])."',
		 review='".addslashes($_POST['review'])."',
		 kind = '".$db->safe_string($_POST['kind'])."'
		 where id='".$_GET['AID']."'
		 ");

			echo debug_message('مدل مورد نظر با موفقیت ویرایش شد...','2','98');
		
		echo '<form action="models.php" method="POST">
		<p align="center"><input type="submit" id="button" value="بازگشت"></p>
		</form>';
		}else{?>
                         <div class="postbox-container" style="width:68%;">
                                <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">
                                   <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span>  اضافه کردن مدل </span></h3>
                                <div style="padding:10px; line-height:24px">

<?php
	

	
			$form_action = 'insert';
		if(isset($_GET['AID'])){
			$sql = $db->query("select * from models where id = '".(int)$_GET['AID']."'");
			$res = $db->fetchNextArray($sql);
			$form_action = 'update&AID='.$_GET['AID'];
		}
?>
<div class="inside" style="">
<form action="models.php?do=add_models&<?php echo $form_action; ?>" method="post" name="form1" id="form1" enctype="multipart/form-data">
<table class="form-table">
<tbody>
<tr class="even" valign="top">
<th scope="row"><label for="name">نام مدل</label></th>
<td><input name="name"  id="name" size="16" value="<?php echo $res['name']; ?>" dir="ltr" class="regular-text" type="text">
<div id='form1_name_errorloc' class="error_strings"></div>
</td>
</tr>
<tr class="odd" valign="top">
<th scope="row"><label for="kind">مربوط به</label></th>
<td><select id="kind" name="kind"  onchange="getkind();">
	
	<option  value="1" <?php if ($res['kind']=="1"){echo 'selected="selected"';}?>>خودروهای خاص</option>
	<option  value="2" <?php if ($res['kind']=="2"){echo 'selected="selected"';}?>>لوازم جانبی</option>
   	<option  value="3" <?php if ($res['kind']=="3"){echo 'selected="selected"';}?>>خورو سواری</option>
    <option  value="4" <?php if ($res['kind']=="4"){echo 'selected="selected"';}?>>موتور سیکلت</option>
    <option  value="5" <?php if ($res['kind']=="5"){echo 'selected="selected"';}?>>فرسوده و تصادفی</option>
    <option  value="6" <?php if ($res['kind']=="6"){echo 'selected="selected"';}?>>کارتینگ</option>
    <option  value="7" <?php if ($res['kind']=="7"){echo 'selected="selected"';}?>>کنترل سوختی</option>
    <option  value="8" <?php if ($res['kind']=="8"){echo 'selected="selected"';}?>>دیگر موارد</option>
    <option  value="9" <?php if ($res['kind']=="9"){echo 'selected="selected"';}?>>فروشگاه</option>
</select>
<div id='form1_kind_errorloc' class="error_strings"></div>
</td>
</tr>
<tr class="odd" valign="top">
<th scope="row"><label for="brands">برند</label></th>
<td><div style="float:right;">
						<select name="brands" id="brands"  style="width: 200px;">
						<option selected="selected" value="<?=$res['brands']?>"> <?php $sql2=$db->query("select * from brands where id='".$res['brands']."'");
																					$res2=$db->fetchNextArray($sql2);
																					echo $res2['name'];?></option>
						</select>
                        </div>
                        <div style="float:right;"><div id="loading" style="display:none; line-height:20px;"><img width="20" height="20" src="images/loading.gif" />در حال بارگزاری...</div> </div>
<div id='form1_operator_errorloc' class="error_strings"></div>
</td>
</tr>
<tr class="even" valign="top">
<th scope="row"><label for="review">نظر کارشناسی</label></th>
<td>
   <textarea cols="80" id="review" name="review" rows="10"><?php echo stripslashes($res['review']);?></textarea>
			<script type="text/javascript">
			//<![CDATA[

				CKEDITOR.replace( 'review',
					{
						fullPage : false,
						extraPlugins : 'docprops'
					});

			//]]>
			</script>                      
</td>
</tr>
</tbody></table>


<p class="submit">
<input name="process" class="button-primary" value="ذخیره" type="submit">
</p>
</form>
<script language="JavaScript" type="text/javascript"
    xml:space="preserve">//<![CDATA[
//You should create the validator only after the definition of the HTML form
  var frmvalidator  = new Validator("form1");
    frmvalidator.EnableOnPageErrorDisplay();
    frmvalidator.EnableMsgsTogether();


	frmvalidator.addValidation("name","req","باید حتما عدد وارد کنید");

	
	frmvalidator.addValidation("password","numeric","باید حتما عدد وارد کنید");
	frmvalidator.addValidation("password","req","وارد کردن این فیلد ضروری است");
	
	frmvalidator.addValidation("operator","numeric","باید حتما وارد کنید");
	
	frmvalidator.addValidation("kind","numeric","باید حتما وارد کنید");

//]]></script>
</div>
                                
                                </div>

                              </div>
                            </div>
                        </div>
                          
                        <div style="width:29%;" class="postbox-container">
<div class="postbox" id="dashboard_recent_drafts">
<div title="برای جانشینی کلیک کنید" class="handlediv"><br></div><h3 class="hndle"><span></span>راهنما</h3>
<div class="inside">
برای اضافه کردن مدل جدید از ستون سمت راست استفاده کنید و برای ویرایش آن روی گزینه ویرایش جلوی آن کلیک کنید
</div>

</div>

</div>
						<div style="width:29%;" class="postbox-container">
<div class="postbox" id="dashboard_recent_drafts">
<div title="برای جانشینی کلیک کنید" class="handlediv"><br></div><h3 class="hndle"><span></span>اضافه کردن مدل جدید</h3>

<div class="inside">
<a href="models.php"><div class="mybutton" style="width:310px;">اضافه کردن مدل جدید</div></a>
</div>

</div>

</div>
						
<?php
		}?>
        <div class="postbox-container" style="width:98%;">
                                <div id="normal-sortables" class="meta-box-sortables ui-sortable"><div id="dashboard_recent_comments" class="postbox ">
                                   <div class="handlediv" title="برای جانشینی کلیک کنید"><br></div><h3 class="hndle"><span>  لیست مدلها </span></h3>
                             <div align="center" style="padding:5px;">
                                               
<table id="rounded-corner"dir="rtl">

  <thead>
			<tr>
            <th scope="col" id="rounded-q5"></th>
				<th scope="col" >نام</th>
                <th scope="col" id="rounded-q5">نوع</th>
                <th scope="col" id="rounded-q5">ویرایش</th>
                <th scope="col" id="rounded-q5"> حذف</th>
			</tr>
		</thead>
		<tbody>
<?php
	/*
		Place code to connect to your DB here.
	*/
	// include your code to connect to DB.

	$tbl_name="models";		//your table name
	// How many adjacent pages should be shown on each side?
	$adjacents = 3;
	
	/* 
	   First get total number of rows in data table. 
	   If you have a WHERE clause in your query, make sure you mirror it here.
	*/

	$query = "SELECT COUNT(*) as num FROM $tbl_name";
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	
	/* Setup vars for query. */
	$targetpage = "models.php"; 	//your file name  (the name of this file)
	$limit = 20; 								//how many items to show per page
	$page = $_GET['page'];
	if($page) 
		$start = ($page - 1) * $limit; 			//first item to display on this page
	else
		$start = 0;								//if no page var is given, set start to 0
	
	/* Get data. */
	
	$sql = "SELECT * FROM $tbl_name ORDER BY id DESC LIMIT $start, $limit";	
	$result = mysql_query($sql);
	
	/* Setup page vars for display. */
	if ($page == 0) $page = 1;					//if no page var is given, default to 1.
	$prev = $page - 1;							//قبلی page is page - 1
	$next = $page + 1;							//بعدی page is page + 1
	$lastpage = ceil($total_pages/$limit);		//lastpage is = total pages / items per page, rounded up.
	$lpm1 = $lastpage - 1;						//last page minus 1
	
	/* 
		Now we apply our rules and draw the pagination object. 
		We're actually saving the code to a variable in case we want to draw it more than once.
	*/
	$pagination = "";
	if($lastpage > 1)
	{	
		$pagination .= "<div class=\"pagination\">";
		//قبلی button
		if ($page > 1) 
			$pagination.= "<a href=\"$targetpage?page=$prev\">« قبلی</a>";
		else
			$pagination.= "<span class=\"disabled\">« قبلی</span>";	
		
		//pages	
		if ($lastpage < 7 + ($adjacents * 2))	//not enough pages to bother breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page)
					$pagination.= "<span class=\"current\">$counter</span>";
				else
					$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
			}
		}
		elseif($lastpage > 5 + ($adjacents * 2))	//enough pages to hide some
		{
			//close to beginning; only hide later pages
			if($page < 1 + ($adjacents * 2))		
			{
				for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
			}
			//in middle; hide some front and some back
			elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
			{
				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
				$pagination.= "...";
				$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
				$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";		
			}
			//close to end; only hide early pages
			else
			{
				$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
				$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
				$pagination.= "...";
				for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page)
						$pagination.= "<span class=\"current\">$counter</span>";
					else
						$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";					
				}
			}
		}
		
		//بعدی button
		if ($page < $counter - 1) 
			$pagination.= "<a href=\"$targetpage?page=$next\">بعدی »</a>";
		else
			$pagination.= "<span class=\"disabled\">بعدی »</span>";
		$pagination.= "</div>\n";		
	}
?>

	<?php
		while($row = mysql_fetch_array($result))
		{
				if($data['visited'] == 0){
			$sytle = 'style="color:#390"';
		}else{
			$style = '';
		}
		if(($i % 2) == 0) {
			$class = 'tableRow2';
		}else{
			$class = 'tableRow1';
		}
			echo '<tr class="'.$class.'" onmouseover="this.className=\'tableRowOver\'" onmouseout="this.className=\''.$class.'\'">';
			
?>
					
<form action="convert">
        <td width="20"><?php echo $row['id'];?></td>  
        <td ><?php echo $row['name'];?></td>  
        <td ><?php $sql1 = $db->query("select * from brands where id = '".(int)$row['brands']."'");
			$res1 = $db->fetchNextArray($sql1);
			echo $res1['name'];
			?></td > 
        <td width="60"><a href="models.php?AID=<?php echo $row['id'];?>"><img src="images/pdo.png"></a></td>  
        <td width="60"><a href="models.php?del=true&AID=<?php echo $row['id'];?>"><img src="images/cros.gif" ></a></td>
			<?php }
	?>
</form>
    		
		</tbody>
  </table>
  <div style="direction:ltr">
<?=$pagination?>
                             </div>                 
                                                </div>

                              </div>
                            </div>
                        </div>



                    </div>
                    <div class="clear"></div>
                </div><!-- dashboard-widgets-wrap -->
            </div><!-- wrap -->
			<div class="clear"></div></div><!-- wpbody-content -->
			<div class="clear"></div></div><!-- wpbody -->
			<div class="clear"></div></div><!-- wpcontent -->
        </div>
    </div><!-- wpcontent -->
</div><!-- wpwrap -->
<?php include("include/footer.php");?>
</div>
</div>
</body>
</html><?php } ?>