<?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'];

 // Common Information

$page_title="Milk Record (MIS)";



?>




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

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



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 update_value(id)
	{

var order_no=id; // Rent

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

var strURL="1002_cattle_milk_record_mis_ajax.php?order_no="+order_no+"&total_unit="+total_unit+"&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>
  
<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> Cattle No</strong></label> 
           <select id="cattle_id" name="cattle_id"  class="form-select"    >
			<option></option>
			
			<?php foreign_relation($conn,'cattle_info','cattle_id','cattle_no',$_POST['cattle_id'],'1 order by cattle_sl');?>
		
        	</select>
		
                </div>
                <div class="col-md-3">
				<label for="first_name" class="form-label"><strong> Shed No</strong></label> 
          <select id="shed_no" name="shed_no"  class="form-select"    >
			<option></option>
			<?php foreign_relation($conn,'shed_info','id','shed_name',$_POST['shed_no'],'1');?>
		
        	</select>
                </div>
				<div class="col-md-2">
				 <button type="submit" class="btn btn-success rounded-pill btn-sm">&nbsp;<i class="bi bi-filter me-1"></i> Run Report&nbsp;&nbsp;</button>
				</div>
				<div class="col-md-2" align="right">
				<a href="1002_milk_receive.php?active_id=1002&menu_id=10"><button type="button" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg me-1"></i> New Entry&nbsp;&nbsp;&nbsp;</button></a>
				</div>
                
              </form><!-- End No Labels Form -->

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

          <div class="card">
            <div class="card-body">
			<br>
			

              <!-- Table with stripped rows -->
              <table class="table table-sm" style="font-size:12px;" id="example">
                <thead>
                  <tr style="text-transform:uppercase; font-size:12px; color:#012970; border-top: 1px solid #CCCCCC; background:#f6f9ff; height:32px;">
                    <th width="15%" scope="col">DATE</th>
                    <th width="15%" scope="col">Shed</th>
                    <th width="20%" scope="col" ><div align="center">Cattle No </div></th>
                    <th width="20%" scope="col">Shift </th>
                    <th width="20%" scope="col">Quantity</th>
                    <th width="10%" scope="col">Action</th>
                  </tr>
                </thead>
                <tbody>
		<?php
		
		
		$sql = "select id, shed_name from shed_info where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $shed_name[$info->id]=$info->shed_name;
		}
		
		$sql = "select id, milk_schedule from milk_schedule where 1 group by id";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $milk_schedule[$info->id]=$info->milk_schedule;
		}
		
		$sql = "select tr_date, cattle_id, milk_schedule, total_unit from milk_record where 1 group by  tr_date, cattle_id, milk_schedule";
		 $query = mysqli_query($conn,$sql);
		 while($info=mysqli_fetch_object($query)){
  		 $total_unit[$info->tr_date][$info->cattle_id][$info->milk_schedule]=$info->total_unit;
		}
		
		
		if($_POST['tr_date']!=''){
		$date_con .= ' and m.tr_date="'.$_REQUEST['tr_date'].'"';
		}else{
		$date_con .= ' and m.tr_date="'.date('Y-m-d').'"';
		}
					
					if($_POST['cattle_id']!='')
					$cattle_id_con=" and m.cattle_id='".$_POST['cattle_id']."'";
					
					if($_POST['shed_no']!='')
					$shed_no_con=" and m.shed_no='".$_POST['shed_no']."'";
					
					
					
						 $sql = 'select m.*, c.cattle_no from milk_record m, cattle_info c 
						where m.cattle_id=c.cattle_id '.$date_con.$cattle_id_con.$shed_no_con.' group by m.id order by m.cattle_id, m.id desc';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                 <tr>
                   <td><?php echo date('d-M-Y',strtotime($data->tr_date));?></td>
                   <td><?php echo $shed_name[$data->shed_no];?></td>
                   <td><div align="center"><?php echo $data->cattle_no;?></div></td>
                   <td><?php echo $milk_schedule[$data->milk_schedule];?></td>
                   <td>
				   <input name="total_unit_<?php echo $data->id?>" type="text" id="total_unit_<?php echo $data->id?>" value="<?php echo $data->total_unit; ?>"  
				   style="width:100px" class="form-control"/>
				   </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:80%; height:30px; background-color:#FF3366; font-weight:700; font-size:12px;"/>
          </span>
				   </td>
                 </tr>
                 
				  <?php }?>
				  
				  
                </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("#cattle_id"); ?>
<!--<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>