<?php session_start();  //Developed by ITechScripts
$adate=$_SESSION['adate'];
$bdate=$_SESSION['bdate'];
$cdate=$_SESSION['cdate'];
$ddate=$_SESSION['ddate'];

  function getDateDifference2($dateFrom, $dateTo)
     {
     $difference = null;
     $dateFromElements = split(' ', $dateFrom);
     $dateToElements = split(' ', $dateTo);
     $dateFromDateElements = split('-', $dateFromElements[0]);
     $dateFromTimeElements = split(':', $dateFromElements[1]);
     $dateToDateElements = split('-', $dateToElements[0]);
     $dateToTimeElements = split(':', $dateToElements[1]); 
     $date1 = mktime($dateFromTimeElements[0], $dateFromTimeElements[1], $dateFromTimeElements[2], $dateFromDateElements[1], $dateFromDateElements[0], $dateFromDateElements[2]);
     $date2 = mktime($dateToTimeElements[0], $dateToTimeElements[1], $dateToTimeElements[2], $dateToDateElements[1], $dateToDateElements[0], $dateToDateElements[2]); 
     if( $date1 > $date2 )
     {
         return null;
     }
     $diff = $date2 - $date1;

     return $diff;
  }

  function AddDays2($date,$interval) 
{ 
  if (!isset($date)) 
  $date = date("Y-m-d H:i:s"); 
  $first = explode(" ", $date); 

  $elts1 = explode("-", $first[0]);
  $elts2 = explode(":", $first[1]);
  $inter=$interval; 
  $dcour=mktime($elts2[0],$elts2[1],$elts2[2],$elts1[1],$elts1[2],$elts1[0]); 
  $dres=$dcour+$inter; 
  $date1=date("Y-m-d H:i:s",$dres);
  $ret_date="$date1";
  return $ret_date; 
}

?>
<?
require 'include/configurations.php'; // ITECHBIDS Configuration File
require 'include/header.php';
$val=$_GET['val'];
if($val=='')
$val='actv';
$uid=$_POST['uid'];
$amnt=$_POST['amnt'];
$pthru=$_POST['pthru'];
$mode=$_GET['mode'];
$catid=$_GET['id'];
$stats=$_GET['stats'];

if(($mode=="relist") || ($mode=="Active"))
{
$productid=$_GET[productid];
$itemsql="select * from auction_details where productid=$productid";
$itemsqlqry=mysql_query($itemsql);
$itemfetch=mysql_fetch_array($itemsqlqry);
$expire_date=$itemfetch[or_expire_date];
$bid_starting_date2=$itemfetch[bid_starting_date];

$date1 = strtotime($bid_starting_date2);
$date2=strtotime($expire_date);

$date1 = date("d-m-Y H:i:s",$date1);
$date2 = date("d-m-Y H:i:s",$date2); 
$dateFrom=$date1;
$dateTo = $date2;

$diffd = getDateDifference2($dateFrom, $dateTo);
echo "$diffd<BR>";
$bidding_start_date=date("Y-m-d H:i:s");
echo "$bidding_start_date<BR>";
$expire_date = AddDays2($bidding_start_date,$diffd); 
echo "$expire_date";
$relistsql="update auction_details set status='Active', bid_starting_date='$bidding_start_date', expire_date='$expire_date', or_expire_date='$expire_date', quantity_sold=0, ship_stat='unpaid' where productid=$productid";
$relistsqlqry=mysql_query($relistsql);
$bidsql="delete from site_bids where productid=$productid";
$bidsqlqry=mysql_query($bidsql);

$abidsql="delete from autobids where productid=$productid";
$abidsqlqry=mysql_query($abidsql);
}



if($mode=="paid" or $mode=="appr")
{
$productid=$_GET[productid];
$up_sql="update auction_details set status='Active' where productid=$productid";
$up_res=mysql_query($up_sql);
$msg="<b><font size=2 color=red>Updated Successfully</font></b><br>";
}

if($mode=="close")
{
$productid=$_GET[productid];
$datecheck=date("Y-m-d H:i:s");
$up_sql="update auction_details set status='Inactive', expire_date='$datecheck' where productid=$productid";
$up_res=mysql_query($up_sql);

$msg="<b><font size=2 color=red>Updated Successfully</font></b><br>";
}

if($stats!="")
{
$productid=$_GET[productid];
$up_sql="update auction_details set ship_stat='$stats' where productid=$productid";
$up_res=mysql_query($up_sql);
}
$datecheck=date("Y-m-d H:i:s");






