<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);
//ini_set('display_errors', 1);

session_start();
ob_start();

require_once('configuration/server_connect.php');
require_once('configuration/all_function.php');

$active_id=$_REQUEST['active_id'];
$menu_id=$_REQUEST['menu_id'];

$request_date=$_REQUEST['tr_date'];

 // Common Information

$page_title="Calf Information";
$page="1001_calf_info.php?active_id=".$active_id."&menu_id=".$menu_id."";


if(isset($_POST['ins_primary_data']))

{


$entry_by =  $_SESSION['user']['id'];
$entry_at = $now=date('Y-m-d H:i:s');

$tr_no=0;
$tr_date=$_POST['tr_date'];
$item_id=$_POST['item_id'];
$cattle_id=$_POST['cattle_id'];
$description=$_POST['description'];
$total_unit=$_POST['total_unit'];
$unit=$_POST['unit'];

$item_sql="SELECT * FROM item_info  WHERE item_id='".$item_id."'";
$item_data = find_all_field_sql($conn,$item_sql);

$total_amt=$total_unit*$item_data->cost_price;

//if($dr_amt>0){}
 $ins_sql = 'INSERT INTO  cattle_medicine_issue (tr_no, tr_date, cattle_id, cattle_no, item_id, unit_name, unit_price, total_unit, total_amt, entry_by, entry_at, edit_by, edit_at, description, unit) VALUES

("'.$tr_no.'", "'.$tr_date.'", "'.$cattle_id.'", "'.$cattle_no.'", "'.$item_id.'", "'.$item_data->unit_name.'", "'.$item_data->cost_price.'", "'.$total_unit.'", "'.$total_amt.'", "'.$entry_by.'", "'.$entry_at.'", "0", "0000-00-00", "'.$description.'", "'.$unit.'")';
	
mysqli_query($conn,$ins_sql);


header("location:".$page.'&tr_date='.$tr_date."");

?>

<!--<script language="javascript">
window.location.href = "<?php echo $page;?>&request_no=<?=$jv_no?>";
</script>-->

<?php }

if(isset($_POST['run_data']))

{

$tr_date=$_POST['tr_date'];
$calf_status_src=$_POST['calf_status_src'];
$cattle_id_src=$_POST['cattle_id_src'];

header("location:".$page.'&tr_date='.$tr_date.'&calf_status_src='.$calf_status_src.'&cattle_id_src='.$cattle_id_src."");

?>

<!--<script language="javascript">
window.location.href = "<?php echo $page;?>&request_no=<?=$jv_no?>";
</script>-->

<?php } ?>




<!DOCTYPE html>
<html lang="en">

<!-- ======= <head> ======= -->
 <?php require_once('inc_files/inc_head.php'); ?>
<!-- ======= </head> ======= -->
 <?php
  do_calander('#tr_date');
  do_calander('#date_of_birth');
  ?>
  
<script language="javascript">

function getXMLHTTP() { //fuction to return the xml http object



		var xmlhttp=false;	



		try{



			xmlhttp=new XMLHttpRequest();



		}



		catch(e)	{		



			try{			



				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");

			}

			catch(e){



				try{



				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");



				}



				catch(e1){



					xmlhttp=false;



				}



			}



		}



		 	



		return xmlhttp;



    }




function TR_calculation(id){

var unit_price=(document.getElementById('unit_price_'+id).value)*1; 
var total_unit=(document.getElementById('total_unit_'+id).value)*1; 

document.getElementById('total_amt_'+id).value=(unit_price*total_unit);

}




	function update_value(id)
	{

var unit_price=(document.getElementById('unit_price_'+id).value); 
var total_unit=(document.getElementById('total_unit_'+id).value); 
var total_amt=(document.getElementById('total_amt_'+id).value); 

var flag=(document.getElementById('flag_'+id).value); 

var strURL="1002_cattle_medicine_issue_ajax.php?id="+id+"&unit_price="+unit_price+"&total_unit="+total_unit+"&total_amt="+total_amt+"&flag="+flag;


		var req = getXMLHTTP();



		if (req) {



			req.onreadystatechange = function() {

				if (req.readyState == 4) {

					// only if "OK"

					if (req.status == 200) {						

						document.getElementById('divi_'+id).style.display='inline';

						document.getElementById('divi_'+id).innerHTML=req.responseText;						

					} else {

						alert("There was a problem while using XMLHTTP:\n" + req.statusText);

					}

				}				

			}

			req.open("GET", strURL, true);

			req.send(null);

		}	



}




