<?
include_once("./_common.php");

?>
<meta http-equiv="content-type" content="text/html; charset=utf-8">

<?

$chk = 0;
if($book_lend == "" && $book_return == "" ){  ?> <script> alert("대여나 반납할 코드를 넣어주세요");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }

if($book_lend != "" && $person == "" ){  ?> <script> alert("대여할 사람 이름을 넣어주세요");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }

if($book_lend == "" && $person != "" ){  ?> <script> alert("대여할 도서명을 넣어주세요");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }

if($book_lend != "" && $book_return != "" ){  ?> <script> alert("대여나 반납 둘 중 하나만 입력해주세요");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }

$query4 = "select bo_7 from g4_board  ";
	$result4 = sql_query($query4);
	while($row  = sql_fetch_array($result4)) {
	
		$check = $row[bo_7]; 

	}
	$out=preg_replace("/\s+/","",$person);
	$person = trim($out);
	$out=preg_replace("/\s+/","",$book_lend);
	$book_lend = trim($out);

if($book_lend != "" && $chk == 0) {
	$i=0;

	$query5 = "select * from g4_write_books  where  wr_1 = '$book_lend'";
	$result5 = sql_query($query5);
	while($row  = sql_fetch_array($result5)) {
	
		$book_subject = $row[wr_subject]; 
		$book_author = $row[wr_2]; 
		$book_print = $row[wr_3]; 
		$book_status = $row[wr_4]; 
	}




/// 설정을 적용하라
	$query7 = "select * from g4_board where bo_table='lib' ";
	$result7 = sql_query($query7);
	while($row  = sql_fetch_array($result7)) {	
		$normal_period = $row[bo_1]; 
		$normal_books = $row[bo_8]; 
		$normal_delay = $row[bo_2]; 
		$regular_period = $row[bo_3]; 
		$regular_books = $row[bo_9]; 
		$regular_delay = $row[bo_4]; 
		$special_period = $row[bo_5]; 
		$special_books = $row[bo_10]; 
		$special_delay = $row[bo_6]; 
		$search_option = $row[bo_7]; 

	}

if($check == 1){  $a = "wr_1 = '$person'"; $b="코드를";}
else  {$a = "wr_subject = '$person'";$b="이름을";}

if($book_status == "" ){  ?> <script> alert("대여자 검색 설정이 <?=$b?>입니다. <?=$b?> 넣으세요");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }

/// 사람도 찾아라
	$query6 = "select * from g4_write_members where $a ";
	$result6 = sql_query($query6);
	while($row  = sql_fetch_array($result6)) {	
		$member_name = $row[wr_subject]; 
		$level = $row[wr_4]; //회원 레벨
		$books = $row[wr_5]; //대여 권수
		$status = $row[wr_6]; //회원 상태
	}


$query8 = "select * from g4_write_books  where  wr_1 = '$book_lend'";  // lib에  해당 사용자의 wr_5가 1이 있다면 연체로 인한 대여 불가를 위해
	$result8 = sql_query($query8);
	while($row  = sql_fetch_array($result8)) {
	
		$book_subject = $row[wr_subject]; 
		$book_author = $row[wr_2]; 
		$book_print = $row[wr_3]; 
		$book_status = $row[wr_4]; 
	}

if($book_status != "대여가능" ){  ?> <script> alert("현재 도서는 <?=$book_status?> 상태로 대여가 불가능합니다");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }

if($member_name == "" ){  ?> <script> alert("등록되지 않은 회원입니다");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }


if($status != "정상" ){  ?> <script> alert("<?=$member_name?>님은  <?=$status?> 상태로 대여가 불가능합니다");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }

if($level == "일반" && $book_lend != "") { 
	$wr_6 = $normal_period;	//대여 기간 
	$wr_7 = $normal_delay;   //연체료
	if($normal_books < $books + 1) {  ?> <script> alert("<?=$member_name?>님은  <?=$normal_books?>일을 초과하여 대여가 불가능합니다");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }

	if($normal_period < $books + 1) {  ?> <script> alert("<?=$member_name?>님은  <?=$normal_books?>를 초과한 연체 도서가 있어 대여가 불가능합니다");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }
}

if($level == "정" && $book_lend != "") { 
	$wr_6 = $regular_period;	//대여 기간 
	$wr_7 = $regular_delay;   //연체료
	if($regular_books < $books + 1) {  ?> <script> alert("<?=$member_name?>님은  <?=regular_books?>일을 초과하여 대여가 불가능합니다");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }

	if($normal_period < $books + 1) {  ?> <script> alert("<?=$member_name?>님은  <?=$normal_books?>를 초과한 연체 도서가 있어 대여가 불가능합니다");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }
}

if($level == "특별" && $book_lend != "") { 
	$wr_6 = $special_period;	//대여 기간 
	$wr_7 = $special_delay;   //연체료	
	if($special_books < $books + 1) {  ?> <script> alert("<?=$member_name?>님은  <?=$special_books?>일을 초과하여 대여가 불가능합니다");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }

	if($normal_period < $books + 1) {  ?> <script> alert("<?=$member_name?>님은  <?=$normal_books?>를 초과한 연체 도서가 있어 대여가 불가능합니다");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }
}

}



