<link rel="stylesheet" href="<?php echo site_url();?>assets/beatpicker/css/BeatPicker.min.css"/>
<link rel="stylesheet" href="<?php echo site_url();?>assets/beatpicker/css/demos.css"/>
<link rel="stylesheet" href="<?php echo site_url();?>assets/beatpicker/css/prism.css"/>
<script src="<?php echo site_url();?>assets/beatpicker/js/BeatPicker.min.js"></script>
<script src="<?php echo site_url();?>assets/beatpicker/js/prism.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<?php date_default_timezone_set('Asia/Kolkata');?>
<div id="page-wrapper">
            <div class="row">
                <div class="col-lg-12">
                    <h3 class="page-header">Edit Tender </h3>
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->
            <div class="row">
                <div class="col-lg-12">
                       <!--    <a href="<?php echo site_url();?>admin/Company/user_add" class="btn btn-info"> Add User</a>-->
                    <div class="panel panel-default">
                        <!-- /.panel-heading -->
                        <div class="panel-body">
                            <?php $success_message = $this->session->flashdata('success_message');
                            if (!empty($success_message)) {?>
                                <div class="alert alert-success">
                                      <a class="close" data-dismiss="alert" href="components-popups.html#" aria-hidden="true">×</a>
                                      <strong><?php echo $this->session->flashdata('success_message');  ?></strong>
                                </div> 
                            <?php }  ?>
                            <?php $failure_message = $this->session->flashdata('failure_message');
                            if (!empty($failure_message)) {?>
                                <div class="alert alert-warning">
                                      <a class="close" data-dismiss="alert" href="components-popups.html#" aria-hidden="true">×</a>
                                      <strong><?php echo $this->session->flashdata('failure_message');  ?></strong>
                                </div> 
                            <?php }  ?>
					   <form action="<?php echo site_url();?>admin/Quotation/update_quotation" method="post">
							<div class="row">
							<div class="form-group">
								<label class="control-label col-md-2">Tender No:<font color="red">*</font></label>
								<div class="col-md-4">
								<input type="text" required name="tender_no" value="<?php echo set_value('tender_no')?set_value('tender_no'):$tender_info[0]['tender_no']?>"  class="ui-dform-text form-control">
								</div>
								<?php
								$tender_date11=$tender_info[0]['tender_date'];
								$tender_date12=date("d-m-Y", strtotime($tender_date11));
								?>
								<label class="control-label col-md-2">Tender Date:<font color="red">*</font></label>
								<div class="col-md-4">

					   <div class="input-group date" id='datetimepicker2'>
						<input class="form-control" required type="text" placeholder="To" value="<?php echo set_value('tender_date')?set_value('tender_date'):$tender_date12?>"  name="tender_date">
						  <span class="input-group-addon">
							<span class="glyphicon glyphicon-calendar"></span>
						   </span>
						</div>
						</div>

							</div>
						</div>
							<br />
							<div class="row">
							<div class="form-group">
								<label class="control-label col-md-2">Product :<font color="red">*</font></label>
								<?php
								$product=$tender_info[0]['product'];
								$sql1 = "SELECT * FROM product where IsActive=0 and id='$product'";
								$record1 = $this->db->query($sql1);
								$pr=$record1->result_array();
								
								$tender_date1=$tender_info[0]['tender_due_date'];
								$tender_due_date=date("d-m-Y", strtotime($tender_date1));
				
								?>
								<div class="col-md-4">
									<select name="product" class="form-control" required>
									<option value="<?=$pr[0]['id']?>"><?=$pr[0]['product']?></option>
									<?php
									/* foreach($product_info as $p)
									{
										?>
										<option value="<?=$p['id']?>"><?=$p['product']?></option>
										<?php										
									} */
									?>
									</select>
								</div>
								
								<label class="control-label col-md-2">Tender Due Date:<font color="red">*</font></label>
								<div class="col-md-4">

					   <div class="input-group date" id='datetimepicker_to'>
						<input class="form-control" required type="text" placeholder="To" value="<?php echo set_value('tender_due_date')?set_value('tender_due_date'):$tender_due_date?>" name="tender_due_date">
						  <span class="input-group-addon">
							<span class="glyphicon glyphicon-calendar"></span>
						   </span>
						</div>
						</div>

							</div>
						</div>
						<br />
							<div class="row">
							<div class="form-group">
								<label class="control-label col-md-2">No.of Pieces:<font color="red">*</font></label>
							
								<div class="col-md-4">
								<input type="text" required name="pieces" value="<?php echo set_value('pieces')?set_value('pieces'):$tender_info[0]['pieces']?>" class="ui-dform-text form-control">
								</div>
								
								<label class="control-label col-md-2">Location:<font color="red">*</font></label>
								<div class="col-md-4">
								<input type="text" required name="location" value="<?php echo set_value('location')?set_value('location'):$tender_info[0]['location']?>" class="ui-dform-text form-control">
								</div>

							</div>
						</div>
							<br /><br />
					
           <div style="overflow-x:auto;">
              
     <table id="example" class="table table-striped table-bordered responsive">
        <thead>
            <tr>
				<th>Category</th>
				<th>Component</th>				
				<th>Price</th>                
				<th>Quantity</th>                
				<th>Amount</th>                
            </tr>
        </thead>
        <tbody>
			  