</script>

<style>
		
		/*.fixed_head{
		 / Important /
  background-color: red;
  position: sticky;
  z-index: 100;
  top: 0;
		}	
	
	table thead  {
  / Important /
  background-color: red;
  position: sticky;
  z-index: 100;
  top: 0;
}*/
	
		</style>
  
<body>

  <!-- ======= Header ======= -->
  <?php require_once('inc_files/inc_header.php'); ?><!-- End Header -->

  <!-- ======= Sidebar ======= -->
   <?php  require_once('inc_files/inc_menu.php'); ?>
   <!-- End Sidebar-->
   
  

  <main id="main" class="main">

    <div class="pagetitle">
      <h1><?php echo $page_title;?></h1>
      <nav>
        <ol class="breadcrumb">
          <li class="breadcrumb-item"><a href="home.php">Home</a></li>
          <li class="breadcrumb-item">Cattle Management</li>
          <li class="breadcrumb-item active"><?php echo $page_title;?></li>
        </ol>
      </nav>
    </div><!-- End Page Title -->
    <section class="section">
      <div class="row">
	  
	
		<div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
             <!-- <h5 class="card-title"><strong><?php echo $page_title;?></strong></h5>-->
			 <br />

              <!-- No Labels Form -->
              <form class="row g-3" action="" method="post" name="codz" id="codz">
                
       
          <div class="col-md-2">
           <label for="first_name" class="form-label"><strong> Date</strong></label> 
			<input type="text" class="form-control" id="tr_date" name="tr_date" required value="<?php echo isset($_REQUEST['tr_date'])?$_REQUEST['tr_date']:date('Y-m-d');?>" >
	
                </div>
                <div class="col-md-3">
			<label for="first_name" class="form-label"><strong> Calf Location </strong></label> 
        <select id="calf_status_src" name="calf_status_src"  class="form-select"  style="width: 95%;">
           
 			<?php foreign_relation($conn,'calf_status','id','calf_status',$_REQUEST['calf_status_src'],'1 order by id');?>
         </select>
		
                </div>
                <div class="col-md-2">
				<label for="first_name" class="form-label"><strong> Mother ID </strong></label> 
         <select id="cattle_id_src" name="cattle_id_src"  class="form-select"  style="width: 95%;">
                      <option></option>
