
<?php 
 //print_r($AppList); 
?>

<style>
            .table-bordered>thead>tr>th, .table-bordered>tbody>tr>th, .table-bordered>tfoot>tr>th, .table-bordered>thead>tr>td, .table-bordered>tbody>tr>td, .table-bordered>tfoot>tr>td {border: 1px solid #333;}
			
            </style>
<div class="content-wrapper">
    
 
	 <section class="content">
	 
	 <div>
         <center><b><h2>Requisition Approval List </h2></b></center>
		  <?php
                      $attributes = array('class' => 'form-label-left Approve ','name'  => 'Approve','id'  => 'Approve');
                        
                        echo form_open($this->config->base_url().'requisitionform/SearchRequistList',$attributes); ?>
         <div class="container-fluid">
	 <div class="row">
	   <div class="col-md-12">
		<div class="col-md-3">
		<label>Requisition Status</label>
			<div>
			
				 	  <select class="form-control required" id="RequisitionStatus" name="RequisitionStatus" >
                                            <option value="-1"  >Select Status</option>
                                            <?php
                                            if(!empty($Status))
                                            {
                                                foreach ($Status as $DEP)
                                                {
													$StatusCode = $DEP->StatusCode;
                                                  													
													if ($_POST['RequisitionStatus'] == $StatusCode) 
													{

												echo "<option value=\"".$StatusCode."\" selected=\"selected\">".$StatusCode.' -  '. $DEP->StatusName."</option>"; 
													}
													else 
													{
													echo "<option value=\"".$StatusCode."\">".$StatusCode.' -  '. $DEP->StatusName ."</option>";       
													}											
                                                    
                                                }
                                            }
                                            ?>
                                        </select>
			</div>
		</div>
		<div class="col-md-3">
		 <div class="form-group">
                <label>Date range:</label>

                <div class="input-group">
                  <div class="input-group-addon">
                    <i class="fa fa-calendar"></i>
                  </div>
                  <input type="text" class="form-control pull-right" id="SearchDate" value="<?php echo isset($_POST['SearchDate']) ? $_POST['SearchDate'] : '' ?>" name="SearchDate">
                </div>
                <!-- /.input group -->
              </div>
	   </div>
	
		<div class="col-md-3">
		<label>&nbsp;&nbsp;</label>
					<div class="form-group">
					 <input type="hidden" name="FromDate" id="FromDate" />
					<input type="hidden" name="ToDate" id="ToDate" />
					 <input type="submit"  class="btn btn-primary" >
					 <!--  <input type="Reset"  class="btn btn-primary" >
					  
					 <!-- <button type="button" class="close" data-dismiss="modal">&times;</button>
     					<!--<a  class="btn btn-info" onclick="Search()"  >Search</a> -->
					 <a  class="btn btn-primary" onclick="Reset()"  >Clear</a>
		 </div>  
		 
			
	   </div>	 
	 </div>
         </div>
	
  

<br/>
  
  
     <div class="modal fade" id="myModal" role="dialog">
		<div class="modal-dialog">
	   <!-- Modal content-->
	 <form>
		  <div class="modal-content">
			<div class="modal-header" style="background-color: #3c8dbc;color:#fff;">
			  <button type="button" class="close" data-dismiss="modal">&times;</button>
			  <center><h4> Requisition Details - REQ No</h4></center>				
			</div>
			<div align="left"> 
			 
			<div class="col-md-12">
						 <div class="col-md-4">
						 <label>Requistion Type</label>
									<div class="form-group">
												<?php
										 $data = array('name' => 'ReqType','value' => set_value('ReqType'),'id'=>'ReqType',  'class' => 'form-control' ,'readonly' => 'true');
										echo form_input($data);
										?>   
									</div>
								</div>
						 
						  <div class="col-md-4">
						   <label>Requested By</label>
									<div class="form-group">
												<?php
										 $data = array('name' => 'ReqBy','value' => set_value('ReqBy'),'id'=>'ReqBy',  'class' => 'form-control' ,'readonly' => 'true');
										echo form_input($data);
										?>   
									</div>
						 </div>
						  <div class="col-md-4">
						   <label>Designation</label>
									<div class="form-group">
												<?php
										 $data = array('name' => 'Desigination','value' => set_value('Desigination'),'id'=>'Desigination',  'class' => 'form-control' ,'readonly' => 'true');
										echo form_input($data);
										?>   
									</div>
						 </div>
						  
		 </div>
		<div class="col-md-12">
						 <div class="col-md-4">
						 <label>Requested Date</label>
									<div class="form-group">
												<?php
										 $data = array('name' => 'ReqDate','value' => set_value('ReqDate'),'id'=>'ReqDate',  'class' => 'form-control' ,'readonly' => 'true');
										echo form_input($data);
										?>   
									</div>
								</div>
						 
						  <div class="col-md-4">
						   <label>Cost Center Name</label>
									<div class="form-group">
												
					<?php
												
							$data = array('name' => 'CostCenter','value' => set_value('CostCenter'),'id'=>'CostCenter',  'class' => 'form-control' ,'readonly' => 'true');
										echo form_input($data);							
			
					

						?>						 										
									</div>
						 </div>
						  <div class="col-md-4">
						   <label>Avl Budget Amt</label>&nbsp;<img id="AlertImg"  src="<?php echo base_url(); ?>assets/dist/img/red.png" alt="your image" width="25px"  style="display:none"/>
									<div class="form-group">
												<?php
										 $data = array('name' => 'AvlBudgetAmount','value' => set_value('AvlBudgetAmount'),'id'=>'AvlBudgetAmount',  'class' => 'form-control' ,'readonly' => 'true');
										echo form_input($data);
										?>   
									</div>
						 </div>
						  
		</div>
		 
				<table id="Items" class="table table-bordered table-hover" style="background-color:#fff;font-size:12px;">    
					  <div align="right" style="padding-right:10px">
						 
					  <div class="modal fade"  role="dialog">
							
						<thead style="background-color:#ddf">
						  <tr>
							 
							<th>SNo</th>
							<th>Item Code</th>
							<th>Item Description</th>
							<th>UOM</th>
							<th>Requested Quantity</th>
							<th>Ordered Quantity</th>
							<th>Status</th>
						  </tr>
						</thead>
						<tbody id="tbleAppend">
   
						</tbody>						
						</div>
						</div>
				</table>
          <div class="modal-footer"> <div class="col-md-12"> 			 
			 <a  class="btn btn-info" data-dismiss="modal"  value="Cancel">Ok</a>
			  </div>
			</div>
		 </div>
		 
			</div>
			
		
		 </form>
			 
		  </div>
	</div>
  
  
    <table id="Requisition" class="table table-bordered table-hover" style="background-color:#fff;font-size:12px;">
  <div align="right" style="padding-right:10px">
     <div id="content" > </div> 
  <div class="modal fade"  role="dialog">
		
   <thead style="background-color:#ddf">
      <tr>
	     
         <th>Requisition Number</th>
         <th>Requested By</th>
         <th>Requested Date</th>
		 <th>Status</th>         
         <th>Remarks</th>
		  <th>Action</th>
      </tr>
    </thead>
    <tbody id="ApprovalList">
     
	    <?php
                    if(!empty($AppList))
                    {
						$index = 0;
                        foreach($AppList as $record)
                        {
							$index = $index +1;
                    ?> 
					<tr id="<?php echo $index ?>" >
						<td><a data-toggle="modal" data-target="#myModal" data-id="<%=index%>" data-userid="<?php echo $record->ReqNo ?>" ><u><?php echo $record->ReqNo ?></u></a></td>
						<td><?php echo $record->FirstName ?></td>
						<td align="right"><?php $Cdt =	new DateTime($record->CreatedDate);
									$CreatedDate =  $Cdt->format('d-m-Y');
						echo  $CreatedDate;
						?></td>
						<td><?php echo $record->StatusName ?></td>
						<?php if (($record->Status)  ==  REQ_PENDING_APPROVAL)
						{ 
						
						?>
						<td contenteditable="true"><input type="text" id="Remarks<?php echo $index ?>" name="Remarks<?php echo $index ?>"class="form-control" maxlength="100"></td>
						<td data-name="sel" contenteditable="true" id="Action<?php echo $index ?>" onchange="saveToDatabase(this,'question','<?php echo $record->ReqNo ; ?>','<?php echo $index ; ?>')">
						    <select name="status<?php echo $index ?>" id="status<?php echo $index ?>">        				        
    					        <option value="1">Select Action</option>
        				        <option value="2">Approve</option>
        				        <option value="3">Reject</option>
						    </select>
						</td>
						<?php 
						  }
						  else
						  {
						?>
						   <td><?php echo $record->Comments ?></td>
						   <td><?php echo $record->StatusName ?></td>
						<?php 	      
						
						  }
						  ?>
						  </tr>
						<?php
                        }
                    }
                    ?>
    
     
    </tbody>
	
  </table>
         
         
 
         <br>
	 </div>
         </div>
         
        </section>
</div>

<script> 
	var startDate = '';
	 var endDate = '';
 
function Reset()
{
	 $('#RequisitionStatus').val("-1");
	 $('#SearchDate').val('');
}
$("#myModal").on("shown.bs.modal", function(e) {
    var  ReqId = $(e.relatedTarget).data('userid');
	$("#CostCenter").val('');
	$("#tbleAppend").empty();
	$("#ReqType").val('');
	$("#ReqBy").val('');
	 $("#ReqDate").val('');
	 $("#Desigination").val('');
	 $("#AvlBudgetAmount").val('');
	 $('#AlertImg').attr("style","display:none;");
	
	$('#content').loader('show');
		    $.ajax({ 
			data:{id:ReqId},
			type:"POST",
			dataType: 'json',
			url:"<?php echo base_url() ?>requisitionform/ViewRequest",
			success:function(json) {
			//	success:function(data) {
				$('#content').loader('hide');
				//alert(data);
				$("#ReqType").val(json.Requist[0]['ReqType']);	
				$("#ReqBy").val(json.Requist[0]['Requestedby']);
				 $("#ReqDate").val(json.Requist[0]['CreatedDate']);
		        $("#Desigination").val(json.Requist[0]['Designation']);
				$("#CostCenter").val(json.Requist[0]['CostCenterCode']);
				 $("#AvlBudgetAmount").val(json.AvilBudAmount);
					
				$("#tbleAppend").append(json.Items);		
		
		}
  
}); 
	
});


function saveToDatabase(editableObj,column,ReqId,Index) {
	
	
	var id = $('#Remarks'+Index).val();
	var Status = $('#status'+Index).val();
	var StatusCode = '<?php echo REQ_APPROVED; ?>';
        var strMsg = 'Do you want to Approve the Requisition?';
	if(Status == '3')
	{
		       StatusCode = '<?php echo REQ_REJECTED; ?>';
                strMsg = 'Do you want to Reject the Requisition?'
	}
	var Remarks = $('#Remarks'+Index).val();
	if(Status == '3' && Remarks == '')
	{
		alert('Please enter remarks');
		$('#status'+Index).val('1'); 
		return false;	
		
	}

		  var answer = confirm(strMsg);
		if (answer) 
		{ 	
			$('#content').loader('show');
		    $.ajax({ 
			data:{id:id},
			type:"POST",
			url:"<?php echo base_url() ?>requisitionform/ApproveRequest?Status="+StatusCode+"&ReqNo="+ReqId,
			success:function(data) {
			if(data)
			{
				$('#content').loader('hide'); 
				 alert(data);
				 location.reload();		 
			}
			else
			{
				alert("Error");	
			}			
		
		}

  
}); 

}
else
{
return;
}

}
	
$(function() {	
	var d = new Date();

	var month = d.getMonth();
	var day = d.getDate();
	var year = d.getFullYear()  ;
	var SIAStartYear = year - 2015;
	var mindt = year-70;
	var maxdt =  year-15;
    $( ".datePicker" ).datepicker(
	{ 
	 	minDate : new Date(year-SIAStartYear,1,1),
		maxDate :'now',
		dateFormat: 'mm/dd/yy',changeMonth: true, changeYear: true,	  
	}
	);
    //$('#SearchDate').daterangepicker(  	);
    //Initialize Select2 Elements
    $(".select2").select2();
	//$('#SearchDate').val('');
	$('#SearchDate').daterangepicker(
{
    locale: {
      format: 'DD-MM-YYYY'
    },
   
	 showDropdowns: true
}, 
function(start, end, label) {
	
	 startDate = start.format('DD-MM-YYYY');
	  endDate = end.format('DD-MM-YYYY');
	    $('#FromDate').val(startDate);
	   $('#ToDate').val(endDate);
	 
	
    //alert("A new date range was chosen: " + start.format('DD-MM-YYYY') + ' to ' + end.format('DD-MM-YYYY'));
});


	
});
$(function () {
   
   $('#Items').DataTable({
      "paging": false,
      "lengthChange": true,
      "searching": false,
      "ordering": false,
      "info": false,
      "autoWidth": false
	   
    }); 
    $('#Requisition').DataTable({
      "paging": true,
      "lengthChange": true,
      "searching": true,
      "ordering": true,
      "info": true,
      "autoWidth": true
	   
    }); 
  });
	</script>
