<?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 Suppliers</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 Suppliers <?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) {
            $sname = htmlspecialchars($_POST['sname'.$i]);
            $phone = htmlspecialchars($_POST['phone'.$i]);
			$email = htmlspecialchars($_POST['email'.$i]);
			$fid = htmlspecialchars($_POST['fid'.$i]);
			$address = htmlspecialchars($_POST['address'.$i]);
            $ts = date('Y-m-d h:i:s');
            $query = "UPDATE suppliers SET sname = '$sname', phone = '$phone', address = '$address', email = '$email', ts = '$ts' WHERE id = '$fid'";
            $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
            $i++;
        }
        while ($i < $rl) {
            $sname = htmlspecialchars($_POST['sname'.$i]);
			$phone = htmlspecialchars($_POST['phone'.$i]);
			$email = htmlspecialchars($_POST['email'.$i]);
			$address = htmlspecialchars($_POST['address'.$i]);
            $ts = date('Y-m-d h:i:s');
            $query = "INSERT INTO suppliers (sname, address, phone, email, ts) VALUES ('$sname', '$address', '$phone', '$email' ,'$ts')";
            $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
            $i++;
        }
        if(!$result) { echo "ERROR in Saving Provider 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['delsupps'])) {         //Delete supps Code
	$id = htmlspecialchars($_REQUEST['id']);
	$sname = htmlspecialchars($_REQUEST['sname']);
	$db=new MySQLDatabase();
	$db->open_connection();
	$query = "SELECT * FROM insrecs WHERE supp = '$id'";
    $result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );  
	$cnt = 0;
	while ($row = mysqli_fetch_assoc($result)) {
	 if(intval($row['tot']) > 0) { $cnt = 1;  break;  }
	}
	if(!$cnt) {
		$query = "DELETE FROM suppliers WHERE id = '$id'";
		$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) ); 
		if(!$result) { echo "ERROR in Deleting Supplier!"; }
        else { echo '<div id="dialog-message" title="Supplier Modification Status">
                        <p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                        Supplier Account Deleted!</p></div>';
                }
	}
	else { echo '<div id="dialog-message" title="Supplier Modification Status">
                        <p><span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span>
                        Supplier Account Has Transactions - Account NOT Deleted!</p></div>';
                }	
	$db->close_connection();			
}

else {
   echo '<form id="supslist" method="post" action="modsups.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 Provider" style="width:130px;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="left"style="width:150px;">Address </th> <th align="left" style="width:100px;">Contacts</th> <th align="left" style="width:150px;">Email </th> <th align="center" style="width:40px;">ACTION </th> </tr>
    </thead>'; 
	$db=new MySQLDatabase();
	$db->open_connection();
    $query = 'SELECT * FROM suppliers ORDER BY sname;';
    $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="sname'.$i.'" name="sname'.$i.'" value="'.$row['sname'].'" size="30">';
        echo '</td><td>';
        echo '<input type="text" id="address'.$i.'" name="address'.$i.'" value="'.$row['address'].'" size="30">';
        echo '</td><td>';
		echo '<input type="text" id="phone'.$i.'" name="phone'.$i.'" value="'.$row['phone'].'" size="12">';
        echo '</td><td>';
		echo '<input type="text" id="email'.$i.'" name="email'.$i.'" value="'.$row['email'].'" size="20">';
        echo '</td><td>';
        echo '<a href="modsups.php?delsupps=true&id='.$row['id'].'&sname='. $row['sname'].'"> 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 Provider  " style="width:120px;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="sname'+rl+'" name="sname'+rl+'" value="" size="30"></td>'
					+'<td><input type="text" id="address'+rl+'" name="address'+rl+'" value="" size="30"></td>'
					+'<td><input type="text" id="phone'+rl+'" name="phone'+rl+'" value="" size="12"></td>'
					+'<td><input type="text" id="email'+rl+'" name="email'+rl+'" value="" size="20"></td></tr>');   
 document.getElementById("rl").value = parseInt(rl)+1;
}; 
</script>