<?php foreign_relation($conn,'cattle_info','cattle_id','cattle_no',$_REQUEST['cattle_id_src'],'1 group by cattle_id order by cattle_type, cattle_sl');?>
                    </select>
                </div>
				<div class="col-md-2">
				<label for="first_name" class="form-label"><strong> </strong></label> 
       
                </div>
				<div class="col-md-3" align="center">
				
				 <button type="submit" name="run_data" id="run_data" class="btn btn-success rounded-pill btn-sm">&nbsp;<i class="bi bi-filter me-1"></i> Run Report&nbsp;&nbsp;</button>
				</div>
				
                
              </form><!-- End No Labels Form -->

            </div>
          </div>
		
		</div>
		
		
	  
	  
        <div class="col-lg-12"> 

          <div class="card">
            <div class="card-body">
			
			<br>
			
			<?php //if($request_date!=""){?>	
			<form  action="" method="post" name="form1" id="form1" onSubmit="return check()" style="font-size:12px;">
             <table class="table table-bordered ">
                <thead>
                  <tr style="text-transform:uppercase; color:#012970; background:#f6f9ff;  border-top: 1px solid #CCCCCC;">
                    <th width="30%" scope="col">Calf No </th>
                    <th width="30%" scope="col">Mother ID </th>
                    <th width="30%" scope="col"><span style="white-space:nowrap;">Date Of Birth </span></th>
                    <th width="30%" scope="col">Type</th>
                    <th width="20%" scope="col">Gender</th>
                    <th width="12%" scope="col">Breed</th>
                    <th width="12%" scope="col">Location</th>
                    <th width="12%" scope="col">Status</th>
                    <th width="8%" scope="col">ACTION</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <th scope="row"><input type="text" class="form-control" name="calf_no" id="calf_no" value="" required style="width:100px;"></th>
                    <th scope="row">
					<select id="mother_id" name="mother_id"  class="form-select"  style="width: 100px;" required>
					<option></option>
                     
						<?php foreign_relation($conn,'cattle_info','cattle_id','cattle_no',$mother_id,'1 group by cattle_id order by cattle_type, cattle_sl');?>
                    </select>					</th>
                    <th scope="row"><input type="text" class="form-control" name="date_of_birth" id="date_of_birth" value="" required style="width:120px;"></th>
                    <th scope="row">
					<select id="cattle_type" name="cattle_type"  class="form-select"  style="width: 120px;" required>
                     <option value="0"></option>
                     <?php foreign_relation($conn,'cattle_type','id','cattle_type',$data->cattle_type,'id in (3,4) ');?>
                   </select></th>
                    <td><select id="gender" name="gender"  class="form-select"  required style="width: 100px;">
                     <option value="0"></option>
                     <?php foreign_relation($conn,'gender','id','gender',$data->gender,'1');?>
                   </select></td>
                    <td>
					<select id="breed" name="breed"  class="form-select"  required style="width: 120px;">
                     <option value="0"></option>
                     <?php foreign_relation($conn,'cattle_breed','id','cattle_breed',$data->breed,'1');?>
                   </select>					</td>
                    <td><select id="status_id" name="status_id"  class="form-select"  required style="width: 120px;">
                    
                     <?php foreign_relation($conn,'calf_status','id','calf_status',$data->status_id,'1 order by id');?>
                   </select></td>
                    <td>
					
				  <input name="status" type="text" id="status"  value="<?php echo $data->status; ?>" style="width:150px" class="form-control"/>
		         
					</td>
                    <td> 
		<button type="submit" class="btn btn-primary btn-sm" name="ins_primary_data" id="ins_primary_data" style="white-space:nowrap" ><i class="bi bi-plus"></i>ADD&nbsp;&nbsp;</button>					</td>
                  </tr>
                </tbody>
              </table>
                

              </form>
			<?php //}?>

              <!-- Table with stripped rows id="example" -->
              <table class="table table-sm" style="font-size:12px;"  >
                <thead class="fixed_head">
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; border-top: 1px solid #CCCCCC; background:#f6f9ff; height:32px;">
                    <th width="2%" scope="col">SL</th>
                    <th width="10%" scope="col">Calf No </th>
                    <th width="11%" scope="col">Mother ID </th>
                    <th width="9%" scope="col" style="white-space:nowrap;">Date Of Birth </th>
                    <th width="12%" scope="col">Type</th>
                    <th width="10%" scope="col">Gender</th>
                    <th width="12%" scope="col">Breed</th>
                    <th width="12%" scope="col" style="white-space:nowrap" ><div align="center">Location</div></th>
                    <th width="14%" scope="col">Status</th>
                    <th width="8%" scope="col">Action</th>
                  </tr>
                </thead>
                <tbody>
		<?php
		$sl=1;
		
		//if($_POST['tr_date']!=''){
//		$tr_date =$_REQUEST['tr_date'];
//		}else{
//		$tr_date =date('Y-m-d');
//		}
		
		
		
		
		 $sql = "select cattle_id, cattle_no from cattle_info where 1 group by  cattle_id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $cattle_no[$info->cattle_id]=$info->cattle_no;
		}
		
		
					if($_REQUEST['calf_status_src']==''){
						$status_id_con=" and c.status_id=1";
					}elseif($_REQUEST['calf_status_src']>0){
						$status_id_con=" and c.status_id='".$_REQUEST['calf_status_src']."'";
					}
					
					//if($_REQUEST['calf_status_src']!='')
					//$status_id_con=" and c.status_id='".$_REQUEST['calf_status_src']."'";
					
					if($_REQUEST['cattle_id_src']!='')
					$cattle_id=" and c.mother_id='".$_REQUEST['cattle_id_src']."'";
					
		
					
						  $sql = 'select  c.* from  calf_info c
						where 1 '.$cattle_id.$status_id_con.' group by c.calf_id order by c.mother_no, c.birth_order, c.calf_id';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				<?php //if($request_date!=""){?>	
                 <tr>
                   <td style="white-space: nowrap;"><?php echo $sl++;?></td>
                   <td style="white-space: nowrap;"><input name="calf_no_<?php echo $data->calf_id;?>" type="text" id="calf_no_<?php echo $data->calf_id;?>" value="<?php echo $data->calf_no;?>"  style="width:100px"   class="form-control"/></td>
                   <td style="white-space: nowrap;">
 <select id="mother_id_<?php echo $data->calf_id;?>" name="mother_id_<?php echo $data->calf_id;?>"  class="form-select"  style="width: 100px;">
                     