<?php 
/*$sql = "SELECT * FROM category where IsActive=0";
        $record = $this->db->query($sql);
        $cat_info=$record->result_array();
		$i=1;

	$category_id=$r['id'];
	 $sql1 = "SELECT * FROM component where IsActive=0 and category='$category_id'";
        $record1 = $this->db->query($sql1);
        $com=$record1->result_array();
		
		$tender_id=$tender_info[0]['tender_id'];
		$category=$r['category'];
	*/
	$tender_id=$tender_info[0]['tender_id'];
		 $q = "SELECT * FROM quotation where IsActive=0 and tender_id='$tender_id'";
        $q1 = $this->db->query($q);
        $q2=$q1->result_array();
		$i=1;
foreach($q2 as $q)
{	
    
    $category=$q['category'];
    $component=$q['component'];
    $sql = "SELECT * FROM category where category_no='$category'";
        $record = $this->db->query($sql);
        $cat_info=$record->result_array(); 
		
		
		$sqlcc = "SELECT * FROM component where component_no='$component'";
        $recordcc = $this->db->query($sqlcc);
        $cat_infocc=$recordcc->result_array();
//print_r($cat_info);
?>							
					<tr class="gradeX">
				<input type="hidden" value="" id="sort_order" name="sort_order[]">
					<td><input type="text" value="<?php echo $cat_info[0]['category'];?>" id="category_name" name="category[]" readonly class="ui-dform-text form-control"></td>
					<td>
					<input type="text" tabindex=<?=$i?> required value="<?php echo set_value('component')?set_value('component'):$cat_infocc[0]['component']?>" name="component[]" id="component_<?=$i?>" data-type="component" class="ui-dform-text form-control autocomplete_txt3" autocomplete="off">
					</td>
					<td><input type="text" value="<?=$q['price']?>" id="price_<?=$i?>" name="price[]" class="ui-dform-text form-control quantity"></td>
					<input type="hidden" name="valor[]" class="valor" value="<?=$cat_info[0]['category_no']?>">
					<input type="hidden" value="<?=$cat_infocc[0]['component_no']?>" id="component_no_<?=$i?>" name="component_no[]" class="form-control">
					<input type="hidden" name="sorder[]" value="<?=$q['sorder']?>">
					
					<td><input type="text" name="quantity[]" value="<?=$q['quantity']?>" id="quantity_<?=$i?>" class="quantity ui-dform-text"></td>
					<td><input type="text" name="amount[]" value="<?=$q['amount']?>" id="totamtgst_<?=$i?>" class="tamount ui-dform-text"></td>

							 </tr> 		
<?php
$i++;
}
		$q3 = "SELECT sum(amount) as amount FROM quotation where IsActive=0 and tender_id='$tender_id'";
        $q4 = $this->db->query($q3);
        $q5=$q4->result_array();
