<?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'>";

    if ($_GET["ls"] == "new") {
        $sql = "select * from point_sch order by id desc limit 50";
    } else {

        if ($_GET["ls"] == "code") {
            $sql = "select * from point_sch where CODE like '" . Trim($_GET["txt_aircode"]) . "%' order by id desc limit 50";
        }
        if ($_GET["ls"] == "name") {
            $sql = "select * from point_sch where CAT like '" . Trim($_GET["txt_airname"]) . "%' order by id desc limit 50";
        }
        if ($_GET["ls"] == "country") {
            $sql = "select * from point_sch where BRAND like '" . Trim($_GET["txt_country"]) . "%' order by id desc limit 50";
        }
        if ($_GET["ls"] == "town") {
            $sql = "select * from point_sch where POINT_PER like '" . Trim($_GET["txt_town"]) . "%' order by id desc limit 50";
        }
       
    }


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

    foreach ($conn->query($sql) as $row) {
        $tb .= "<tr>";
        $tb .= "<td onclick=\"getcode('" . $row['CODE'] . "','" . $row['CAT'] . "','" . $row['BRAND'] . "','" . $row['POINT_PER'] . "')\">" . $row['CODE'] . "</td>";
        $tb .= "<td onclick=\"getcode('" . $row['CODE'] . "','" . $row['CAT'] . "','" . $row['BRAND'] . "','" . $row['POINT_PER'] . "')\">" . $row['CAT'] . "</td>";
        $tb .= "<td onclick=\"getcode('" . $row['CODE'] . "','" . $row['CAT'] . "','" . $row['BRAND'] . "','" . $row['POINT_PER'] . "')\">" . $row['BRAND'] . "</td>";
        $tb .= "<td onclick=\"getcode('" . $row['CODE'] . "','" . $row['CAT'] . "','" . $row['BRAND'] . "','" . $row['POINT_PER'] . "')\">" . $row['POINT_PER'] . "</td>";
        

        $tb .= "</tr>";
    }
    $tb .= "</table>";

    echo $tb;
}



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


    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
        $sql = "delete from point_sch where CODE = '" . $_GET['txt_aircode'] . "'";
        $result = $conn->query($sql);



        $sql = "Insert into point_sch (CODE, CAT, BRAND, POINT_PER)values 
    ('" . $_GET['txt_aircode'] . "', '" . $_GET['txt_airname'] . "', '" . $_GET['txt_country'] . "','" . $_GET['txt_town'] . "') ";

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


?>