<?php	
	require_once("includes/config.php");
	$ac = new DbQuery();
    $ac->checkLogin();
    
    /****** 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();
	}
	
	/********** Fetch Contacts Rows ***************/
	$contacts_query='SELECT * FROM `contacts` where business_id=1 and type="customer" ';
	$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 
    }
    
    $select = array();
    
    if(isset($_GET['id']) && !empty($_GET['id'])){
        
       $id = $ac->mcDecrypt($_GET['id']);
    
        /******** Fetch PO Row ********/
        $param = array(	
    					'table' => 'po_req',
    					'get'   =>'*',
    					'where' => array('ID'=> $id),
    				);
    
    	$select = $ac->select($param,'get_row'); 
    }
    
    	/********** Fetch Products Rows ***************/
        	
            
            $param = array(	
					'table' => 'po_manage_req',
					'get'   =>'*',
					'where' => array('po_req_id'=> $select['ID']),
				);

			$products_rows = $ac->select($param,'get_data');
				
            $products_html = '';
            if($products_rows){
                foreach($products_rows as $products_row){
                    $products_html .= '
                    <tr class="product" data-id="'.$products_row['product_id'].'">
                         <input type="hidden" name="po['.$products_row['product_id'].'][product_id]" value="'.$products_row['product_id'].'">
                        <input type="hidden" name="po['.$products_row['product_id'].'][name]" value="'.$products_row['name'].'">
                        <input type="hidden" name="po['.$products_row['product_id'].'][price]" value="'.number_format($products_row['price'], 2).'">
                        <td>'.$products_row['name'].'</td>
                        <td><input type="number" name="po['.$products_row['product_id'].'][quantity]" value="'.$products_row['quantity'].'" min="1" class="quantity" data-id="'.$products_row['product_id'].'"></td>
                        <td class="subtotal" data-default="'.number_format($products_row['price'], 2).'" data-id="'.$products_row['product_id'].'">'.number_format($products_row['price'], 2).'</td>
                        <td><a data-toggle="tooltip" title="Delete" href="javascript:void(0);" class="btn system-red-float-btn remove_product" data-rid="'.$products_row['po_req_id'].'" data-id="'.$products_row['product_id'].'" data-original-title="Delete"><i class="fa fa-trash"></i></a></td>
                    </tr>';
                }
            }
            
    
    
    
    $contacts_option = '';
    if($contacts_rows){
        foreach($contacts_rows as $contacts_row){
            $contacts_option .= '
            <option value="'.$contacts_row['id'].'" '.(( isset($select['customer']) && $select['customer']==$contacts_row['id'] )?'selected':'').'>'.$contacts_row['name'].'</option>';
        }
    }
    
    $page_body .='
        <!-- MAIN CONTENT-->
            <div class="main-content">
                <div class="section__content section__content--p30">
                    <div class="container-fluid">
                                <h3 class="title-5 m-b-35">Manage PO Request</h3>
                                <form action="'.CALLBACK.'po-request.php" method="post" >
                                    <input type="hidden" name="id" value="'.$select['ID'].'">
                                    <input type="hidden" name="quantity" value="'.$select['quantity'].'">
                                    <input type="hidden" name="action" value="save_po_req">
                                    <div class="card">
                                        <div class="card-body">
                                            <hr>
                                           
                                                <div class="row">
                                                    <div class="col-sm-6">
                                                        <div class="form-group">
        	                                                <label for="customer" class="control-label mb-1">Customer</label>  
        	                                                <div class="input-group">
        	                                                    <div class="input-group-addon">
        	                                                        <i class="fa fa-user"></i>
        	                                                    </div>
        	                                                    <select id="customer" name="customer" class="form-control" aria-required="true" aria-invalid="false" required>
        	                                                        '.$contacts_option.'
        	                                                    </select>
        	                                                </div>
                                                          
                                                        </div>
                                                    </div>
                                                    <div class="col-sm-6">
                                                        <div class="form-group">
                                                            <label for="sale_date" class="control-label mb-1">Delivery Date</label>
                                                            <input id="sale_date" name="sale_date" type="date" value="'.$select['sale_date'].'" class="form-control" aria-required="true" aria-invalid="false" required>
                                                        </div>
                                                    </div>
                                                </div>
                                        </div>
                                    </div>
                                    <div class="card">
                                        <div class="card-body">
                                           
                                                <div class="row">
                                                    <div class="col-sm-12">
                                                        <div class="form-group search-container">
        	                                                <input type="text" id="product_val" name="product_val" class="form-control" placeholder="Enter Product Name" >
        	                                                <div class="product-container" style="display: none;"></div>
        	                                            </div>
                                                    </div>
                                                </div>
                                                <div class="row">
                                                    <div class="col-sm-12">
                                                        <div class="table-responsive">
                                                            <table class="table table-condensed table-bordered table-striped table-responsive" id="app_products">
                                                                <thead>
                                        							<tr>
                                        								<th class="text-center">Product</th>
                                        								<th class="text-center">Quantity</th>
                                        								<th class="text-center">Subtotal</th>
                                        								<th class="text-center">&nbsp;&nbsp;</th>
                                        							</tr>
                                        						</thead>
                                        						<tbody>
                                        						'.$products_html.'
                                        						</tbody>
                                                            </table>
        	                                            </div>
        	                                            <div class="table-responsive">
                                                            <table class="table table-condensed table-bordered table-striped table-responsive">
                                                                <tbody>
                                                                    <tr>
                                            							<td>
                                            								<div class="pull-right">
                                            								<b>Items:</b> 
                                            								<span class="total_quantity">'.(( isset($select['quantity']) && !empty($select['quantity']) )?$select['quantity']:'0').'</span>
                                            								&nbsp;&nbsp;&nbsp;&nbsp;
                                            								<b>Total: </b>
                                            									<span class="price_total">'.(( isset($select['amount']) &&  !empty($select['amount']) )?$select['amount']:'0.00').'</span>
                                            								</div>
                                            							</td>
                                            						</tr>
                                            					</tbody>
                                                            </table>
                                                        </div>
                                                    </div>
                                                </div>
                                        </div>
                                    </div>
                                    <div class="card">
                                        <div class="card-body">
                                            <div class="card-title">
                                                <h3 class="text-center title-2">Add Payment</h3>
                                            </div>
                                            <hr>
                                           
                                                <div class="row">
                                                   
                                                    <div class="col-sm-6">
                                                        <div class="form-group">
                                                            <label for="amount" class="control-label mb-1">Amount</label>
                                                            <div class="input-group">
        	                                                    <div class="input-group-addon">
        	                                                        <i class="fas fa-money-bill-alt"></i>
        	                                                    </div>
                                                                <input id="amount" name="amount" type="text" value="'.$select['amount'].'" class="form-control" aria-required="true" aria-invalid="false" required>
                                                            </div>
                                                        </div>
                                                    </div>
                                                    <div class="col-sm-6">
                                                        <div class="form-group">
        	                                                <label for="payment_method" class="control-label mb-1">Payment Method</label>  
        	                                                <div class="input-group">
        	                                                    <div class="input-group-addon">
        	                                                        <i class="fas fa-money-bill-alt"></i>
        	                                                    </div>
        	                                                    <select id="payment_method" name="payment_method" class="form-control" aria-required="true" aria-invalid="false" required>
        	                                                        <option value="adv_cash" '.(( isset($select['payment_method']) && $select['payment_method']=='adv_cash' )?'selected':'').'>Advance Cash</option>
        	                                                        <option value="cash" '.(( isset($select['payment_method']) && $select['payment_method']=='cash' )?'selected':'selected').'>Cash</option>
        	                                                        <option value="card" '.(( isset($select['payment_method']) && $select['payment_method']=='card' )?'selected':'').'>Card</option>
        	                                                        <option value="cheque" '.(( isset($select['payment_method']) && $select['payment_method']=='cheque' )?'selected':'').'>Cheque</option>
        	                                                        <option value="bnk_trs" '.(( isset($select['payment_method']) && $select['payment_method']=='bnk_trs' )?'selected':'').'>Bank Transfer</option>
        	                                                        <option value="other" '.(( isset($select['payment_method']) && $select['payment_method']=='other' )?'selected':'').'>Other</option>
        	                                                    </select>
        	                                                </div>
                                                          
                                                        </div>
                                                    </div>
                                                </div>
                                        </div>
                                    </div>
                                            <hr>
                                                
                                                <div class="row">
                                                    <div class="col-sm-6 offset-3">
                                                        <button id="payment-button" type="submit" class="btn btn-lg btn-info btn-block">
                                                            <i class="fa fa-lock fa-lg"></i>&nbsp;
                                                            <span id="payment-button-amount">Submit</span>
                                                            <span id="payment-button-sending" style="display:none;">Sending…</span>
                                                        </button>
                                                    </div>
                                                </div>
                                    
                                </form>
                        
                    </div>
                </div>
            </div>
        
        
    ';
    
    $title = 'Manage PO Request';
	$body = array(
		"pageTitle"=>$title,
		'content'=>$page_body,
	);
	echo $ac->buildFullPage($body);

?>