if($val=='exp')
$sql="select * from auction_details where status='Closed'";
else if($val=='today')
$sql="select * from auction_details  where (bid_starting_date > '$datecheck')";
else if($val=='actv')
$sql="select * from auction_details  where status='Active'";
else if($val=='appr')
$sql="select * from auction_details  where status='unapproved'";
else if($val=='sold')
$sql="select * from auction_details where quantity_sold > 0";
else if($val=='unpaid')
$sql="select * from auction_details where status='new'";
$res=mysql_query($sql);


if(isset($_POST['search']))
   {

$sell=$_POST['cboSell'];
$sfrom=$_POST['cboAyear']."-".$_POST['cboAmonth']."-".$_POST['cboAday'];
$sto=$_POST['cboByear']."-".$_POST['cboBmonth']."-".$_POST['cboBday'];
$efrom=$_POST['cboCyear']."-".$_POST['cboCmonth']."-".$_POST['cboCday'];
$eto=$_POST['cboDyear']."-".$_POST['cboDmonth']."-".$_POST['cboDday'];
$sql="select * from auction_details  where selling_method=$sell and (bid_starting_date >= '$sfrom') and (bid_starting_date <= '$sto') and (expire_date >= '$efrom') and (expire_date <= '$eto')";
$res=mysql_query($sql);
}


$id=$_POST['id'];
if(isset($_POST['btn_release']))
{
$coid=$_POST['chkSub'];
foreach($coid as $cnid)
{
$masql="delete from auction_details where productid=$cnid";
$mares=mysql_query($masql);
$masql="delete from site_bids where productid=$cnid";
$mares=mysql_query($masql);
$masql="delete from watch_list where productid=$cnid";
$mares=mysql_query($masql);
$masql="delete from featuredproducts where productid=$cnid";
$mares=mysql_query($masql);
$message="Items deleted sucessfully";
}
}






