<?php session_start();?>
<?php
include ('dbf.php');
if($_POST[ins])
{

    $valu=split(",",$_POST[ins]);
    if($_POST[neew]=='1')
		$kind='1';
	else
	    $kind='0';
	if($valu['0']=='')
		$ido=$valu['1'];
	else
	    $ido=$valu['0'];
	if($valu['2']=='')
		$idb=$valu['3'];
	else
	    $idb=$valu['2'];
/*
elseif(($valu['2']=="") && ($valu['3']==""))
    	{ ?>
		<script language="javascript">
		window.alert('به علت نکردن کدکتاب از ثبت آن معذوریم');
		</script>
	<?php	}
*/


/*
elseif(($valu['0']=="") && ($valu['1']==""))
    	{ ?>
		<script language="javascript">
		window.alert('بعلت وارد نکردن کد عضو از ثبت آن معذوریم');
		</script>
	<?php	}
*/
	$fg=mysql_query("select * from `ketab` where (id='{$idb}')");
	$a=mysql_fetch_assoc($fg);
	$fg1=mysql_query("select * from `rezerv` where (id_b ='{$idb}')");
	$cd=mysql_num_rows($fg1);
	$da=$valu['4'];
	$f1g=mysql_query("select * from `set_h_t`");
	$g1=mysql_fetch_assoc($f1g);
	include('date1.php');
	$date_sar=datet($da,$g1['day']);
	$fg=mysql_query("select * from `ozv` where (id='{$ido}')");
	$e=mysql_fetch_assoc($fg);
	$date_in=0;
	$mz=mysql_query("select * from `amanat` where (id_b='{$idb}' and date_in=0 )");
	$fmz=mysql_fetch_assoc($mz);
    if($fmz!="")
     {
        echo '<span class="blacktext">اين كتاب امانت است</span>';
	 }
	 else
	 {
	  if($cd=='0')
	    $suq=mysql_query("insert into  amanat (id_ozv,fname_ozv,lname_ozv,id_b,name_b,date_out,date_in,date_sar,kind,user,city)
	   VALUES ('{$valu[0]}','{$e['fname']}','{$e['lname']}','{$a['id']}','{$a['name']}',
	   '{$valu['4']}','$date_in','{$date_sar}','{$kind}','{$_SESSION['user']}','{$_SESSION['city']}')");
	 if($suq)
      echo 'success';
	 else
	   echo '<span class="blacktext">اين كتاب رزرو شده است</span>';
	 }
		//}
}
if (isset($_POST['del']))
{
	mysql_query("delete from amanat where id='{$_POST['id']}'");
}
///////////////bazgasht ketab

if ($_POST[ins1])
{
    $valu=split(",",$_POST[ins1]);
	$fg=mysql_query("select * from `amanat` where (id='{$valu[0]}')");
	$e=mysql_fetch_assoc($fg);
	mysql_query("update amanat set date_in='{$valu[1]}' where (id='{$valu[0]}')");
    if($_POST[neew]=="1")
		mysql_query("insert into `takhir_am`(id_ozv,fname_ozv,lname_ozv,id_b,name_b,date_out,date_in,kind,day_takhir,haz_takhir,pardakht,user,city) values('{$e[id_ozv]}','{$e[fname_ozv]}','{$e[lname_ozv]}','{$e[id_b]}','{$e[name_b]}','{$e[date_out]}','{$valu[1]}','{$e[kind]}','{$valu[2]}','{$valu[3]}','{$_POST[neew]}','{$_SESSION['user']}','{$e[city]}')");
    $_SESSION[selpage]="requirpage.php";
    include ('login.php');
}
//////reserv ketab
if ($_POST[ins2])
{
  $valu=split(",",$_POST[ins2]);
  include_once('jdf.php');
  $date1=jdate('Y/m/d');
  if(($valu['0']=='') && ($valu['1']!=''))
	$id=$valu['1'];
  elseif(($valu['0']!='')&&($valu['1']==''))
	$id=$valu['0'];
  elseif(($valu['0']!='')&&($valu['1']!=''))
	$id=$valu['1'];
  $t=mysql_query("select * from `ozv` where (id='{$id}')");
  $ft=mysql_fetch_assoc($t);
  if($ft['lname']!="")
	{
	  $m=mysql_query("select * from ketab where id='{$valu[2]}'");
	  $fm=mysql_fetch_assoc($m);
	  $lk=mysql_query("select * from `rezerv` where id_b='{$fm[id]}'  ");
	  $flk=mysql_fetch_assoc($lk);
	  if($flk!="")
	  {
		mysql_query("update `rezerv` set name_b='{$fm[name]}',id_b='{$fm[id]}',fname_ozv='{$ft[fname]}',lname_ozv='{$ft[lname]}',id_ozv='{$ft[id]}',date='{$date1}',user='{$_SESSION['user']}',city='{$ft[city]}' where id='{$flk[id]}'");
	  }
	  else
	  {
		mysql_query("insert into `rezerv`(name_b,id_b,fname_ozv,lname_ozv,id_ozv,date,user,city) values('{$fm[name]}','{$fm[id]}','{$ft[fname]}','{$ft[lname]}','{$ft[id]}','{$date1}','{$_SESSION['user']}','{$_SESSION['city']}')");
	  }
	}
	$_SESSION[selpage]="requirpage.php";
   include ('login.php');
}
/////tamdid ketab
if ($_POST[ins4])
{
    $valu=split(",",$_POST[ins4]);
	if(($valu['0']==$valu['1']))
	{
	  $da=$valu['1'];
	  $dj=mysql_query("select * from amanat where (id='{$valu['2']}')");
	  $djf=mysql_fetch_assoc($dj);
	  if($djf['date_tam']!="")
	  {
	    echo "<span class='blacktext'>این کتاب قبلا یکبار تمدید شده است از تمدید مجدد معذوریم</span>";
	  }
	  else
	  {
	    include_once('date.php');
	    include_once('jdf.php');
        $date_tam=jdate('Y/m/d');
	    $suq=mysql_query("update amanat set date_tam='{$date_tam}',date_sar='{$date_in1}' where (id='{$valu['2']}')");
	    if($suq)
           echo 'success';
		$_SESSION[selpage]="requirpage.php";
        include ('login.php');
		//echo "<span class='blacktext'>تمديد با موفقيت انجام شد</span>";
	  }
	}
	else
	{
      echo "<span class='blacktext'>به علت تاخیر در امانت از تمدید معذوریم</span>";

    }

}
?>