<?php

session_start();

////////////////////////////////////////////// Database Connector /////////////////////////////////////////////////////////////
include './connection_sql.php';

////////////////////////////////////////////// Write XML ////////////////////////////////////////////////////////////////////
header('Content-Type: text/xml');

date_default_timezone_set('Asia/Colombo');
if ($_GET["Command"] == "getdt") {
 
	$ResponseXML = "";
	$ResponseXML .= "<salesdetails>";
	$ResponseXML .= "<sales_table><![CDATA[<table class=\"table\">
	<tr>
	<th style=\"width: 100px;text-align:center;\" class=\"success\">NAME</th>
	<th style=\"width: 100px;text-align:center;\"  class=\"success\">DETAILS</th>

	</tr>";

	$i = 1;
	
	if($_GET["department"]!=""){
	    $sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'invpara'    and (COLUMN_NAME='invc'  or COLUMN_NAME='invcm' or COLUMN_NAME='spinv1') group by COLUMN_NAME ";  
    	$result = $conn->query( $sql);
    	 foreach ($conn->query($sql) as $row) {
            $details=$row['COLUMN_NAME'] ;
            $sql1 = "Select $details as details from invpara where COMCODE='".$_GET["department"] ."'";
            $result1 = $conn->query($sql1);
            $row1 = $result1->fetch(); 
            
        	$field = "field" .$i;
    		$des = "details" . $i; 
    		
            $ResponseXML .= "<tr>";
            $ResponseXML .= "<td><input class='input-sm form-control' disabled type='text' id='" . $field . "' value='" . $row['COLUMN_NAME'] . "'></td>";
            $ResponseXML .= "<td><input class='input-sm form-control' style=\"text-align:right\" type='text'  id='" . $des . "' value='" . $row1['details'] . "'></td>";  
            $ResponseXML .= "</tr>";
            	$i = $i + 1;
        }
	}
	
	

	$ResponseXML .= "   </table>]]></sales_table>";



	$ResponseXML .= "<itm_count><![CDATA[" . $i . "]]></itm_count>";

	$ResponseXML .= " </salesdetails>";
	echo $ResponseXML;

}


 


if ($_POST["Command"] == "save_item") {

 
		try {
			$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$conn->beginTransaction();
 
			$i = 1; 
			$count = $_POST['count'];
			while ($_POST["count"] > $i) {	
    			$field = "field" .$i;
    	    	$des = "details" . $i; 
    	    	
    	    	
                $sql1 = "select * from invpara where COMCODE='".$_POST["department"] ."'"; 
                $result1 = $conn->query($sql1);
                if ($row1 = $result1->fetch()) {
                    $sql = "update invpara set $_POST[$field] ='" . $_POST[$des] . "' where COMCODE='".$_POST["department"] ."'";
    				$conn->query($sql);	 
    				
                }
                // else{
                //     $sql2 = "insert into invpara($_POST[$field]) values ('" . $_POST[$des] . "')"; 
                //     $result2 = $conn->query($sql2);
                     
                // }
    				 
				$i = $i+1;	
    		}
	        $conn->commit();
			echo "Updated";

		
		} catch (Exception $e) {
			$conn->rollBack();
			echo $e;
		}
	}

?>
