<?php 
session_start();
require_once 'includes/database.php';
require_once 'includes/funcs.php';
if (!isset($_SESSION['reusername'])  || ($_SESSION['reusergroup'] > 1)) { echo 'Sorry You Dont Have permissions to View this Page!'; exit();}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head> <title>Modify banks</title>
        <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.10.4.custom.js"></script>
        <link rel="stylesheet" href="css/allcss.css" type="text/css" />
        <link rel="stylesheet" href="css/jquery-ui-1.10.4.custom.css" type="text/css" />
        <style type="text/css">
            html, body {margin:0; padding:0; height:100%; width:100%; min-width:100%; min-height:100%;}
        </style>
</head>
<body>		
<p class="titletr">Modify Banks <?php echo str_repeat('&nbsp;', 180); ?><a href="#"><img src="images/close.png" onclick="window.close();"/> </a></p> 
<?php
if (isset($_POST['addsavsupps'])) {         //Save suppss table
        $rl = intval($_POST['rl']); $orl = intval($_POST['orl']);  
        $i=1;
		$db=new MySQLDatabase();
		$db->open_connection();
        while ($i < $orl) {
            $bacc = htmlspecialchars($_POST['bacc'.$i]);
			$fid = htmlspecialchars($_POST['fid'.$i]);
            $ts = date('Y-m-d h:i:s');
            $query = "UPDATE banks SET bacc = '$bacc', ts = '$ts' WHERE id = '$fid'";
            $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
            $i++;
        }
        while ($i < $rl) {
            $bacc = htmlspecialchars($_POST['bacc'.$i]);
            $ts = date('Y-m-d h:i:s');
            $query = "INSERT INTO banks (bacc, ts) VALUES ('$bacc', '$ts')";
            $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
            $i++;
        }
        if(!$result) { echo "ERROR in Saving Bank Accounts Table!"; }
        else { echo '<div id="dialog-message" title="Accounts Modification Status">
                        <p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                        Accounts Details Updated!</p></div>';
                }
		$db->close_connection();
}
elseif (isset($_GET['delbk'])) {         //Delete supps Code
	$id = htmlspecialchars($_REQUEST['id']);
	$db=new MySQLDatabase();
	$db->open_connection();
	$query = "SELECT * FROM recptrans WHERE bacc = '$id'";
    $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );  
	$cnt = 0;
	while ($row = mysqli_fetch_assoc($result)) {
	 if(intval($row['amount']) > 0) { $cnt = 1;  break;  }
	}
	$query = "SELECT * FROM bktrans WHERE cacc = '$id' OR dacc = '$id'";
    $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );  
	while ($row = mysqli_fetch_assoc($result)) {
	 if(intval($row['amount']) > 0) { $cnt = 1;  break;  }
	}
	if(!$cnt) {
		$query = "DELETE FROM banks WHERE id = '$id'";
		$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
		if(!$result) { echo "ERROR in Deleting bank!"; }
        else { echo '<div id="dialog-message" title="Bank Modification Status">
                        <p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                        Bank Account Deleted!</p></div>';
                }
	}
	else { echo '<div id="dialog-message" title="Bank Modification Status">
                        <p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                        Bank Account Has Transactions - Account NOT Deleted!</p></div>';
                }	
	$db->close_connection();			
}

else {
   echo '<form id="supslist" method="post" action="modbanks.php" > <center>'; 
   echo '<table cellspacing="10px" align="center" style="width:80%; margin-left:10px;" >'; 
   echo '<tr><td align="center"><input type="text" id="btnaddsupps" name="btnaddsupps" onclick="getsuppsrow();" class="text ui-widget-content ui-corner-all" value="   Add Bank" style="width:100px;font-weight:bold;"/></td></tr>';	 
   echo '</table><br/>';
   
   echo '<table id="tablelist" style="margin-left:10px;">
    <thead>
    <tr> <th align="left" style="width:150px;">Account Name </th> <th align="center" style="width:40px;">ACTION </th> </tr>
    </thead>'; 
	$db=new MySQLDatabase();
	$db->open_connection();
    $query = 'SELECT * FROM banks ORDER BY bacc;';
    $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
    $odd = true; $i=1;
    while ($row = mysqli_fetch_assoc($result)) 
        {
        echo ($odd == true) ? '<tr class = "odd_row">' : '<tr class = "alt">';
        $odd = !$odd;
        echo'<td>';
		echo '<input type="text" id="bacc'.$i.'" name="bacc'.$i.'" value="'.$row['bacc'].'" size="40">';
        echo '</td><td>';
        echo '<a href="modbanks.php?delbk=true&id='.$row['id'].'"> DELETE</a> <input type="hidden" id="fid'.$i.'" name="fid'.$i.'" value="'.$row['id'].'">';
        echo '</td></tr>';
        $i++;
        }   
    echo '<input type="hidden" id="rl" name="rl" value="'.$i.'"><input type="hidden" id="orl" name="orl" value="'.$i.'">';
    echo '<tr id="trow"><br/></tr>';    
    echo '<tr align="center"><td colspan=3><input type="submit" id="addsavsupps" name="addsavsupps"  class="text ui-widget-content ui-corner-all" value=" Save Banks  " style="width:100px;font-weight:bold;"/></td></tr>';
    echo '</table>';
    echo '</center></form>';
	$db->close_connection();
}
?>

</body>

</html>
<script type="text/javascript"> 
$(function() {
	// dialog boxes
    $( "#dialog:ui-dialog" ).dialog( "destroy" );

		$( "#dialog-message" ).dialog({
			modal: true,
			width: 400,
			buttons: {
				Ok: function() {
					window.close();
				}
			}
		});
});

function getsuppsrow(){ 
var rl = $("#rl").val();    
 $('#trow').before('<tr><td><input type="text" id="bacc'+rl+'" name="bacc'+rl+'" value="" size="30"></td></tr>');   
 document.getElementById("rl").value = parseInt(rl)+1;
}; 
</script>


