<?php
require_once '../lib/DataSourceResult.php';
require_once '../lib/Kendo/Autoload.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    header('Content-Type: application/json');

    $request = json_decode(file_get_contents('php://input'));

    $result = new DataSourceResult('sqlite:..//sample.db');

    echo json_encode($result->read('Orders', array('OrderID', 'CustomerID', 'ShipName', 'ShipAddress', 'ShipCity', 'ShipCountry'), $request));

    exit;
}

require_once '../include/header.php';

$transport = new \Kendo\Data\DataSourceTransport();

$read = new \Kendo\Data\DataSourceTransportRead();

$read->url('virtualization-remote-data-binding.php')
     ->contentType('application/json')
     ->type('POST');

$transport->read($read)
          ->parameterMap('function(data) {
              return kendo.stringify(data);
          }');

$schema = new \Kendo\Data\DataSourceSchema();
$schema->data('data')
       ->total('total');

$dataSource = new \Kendo\Data\DataSource();

$dataSource->transport($transport)
           ->pageSize(100)
           ->schema($schema)
           ->serverSorting(true)
           ->serverPaging(true);

$grid = new \Kendo\UI\Grid('grid');

$orderID = new \Kendo\UI\GridColumn();
$orderID->field('OrderID')
            ->width(110)
            ->title('Order ID');

$customerID = new \Kendo\UI\GridColumn();
$customerID->field('CustomerID')
          ->width(130)
          ->title('Customer ID');

$shipName = new \Kendo\UI\GridColumn();
$shipName->field('ShipName')
          ->width(280)
          ->title('Ship Name');

$shipAddress = new \Kendo\UI\GridColumn();
$shipAddress->field('ShipAddress')
          ->title('Ship Address');

$shipCity = new \Kendo\UI\GridColumn();
$shipCity->field('ShipCity')
          ->width(160)
          ->title('Ship City');

$shipCountry = new \Kendo\UI\GridColumn();
$shipCountry->field('ShipCountry')
          ->width(160)
          ->title('Ship Country');

$scrollable = new \Kendo\UI\GridScrollable();
$scrollable->virtual(true);

$grid->addColumn($orderID, $customerID, $shipName, $shipAddress, $shipCity, $shipCountry)
     ->dataSource($dataSource)
     ->scrollable($scrollable)
     ->sortable(true)
     ->height(543);

echo $grid->render();
?>

<style>

    /*horizontal Grid scrollbar should appear if the browser window is shrinked too much*/
    #grid table
    {
        min-width: 1190px;
    }

</style>

<?php require_once '../include/footer.php'; ?>