<?php foreign_relation($conn,'cattle_info','cattle_id','cattle_no',$data->mother_id,'cattle_id="'.$data->mother_id.'" group by cattle_id order by cattle_type, cattle_sl');?>
                    </select>				   </td>
                   <td style="white-space: nowrap;"><?php echo date('d-M-y',strtotime($data->date_of_birth));?>   				   </td>
                   <td><select id="cattle_type_<?php echo $data->calf_id;?>" name="cattle_type_<?php echo $data->calf_id;?>"  class="form-select"  style="width: 120px;">
                     <option value="0"></option>
                     <?php foreign_relation($conn,'cattle_type','id','cattle_type',$data->cattle_type,'id in (3,4) ');?>
                   </select></td>
                   <td>
				   <select id="gender_<?php echo $data->calf_id;?>" name="gender_<?php echo $data->calf_id;?>"  class="form-select"  style="width: 100px;">
                     <option value="0"></option>
                     <?php foreign_relation($conn,'gender','id','gender',$data->gender,'1');?>
                   </select></td>
                   <td><select id="breed_<?php echo $data->calf_id;?>" name="breed_<?php echo $data->calf_id;?>"  class="form-select"  style="width: 120px;">
                     <option value="0"></option>
                     <?php foreign_relation($conn,'cattle_breed','id','cattle_breed',$data->breed,'1');?>
                   </select></td>
                   <td style="white-space: nowrap;">
				   <select id="status_id_<?php echo $data->calf_id;?>" name="status_id_<?php echo $data->calf_id;?>"  class="form-select"  style="width: 120px;">
                    
                     <?php foreign_relation($conn,'calf_status','id','calf_status',$data->status_id,'1 order by id');?>
                   </select></td>
                   <td>
				   <div align="center">
				  <input name="status_<?php echo $data->calf_id;?>" type="text" id="status_<?php echo $data->calf_id;?>"
				   value="<?php echo $data->status; ?>" style="width:150px" class="form-control"/>
		           </div></td>
                   <td><span id="divi_<?php echo $data->id?>">
			  <input name="flag_<?php echo $data->id?>" type="hidden" id="flag_<?php echo $data->id?>" value="1" />
			  <input class="form-control" type="button" name="Button" value="Edit"  onclick="update_value(<?php echo $data->id?>)" style="width:80px; height:30px; background-color: #FF3366; font-weight:700; font-size:12px;"/>
			  
          </span></td>
                 </tr>
                 
                 
				  <?php } //}?>
				  
				  <!--<tr style="font-size:16px;">
                   <td>&nbsp;</td>
                   <td>&nbsp;</td>
                   <td><div align="center"><strong>Total</strong></div></td>
                   <td>&nbsp;</td>
                   <td><div align="center"><strong><?php echo number_format($tot_morning_qty,2);?></strong></div></td>
                   <td>&nbsp;</td>
                 </tr>-->
                </tbody>
              </table>
			  
			
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        
      </div>
    </section>

  </main><!-- End #main -->

 <!-- ======= Footer ======= -->
 <?php require_once('inc_files/inc_footer.php'); ?>
<!-- ======= Footer ======= -->
<?php selected_two("#item_id"); ?>
<?php selected_two("#cattle_id"); ?>
<?php selected_two("#mother_id"); ?>
<?php selected_two("#cattle_id_src"); ?>
<script>
var printCounter = 0;
 
new DataTable('#example', {
    caption: "&copy; Copyright Five Star Farm House Limited. All Rights Reserved",
    layout: {
        topStart: {
            buttons: [
                'copy',
                {
                    extend: 'excel',
                    messageTop:
                        'The information in this table is copyright to Five Star Farm House Limited.'
                },
                {
                    extend: 'pdf',
                    //messageBottom: null
					messageBottom: "Copyright Five Star Farm House Limited. All Rights Reserved"
                },
                {
                    extend: 'print',
                    messageTop: function () {
                        printCounter++;
 
                        if (printCounter === 1) {
                            return 'This is the first time you have printed this document.';
                        }
                        else {
                            return (
                                'You have printed this document ' + printCounter + ' times'
                            );
                        }
                    },
                    messageBottom: "&copy; Copyright Five Star Farm House Limited. All Rights Reserved"
                }
            ]
        }
    }
});
</script>

<!--<script type="text/javascript">
$(document).ready(function() {
    // Check if DataTable is already initialized
    if ($.fn.DataTable.isDataTable('#example')) {
        // If DataTable is already initialized, destroy it
        $('#example').DataTable().destroy();
    }
    
    // Reinitialize DataTable with new options
    $('#example').dataTable({
        'iDisplayLength': 50
    });
});
</script>-->


</body>

</html>