<?php

session_start();


require_once ("connection_sql.php");

date_default_timezone_set('Asia/Colombo');

if ($_GET["Command"] == "getdt") {

    $tb = "";
    $tb .= "<table class='table table-hover'>";
    $sql = "select * from s_mas order by GROUP1 desc limit 5000";
    if ($_GET["ls"] == "new") {
        $sql = "select * from s_mas order by GROUP1 desc limit 5000";
    } else {

        if ($_GET["ls"] == "code") {
            $sql = "select * from s_mas where STK_NO like '" . Trim($_GET["txt_aircode"]) . "%' order by STK_NO desc limit 200";
        }
        if ($_GET["ls"] == "name") {
            $sql = "select * from s_mas where PART_NO like '" . Trim($_GET["txt_airname"]) . "%' order by STK_NO desc limit 200";
        }
        if ($_GET["ls"] == "country") {
            $sql = "select * from s_mas where DESCRIPT like '" . Trim($_GET["txt_country"]) . "%' order by STK_NO desc limit 200";
        }
        if ($_GET["ls"] == "group") {
            $sql = "select * from s_mas where GROUP1 like '" . Trim($_GET["group"]) . "%' order by STK_NO desc limit 200";
        }
       
    }

//    echo $sql;
    $tb .= "<tr>";
    $tb .= "<td style=\"\"></td>";
    $tb .= "<td style=\"\"></td>";
    $tb .= "<td style=\"\"></td>";
    $tb .= "<td style=\"width: 350px;\"></td>";
    $tb .= "<td style=\"\"></td>";
    
   
    $tb .= "</tr>";

    foreach ($conn->query($sql) as $row) {
        $tb .= "<tr>";
        $tb .= "<td onclick=\"getcode('" . $row['STK_NO'] . "','" . $row['PART_NO'] . "','" . $row['DESCRIPT'] . "','" . $row['GROUP4'] . "','" . $row['GROUP1'] . "')\">" . $row['STK_NO'] . "</td>";
        $tb .= "<td onclick=\"getcode('" . $row['STK_NO'] . "','" . $row['PART_NO'] . "','" . $row['DESCRIPT'] . "','" . $row['GROUP4'] . "','" . $row['GROUP1'] . "')\">" . $row['PART_NO'] . "</td>";
        $tb .= "<td onclick=\"getcode('" . $row['STK_NO'] . "','" . $row['PART_NO'] . "','" . $row['DESCRIPT'] . "','" . $row['GROUP4'] . "','" . $row['GROUP1'] . "')\">" . $row['GROUP1'] . "</td>";
        $tb .= "<td style=\"width: 350px;\" onclick=\"getcode('" . $row['STK_NO'] . "','" . $row['PART_NO'] . "','" . $row['DESCRIPT'] . "','" . $row['GROUP4'] . "','" . $row['GROUP1'] . "')\">" . $row['DESCRIPT'] . "</td>";
        $tb .= "<td onclick=\"getcode('" . $row['STK_NO'] . "','" . $row['PART_NO'] . "','" . $row['DESCRIPT'] . "','" . $row['GROUP4'] . "','" . $row['GROUP1'] . "')\">" . $row['GROUP4'] . "</td>";
        
        $tb .= "</tr>";
    }
    $tb .= "</table>";

    echo $tb;
}



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


    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();




        $sql = "UPDATE s_mas SET GROUP4 = '" . $_GET['txt_town'] . "' where STK_NO = '" . $_GET['txt_aircode'] . "' ";


        $result = $conn->query($sql);
        $conn->commit();
        echo "Saved";
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}


?>