?>	
                      
  </tbody>
    </table>
					
							<div class="col-md-1 col-md-offset-1">
								Tamount:
							 </div>
							 <div class="col-md-4">

							 <input type="number" required step="0.0001" readonly class="ui-dform-text form-control" tabindex=<?=$i?> value="<?php echo set_value('tamount')?set_value('tamount'):$q5[0]['amount']?>" id="tamount" name="tamount" placeholder="Total Amount" onkeypress="return IsNumeric(event);" ondrop="return false;" onpaste="return false;">
							</div>
							<div class="col-md-1">
								Status:
							 </div>
							 <div class="col-md-4">
								<select name="cstatus" class="form-control">
								<option value="<?=$q['cstatus']?>"><?=$q['cstatus']?></option>				
								<option value="Complete">Complete</option>								
								<option value="Incomplete">Incomplete</option>								
								</select>
							</div>
							<input type="hidden" value='<?=$tender_info[0]['tender_id'];?>' name='tender_id'>
							<div class="col-md-2 col-md-offset-6">
								<div class="col-md-1">
								<input href type="submit" name="submit" value="UPDATE >>" target="_blank" class="btn btn-warning pull-right" >	
								</div>
							</div>
						</form>
		
					<script>
					$(document).on('focus','.autocomplete_txt3',function(){
									type = $(this).data('type');
									if(type =='component' )autoTypeNo=0; 
									 var valor = $(this).parents('tr').find('.valor').val();
									 var product = '<?php echo $product ;?>';
								//	alert(id);
									$(this).autocomplete({
										source: function( request, response ) {
										//	alert(valor);
											//alert(category);
											//alert($("#category_id").val());
											$.ajax({
												url : 'http://pctest.spectracloud.net/spectra/admin/Quotation/auo_product3',
												dataType: "json",
												method: 'post',
												
												data: {
												 category: valor,
												 product: product,
												  //alert(category);												  
												   name_startsWith: request.term,
												   type: type 
												},
												 success: function( data ) {
													 response( $.map( data, function( item ) {
														var code = item.split("|");
														return {
															label: code[autoTypeNo],
															value: code[autoTypeNo],
															data : item
														}
													}));
												}
											});
										},
										autoFocus: true,	      	
										minLength: 0,
										select: function( event, ui ) {
											var names = ui.item.data.split("|");
											id_arr = $(this).attr('id');
											id = id_arr.split("_");
											//alert(names);
											$('#component_'+id[1]).val(names[0]);
											$('#price_'+id[1]).val(names[1]);
											$('#component_no_'+id[1]).val(names[2]);
											$('#totamtgst_'+id[1]).val(names[1]);
									}		      	
									});
								});
								
								
		 $(document).on('change keyup blur','.quantity',function(){
			id_arr = $(this).attr('id');
			id = id_arr.split("_");
			quantity = $('#price_'+id[1]).val();
			price = $('#quantity_'+id[1]).val();
			if( quantity!='' && price !='' ) $('#totamtgst_'+id[1]).val( (parseFloat(price)*parseFloat(quantity)).toFixed());	
			
			 calculateTotal4();
		});
		function calculateTotal4(){
									tamount = 0 ; total1 = 0; 
									$('.tamount').each(function(){
										if($(this).val() != '' )tamount += parseFloat( $(this).val() );
									});
									$('#tamount').val(tamount.toFixed() );

										total1 = tamount;
										
										
										
								}
			  
	</script>
	</div>					   
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>
            <!-- /.row -->            
        </div>
        <!-- /#page-wrapper -->


<script>
function doconfirm()
{
    job=confirm("Are you sure to delete permanently?");
    if(job!=true)
    {
        return false;
    }
}
</script>
		
             <script>

$(document).keypress(function(e) {
    if(e.which == 13) {
e.preventDefault();
            // Do something here if the popup is open
            //alert("dd")
            var index = $('.ui-dform-text').index(document.activeElement) + 1;
            $('.ui-dform-text').eq(index).focus();
        
    }
}); 
</script>
      