<?php	
	require_once("includes/config.php");
	$ac = new DbQuery();
    $ac->checkLogin();
    

	$html = '';
	$total = 0.00;
	
	 /****** POS DB Integration**********/
    define('DB_SERVER','localhost');
    define('DB_USER','pos');
    define('DB_PASS' ,'##1Samir##');
    define('DB_NAME', 'pos');
    $con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

	// Check connection
	if (mysqli_connect_errno())
	{
		echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}
	


	       
	       /************* Products Rows *************/
	       $params = array(	
				'table' => 'product',
				'get'   =>'*',
			);

			$product_req = $ac->select($params,'get_data');
			
			    
			
		if($product_req){
		    $i=0;
		   foreach($product_req as $row){
    	       
    	      	/********** Fetch Vendor Rows ***************/
            	$contacts_query='SELECT * FROM `contacts` where type="supplier" and business_id=1 ';
            	$contacts_result = $con->query($contacts_query);
            	if(!$contacts_result) throw new exception(sprintf("Invalid query : %s", $contacts_query));
                
                $contacts_rows = array();
                if($contacts_result->num_rows <= 0){
                    echo "no users found !";
                }
                else{
            
                    $contacts_rows = $contacts_result->fetch_all(MYSQLI_ASSOC); //faster 
                }
                $contacts_option = '';
                if($contacts_rows){
                    foreach($contacts_rows as $contacts_row){
                        $contacts_option .= '
                        <option value="'.$contacts_row['id'].'" '.(($row['vendor']==$contacts_row['id'])?'selected':'').'>'.$contacts_row['name'].'</option>';
                    }
                }
    	       
    	       /*************************** Fetch Products ************************************/
    	       $product_querys='SELECT * FROM `products` where is_inactive=0 and business_id=1 and id='.$row['product'].' order by name';
            	$product_results = $con->query($product_querys);
            	if(!$product_results) throw new exception(sprintf("Invalid query : %s", $product_querys));
                
                $product_reqs = array();
                if($product_results->num_rows <= 0){
                    echo "no users found !";
                }
                else{
            
                    $product_reqs = $product_results->fetch_all(MYSQLI_ASSOC); //faster 
                }
                if($product_reqs && !empty($product_reqs)){
                   $name = '';
	                 foreach($product_reqs as $pr_row){
	                     $name = $pr_row['name'];
	                 }
        	      $html .=' <tr class="tr-shadow">
            	                <td><input type="hidden" name="products['.$i.'][product]" value="'.$row['product'].'" />'.$name.'</td>
            	                <td><div class="rs-select2--light rs-select2--sm"><select class="js-select2 custom-range-select" name="products['.$i.'][vendor]">'.$contacts_option.'</select><div class="dropDownSelect2"></div></div></td>
                            </tr>
                            <tr class="spacer"></tr>';
                    $i++;
                }
                    
		   }
	    
    	}else{
    	   $html .='<tr class="tr-shadow text-center">
        	            <td colspan="8">No Record Found</td>
        	       </tr>';
    	}
	/**************** END Fetching **************/
	/******** END Integration**************/
   $page_body ='
            <div class="main-content">
                <div class="section__content ">
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-md-12">
                                <!-- DATA TABLE -->
                                <form action="'.CALLBACK.'products.php" method="post">
                                    <input type="hidden" name="action" value="save-products-record" />
                                    <h3 class="title-5 m-b-35">Products</h3>
                                    
                                            <div class="table-responsive table-responsive-data2">
                                            
                                                <table class="table table-data2">
                                                    <thead>
                                                        <tr>
                                                            <th>Product</th>
                                                            <th>Vendor</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        '. $html .' 
                                                    </tbody>
                                                </table>
                                                <button class="au-btn au-btn-icon au-btn--green au-btn--small mt-3 px-5 float-right">Save</button>
                                           
                                            </div>
                                 </form>
                                <!-- END DATA TABLE -->
                            </div>
                        </div>
                    </div>
                </div>
            </div>';
        
    $title = 'Products';
		$body = array(
		"pageTitle"=>$title,
		'content'=>$page_body,
		);
		echo $ac->buildFullPage($body);

?>

   