$period = date("Y-m-d");
if($book_return != "" && $chk == 0) {
	/// 사람도 찾아라
	$query6 = "select wr_3 from g4_write_lib where wr_1 = '$book_return' and wr_4 = '대여중'  ";
	$result6 = sql_query($query6);
	while($row  = sql_fetch_array($result6)) {	
		$member_name = $row[wr_3]; 
	}
	
	if($member_name == "" ){  ?> <script> alert("대여된 책이 아닙니다");   document.location.href="<?=$g4[path]?>/bbs/board.php?bo_table=<?=$bo_table?>" </script>  <?  $chk = 1; }

	if( $chk == 0) {
	$ReturnBook = $books - 1;
	  sql_query("update g4_write_members set wr_5 = '$ReturnBook' where wr_subject = '$member_name'");
	  sql_query("update g4_write_lib set wr_4 = '반납' where wr_1 = '$book_return'");
	  sql_query("update g4_write_books set wr_4 = '대여가능' where wr_1 = '$book_return'");
	  sql_query("update g4_write_lib set wr_2 = '$period' where wr_1 = '$book_return'");
	  $chk = 1;
	}
}





if($chk != 1) {
	$HowManyBooks = $books + 1;
	  sql_query("update g4_write_members set wr_5 = '$HowManyBooks' where $a");
	  sql_query("update g4_write_books set wr_4 = '대여중' where wr_1 = '$book_lend'");
	 // sql_query("update g4_write_lib set wr_4 = '대여중' where wr_1 = '$book_lend'");

$table = "g4_write_lib";
 $wr_num = get_next_num($table);

   $sql = " insert into $table
                set wr_num = '$wr_num',
                    wr_reply = '$wr_reply',
                    wr_comment = 0,
                    ca_name = '$ca_name',
                    wr_option = '$html,$secret,$mail',
                    wr_subject = '$book_subject',
                    wr_content = '$wr_content',
                    wr_link1 = '$wr_link1',
                    wr_link2 = '$wr_link2',
                    wr_link1_hit = 0,
                    wr_link2_hit = 0,
                    wr_trackback = '$wr_trackback',
                    wr_hit = 0,
                    wr_good = '$wr_good',
                    wr_nogood = 0,
                    mb_id = '$member[mb_id]',
                    wr_password = '$wr_password',
                    wr_name = ' $wr_name',
                    wr_email = '$wr_email',
                    wr_homepage = '$wr_homepage',
                    wr_datetime = '$g4[time_ymdhis]',
                    wr_last = '$g4[time_ymdhis]',
                    wr_ip = '$_SERVER[REMOTE_ADDR]',
                    wr_1 = '$book_lend',
                    wr_2 = '$wr_2',
                    wr_3 = '$person',
                    wr_4 = '대여중',
                    wr_5 = '0',
                    wr_6 = '$wr_6',
                    wr_7 = '$wr_7',
                    wr_8 = '$wr_8',
                    wr_9 = '$wr_9',
                    wr_10 = 'wr_10' ";

    sql_query($sql);

    $wr_id = mysql_insert_id();

    // 부모 아이디에 UPDATE
    sql_query(" update $table set wr_parent = '$wr_id' where wr_id = '$wr_id' ");

    // 새글 INSERT
    //sql_query(" insert into $g4[board_new_table] ( bo_table, wr_id, wr_parent, bn_datetime ) values ( '$bo_table', '$wr_id', '$wr_id', '$g4[time_ymdhis]' ) ");
    sql_query(" insert into $g4[board_new_table] ( bo_table, wr_id, wr_parent, bn_datetime, mb_id ) values ( '$bo_table',  '$wr_id', '$wr_id', '$g4[time_ymdhis]', '$member[mb_id]' ) ");

    // 게시글 1 증가
    sql_query("update $g4[board_table] set bo_count_write = bo_count_write + 1 where bo_table = '$bo_table'");
}

?>

<meta http-equiv="refresh" content="0; url=<?=$g4[path]?>/bbs/board.php?bo_table=lib">