?>
>
<style type="text/css">
<!--
.style1 {
	color: #666666;
	font-weight: bold;
}
.style3 {color: #666666; font-size: 11px; font-family:Arial, Helvetica, sans-serif}
-->
</style>

<table align="center" width="95%" height="35">
 <? require 'include/search.php' ?>

<tr><td>&nbsp;</td></tr>
  
</table>
 <? if($msg)
  {
  ?>
 <table width="100%"> <tr align="center"><td colspan="2"><br> <?=$msg;?> </td></tr></table>
  <?
  $msg="";
  }
  ?>
 
<form name="frm1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> 
  <table align="center" width="80%"> 
    <tr> 
      <td align="center"><a href="auction.php?val=today" id="tablink">Future Items</a></td> 
      <td align="center"><a href="auction.php?val=actv" id="tablink">Live Items</a></td> 
      <td align="center"><a href="auction.php?val=sold" id="tablink">Sold Items</a></td> 
      <td align="center"><a href="auction.php?val=exp" id="tablink">Expired Items</a></td> 
    </tr> 
  </table> 
  <table align="center" width="95%" class="ft"> 
    <tr class="ct" height="25"> 
      <td height="24" colspan="10">
	  <? if($val=='today') echo 'New ';else if($val=='actv') echo 'Active ';else if($val=='sold') echo 'Sold';else if($val=='exp') 																																																																										        echo 'Expired ';?> Items
	  </td> 
    </tr><TR><TD></TD></TR>
    <tr class="ct" height="25"> 
      <td width="4%"><input type="checkbox" name="chkMain" onClick="chkall();" class="check" value=1></td> 
      <td width="23%">Item Name</td> 
	  <td width="23%">
	  <? if($val!="sold") echo "Current Price"; else if($val=="sold") echo "Sold Amount"; ?> </td> 
	  
	  <td width="13%">
	   <? if($val!="sold") echo "Ends"; else if($val=="sold") echo "Buyer"; ?> 
	  </td> 
	  <? if($val=="today" or $val=='actv' or $val=='sold')
	  {
	  ?>
	  <td width="13%">
	  Relist
	  </td> 
	  <?
	}
	  ?>
      <td width="11%"># of Bids</td> 
   <? if($val!="sold"){ ?>
       <td width="21%"># of Clicks</td> 
<? } ?>
   <? if($val=="sold"){ ?>
       <td width="21%">Change Status</td> 
<? } ?>
	   <td width="21%">Status</td> 
	    <td width="21%">Action</td>
   </tr> 
 <?php
 if($catid!="")
 {
 $cat_sql="select * from site_categories where category_head_id=$catid"; 
 $cat_res=mysql_query($cat_sql);
 $tot_rec=mysql_num_rows($cat_res);
 
 $cat="category_id=";
 if($tot_rec>0)
 {
   while($cat_row=mysql_fetch_array($cat_res))
   {
    $cat=$cat.$cat_row[category_id]." or category_id=";
   }
 $cat=rtrim($cat," or category_id=");
 }
 
 else
 {
 $cat="category_id=$cate_id";
 }
 
 $sql="select * from auction_details where ( $cat ) and status='Active'";
 }
 if($adate!='' && $adate!='--')
 $sql.="and  bid_starting_date >='$adate' ";
 if($bdate!='' && $bdate!='--' )
 $sql.="and  bid_starting_date <='$bdate' ";
 
 if($cdate!='' && $cdate!='--')
 $sql.="and  expire_date  >='$cdate'  ";
 if($ddate!='' && $ddate!='--' )
 $sql.=" and expire_date  <='$ddate' ";

$res=mysql_query($sql);	
$tot=mysql_num_rows($res);
$curpage=$_GET['curpage'];
if(strlen($_GET['curpage']) == 0) $curpage=1;
$start=($curpage-1) * 10;
$end=10;
$sql.=" limit $start,$end";
 $res=mysql_query($sql);
?> 
    <tr> 
      <td align="right" colspan="7"> <?php
	if($curpage!=1) {
?> 
        <a href="auction.php?val=<?=$val?>&curpage=<?=($curpage-1);?>" id="link2">Prev</a> |
        <?php
}
?> 
        <?php
if($tot > ($start + $end)) {
?> 
        <a href="auction.php?val=<?=$val?>&curpage=<?=($curpage+1);?>" id="link2">Next</a> 
        <?php
}
?> </td> 
    </tr> 
    <?php
if($tot>0)
{
while($row=mysql_fetch_array($res))
{
?> 
      <tr > 
      <td><input name="chkSub[]" id="chkSub" type="checkbox" class="check" value="<?php echo $row['productid'];  ?>">
	  <input type="hidden" name="id" value="<?=$row['deposit_id']; ?>"></td> 
      <td>
	  	<a href="item_itechds.php?id=<?= $row[productid]?>" id="link3"><?php echo $row['producttitle'];?></a>
	  </td> 

	  <td>
<?  
    echo $row[currency].""; 
	$cur_sql="select MAX(bidding_amount) from site_bids where productid=".$row['productid'];
    $cur_res=mysql_query($cur_sql);
    $cur_row=mysql_fetch_array($cur_res);
	if($val!="sold")
	{
    if(!empty($cur_row[0]))
	echo $cur_row['bidding_amount'];
	else
	echo $row['min_bid_amount'];
	}
	else if($val=="sold")
	{
	$cur_sql1="select * from site_bids where user_pos='Yes' and productid=".$row['productid'];
    $cur_res1=mysql_query($cur_sql1);
    $cur_row1=mysql_fetch_array($cur_res1);
	echo $cur_row1['bidding_amount'];  
	}
	?>
</td>
      <td>
	  <?
	  if($val!="sold")
	 {
	  $expire_date=$row['expire_date'];
	  require 'ends.php'; 
	  echo "$string_difference" ;
	  }
	  else if($val=="sold")
	  {
      $cur_sql="select * from site_bids where  user_pos='yes' and productid=".$row['productid']." order by bidding_amount desc";
      $cur_res=mysql_query($cur_sql);
      $cur_row=mysql_fetch_array($cur_res);
	  $mres=mysql_query("select * from member_details where user_id=".$cur_row['user_id']); 
      $mrow=mysql_fetch_array($mres);
	  ?>
	  <a href="useritechds.php?id=<?= $row[user_id] ?>&type=gen" id="link3">
	  <?
      echo $mrow['user_name'];
	  ?>
	  </a>
	  <?
	  }
	  ?></td> 
	   <? if($val=="today" or $val=='actv' or $val=='sold')
	  {
	  ?>
	  <td width="13%">
	  <a href="auction.php?productid=<?=$row['productid']?>&mode=relist" id="link3">Relist</a>
	  </td> 
	  <?
	 }
	  ?>
      <td>
	  <a href="bid_itechd.php?productid=<?= $row['productid']; ?>" id="link3"><?
	  $bid_sql="select * from site_bids where productid=".$row['productid'];
      $bid_res=mysql_query($bid_sql);
      $bid_row=mysql_fetch_array($bid_res);
      $no_of_bid=mysql_num_rows($bid_res); ?>
	  <?=$no_of_bid; ?> </a></td> 
<? if($val!="sold")
	  {
	  ?> 
     <td width="3%"> <?php
     echo $row['clicks'];
?></td> 
<? } ?>
<? if($val=="sold")
	  {
	  ?> 
     <td width="3%">
<a href="auction.php?curpage=<?=$curpage?>&val=<?=$val?>&productid=<?=$row['productid']?>&stats=unpaid">unpaid</a><BR>
<a href="auction.php?curpage=<?=$curpage?>&val=<?=$val?>&productid=<?=$row['productid']?>&stats=paid">paid</a><BR>
<a href="auction.php?curpage=<?=$curpage?>&val=<?=$val?>&productid=<?=$row['productid']?>&stats=awaiting shipping">awaiting shipping</a><BR>
<a href="auction.php?curpage=<?=$curpage?>&val=<?=$val?>&productid=<?=$row['productid']?>&stats=shipped">shipped</a><BR>
<a href="auction.php?curpage=<?=$curpage?>&val=<?=$val?>&productid=<?=$row['productid']?>&stats=completed">completed</a><BR>
</td> 
<? } ?>
<? if($val!="sold")
	  {
	  ?> 
<td>
     <? 
	  if($row[status]=="Active")
	  {
	  ?>
	  <a href="auction.php?productid=<?= $row['productid']; ?>&mode=close" id="link3"> <?= "Close" ?> </a>
	  <?
	  }
	   else if($row[status]=="Suspend")
	   {
	  ?>
	   <a href="auction.php?productid=<?= $row['productid']; ?>&mode=Active" id="link3"> <?= "Active" ?> </a>
	  <?
	  } 
	   else if($row[status]=="unapproved")
	   {
	  ?>
	   <a href="auction.php?productid=<?= $row['productid']; ?>&mode=appr" id="link3"> <?= "Approve" ?> </a>
	  <?
	  } 
	  else if ($row[status]=="Closed")
	  {
      ?>
	   <a href="auction.php?productid=<?= $row['productid']; ?>&mode=Active" id="link3"> <?= "Active" ?> </a>
	  <?
	  }
	  else if ($row[status]=="new")
	  {
      ?>
	   <a href="auction.php?productid=<?= $row['productid']; ?>&mode=paid" id="link3"> Paid </a>
	  <?
	  }
	  ?>
	  
</td>
<?
}
?>
<? if($val=="sold")
	  {
	  ?> 
<td><?= $row['ship_stat']; ?></td>
<? } ?>
<td><a href="modifyauction.php?productid=<?= $row['productid']; ?>" > Edit</a> </td>
     </tr>
<input type="hidden" name="uid"  value="<?=$row['member_id']; ?>">
<input type="hidden" name="amnt"  value="<?=$row['amount']; ?>">
<input type="hidden" name="pthru"  value="<?=$row['payment_thro']; ?>">
    <?php
}
?> 
    <?php if($val!='actv') { ?> 
    <tr> 
      <td colspan="7"> 
	  <input type="submit" class="button"   name="<?php if($val=='new') echo 'btn_new'; else if($val=='mature') echo 'btn_mature'; else echo 'btn_release'; ?>" value="<?php if($val=='new') echo 'Activate'; else  echo 'Delete';  ?>" ></td> 
    </tr> 
    <?php } ?> 
    <?
}
else 
echo '<tr align=center><td colspan=7><font color=#FF0000>No items Found</font></td></tr>';
?> 
  </table> 
</form> <?php
require 'include/footer.php';
?> 
<script language="JavaScript">
function chkall() {
	len=document.frm1.chkSub.length;
	if(len > 1) {
	for(i=0;i<len;i++) {
		if(document.frm1.chkMain.checked==true) {
			document.frm1.chkSub[i].checked=true;
		}
		else {
			document.frm1.chkSub[i].checked=false;
		}
	}
	}
	else {
		if(document.frm1.chkMain.checked==true) {
			document.frm1.chkSub.checked=true;
		}
		else {
			document.frm1.chkSub.checked=false;
		}
	
	}
	}

function condelete()
{
var confrm;
confrm=window.confirm("Are You sure you want to delete");
return confrm;
}
</script> 
