<?php

    include "config.php";
	include "functions.php";
	
    makeconnection();

    $aColumns = array('','product_stock_id', 'product_stock_brand', 'product_stock_model', 'product_stock_interface', 'product_stock_format',
        'product_stock_capacity', 'product_stock_rpm' ,'product_stock_parts', '', '',
        'product_stock_sn', 'product_stock_parameters', 'product_stock_qty', 'product_stock_pprice', 'product_stock_remark');

    $sLimit = "";
    if (isset($_GET['iDisplayStart']) && $_GET['iDisplayLength'] != '-1')
    {
        $sLimit = "LIMIT " . intval($_GET['iDisplayStart']) . ", " .
            intval($_GET['iDisplayLength']);
    }

    $sOrder = "";
    if (isset($_GET['iSortCol_0']))
    {
        $sOrder = "ORDER BY  ";
        for ($i = 0; $i < intval($_GET['iSortingCols']); $i++)
        {
            if ($_GET['bSortable_' . intval($_GET['iSortCol_' . $i])] == "true")
            {
                $sOrder .= "`" . $aColumns[intval($_GET['iSortCol_' . $i])] . "` " .
                    ($_GET['sSortDir_' . $i] === 'asc' ? 'asc' : 'desc') . ", ";
            }
        }

        $sOrder = substr_replace($sOrder, "", -2);
        if ($sOrder == "ORDER BY")
        {
            $sOrder = "";
        }
    }

    $sWhere = "";
    if (isset($_GET['sSearch']) && $_GET['sSearch'] != "")
    {
        
        /*
		$sWhere = "WHERE (";
		for ($i = 0; $i < count($aColumns); $i++)
        {
            if (isset($_GET['bSearchable_' . $i]) && $_GET['bSearchable_' . $i] == "true")
            {
                $sWhere .= "`" . $aColumns[$i] . "` LIKE '%" . mysql_real_escape_string($_GET['sSearch']) . "%' OR ";
            }
        }
        $sWhere = substr_replace($sWhere, "", -3);
        $sWhere .= ')'; 
		*/
		
		$sWhere = "WHERE (`product_stock_model` LIKE '%".mysql_real_escape_string($_GET['sSearch'])."%' OR `product_stock_parameters` LIKE '%".mysql_real_escape_string($_GET['sSearch'])."%')";
		$query1 = "search: ".$_GET['sSearch'];
		$query2 = "Column: ".$aColumns[$i-1];
		$query3 = $sWhere;
    }

    /* Individual column filtering */
    for ($i = 0; $i < count($aColumns); $i++)
    {
        if (isset($_GET['bSearchable_' . $i]) && $_GET['bSearchable_' . $i] == "true" && $_GET['sSearch_' . $i] != '')
        {
            if ($sWhere == "")
            {
                $sWhere = "WHERE ";
            }
            else
            {
                $sWhere .= " AND ";
            }
            $sWhere .= "`" . $aColumns[$i] . "` LIKE '%" . mysql_real_escape_string($_GET['sSearch_' . $i]) . "%' ";
        }
    }


    $sQuery = "SELECT SQL_CALC_FOUND_ROWS * FROM `product_stock`
		$sWhere
		$sOrder
		$sLimit
		";
		
    $rResult = mysql_query($sQuery) or fatal_error('MySQL Error: ' . mysql_errno());

	
    /* Data set length after filtering */
    $sQuery = "SELECT FOUND_ROWS()";
    $rResultFilterTotal = mysql_query($sQuery) or fatal_error('MySQL Error: ' . mysql_errno());
    $aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
    $iFilteredTotal = $aResultFilterTotal[0];

    /* Total data set length */
    $sQuery = "SELECT COUNT(`product_stock_id`) FROM `product_stock`";
    $rResultTotal = mysql_query($sQuery) or fatal_error('MySQL Error: ' . mysql_errno());
    $aResultTotal = mysql_fetch_array($rResultTotal);
    $iTotal = $aResultTotal[0];


    /*
     * Output
     */
    $output = array(
        "sEcho" => intval($_GET['sEcho']),
        "iTotalRecords" => $iTotal,
        "iTotalDisplayRecords" => $iFilteredTotal,
        "aaData" => array()
    );

    while ($aRow = mysql_fetch_array($rResult))
    {
        $row = array();
        
        $product_stock_id = $aRow["product_stock_id"];
        $product_stock_parts = $aRow["product_stock_parts"];
        
        switch ($product_stock_parts)
		{
			case "HDD" :	$product_stock_parts_class = "label-success"; break;//$product_stock_parts = $query1; break;
			case "PCB" :	$product_stock_parts_class = "label-info"; break;//$product_stock_parts = $query2; break;
			case "HDA" :	$product_stock_parts_class = "label-warning"; break;//$product_stock_parts = $query3; break;
					
		}           

			//$product_stock_parts = $queryyyy;
        
        $row[] = '<span class="row-details row-details-close"></span>';
        $row[] = '<a href="./?p=stock_add&product_stock_id='.$product_stock_id.'">'.$product_stock_id.'</a>';//$aRow["product_stock_id"];
		$row[] = $aRow["product_stock_brand"];
        $row[] = $aRow["product_stock_model"];
        $row[] = $aRow["product_stock_interface"];
        $row[] = $aRow["product_stock_format"];
        $row[] = $aRow["product_stock_capacity"];
        $row[] = $aRow["product_stock_rpm"];
        $row[] = '<span class="label label-sm '.$product_stock_parts_class.'"> '.$product_stock_parts.' </span>'; 
        $row[] = '<input type="checkbox" name="chk_product_stock_id[]" class="checkboxes" value="'.$product_stock_id.'"/>';        
        $row[] = $aRow["product_stock_sn"];
        $row[] = $aRow["product_stock_parameters"] == NULL ? "/" : $aRow["product_stock_parameters"];
        $row[] = $aRow["product_stock_qty"];
        $row[] = $aRow["product_stock_pprice"];
        $row[] = $aRow["product_stock_remark"];
        
          
        $output['aaData'][] = $row;
    }

    echo json_encode($output);
    