<?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="Cattle Information";
$page="1001_cattle_info.php?active_id=".$active_id."&menu_id=".$menu_id."";
$table='cattle_info';	
$unique='cattle_id';

$request_id=$_REQUEST['cattle_id'];

$ms_data=mysqli_fetch_object(mysqli_query($conn,'select * from '.$table.' where cattle_id="'.$request_id.'"'));

// End Common Information

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

{		

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

$cattle_no = $_POST['cattle_no'];
$cattle_type = $_POST['cattle_type'];
$cattle_name = '';
$shed_no = $_POST['shed_no'];

if($cattle_type=2){
$cattle_sl_no=2000000;
}else{
$cattle_sl_no=1000000;
}

$cattle_sl = $cattle_sl_no+$cattle_no;

$ins_sql = 'INSERT INTO cattle_info (cattle_no, cattle_sl, cattle_name, cattle_type, shed_no, entry_by, entry_at, edit_by, edit_at) VALUES

("'.$cattle_no.'", "'.$cattle_sl.'", "'.$cattle_name.'", "'.$cattle_type.'",  "'.$shed_no.'", "'.$_POST['entry_by'].'", "'.$_POST['entry_at'].'", 
"0", "0000-00-00")';
	
mysqli_query($conn,$ins_sql);


header("location:".$page);

?>

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

<?php

}

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

{		

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

$cattle_no = $_POST['cattle_no'];
$shed_no = $_POST['shed_no'];
$cattle_sl_no=1000000;
$cattle_sl = $cattle_sl_no+$cattle_no;

$up_id=$request_id;


$up_sql = 'update cattle_info set cattle_no="'.$cattle_no.'", shed_no="'.$shed_no.'", cattle_sl="'.$cattle_sl.'", edit_by="'.$_POST['entry_by'].'", edit_at="'.$_POST['entry_at'].'"  where cattle_id='.$up_id;
mysqli_query($conn,$up_sql);

header("location:".$page);
?>

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

<?php

}

?>


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

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

<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-8"> 

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

              <!-- Table with stripped rows -->
              <table class="table datatable"  style="font-size:14px;">
                <thead>
                  <tr>
                    <th  scope="col"><span class="card-title">Sl</span></th>
                    <th  scope="col"><span class="card-title">Cattle No</span></th>
                    <th  scope="col"><span class="card-title">Shed No</span></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 * from '.$table.' where 1';
						$query = mysqli_query($conn,$sql);
						while ($data = mysqli_fetch_object($query)) {

					?>
				
                 <tr>
                  <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->cattle_id;?>" style="color:#000000"><?php echo $data->cattle_id;?></a></td>
                  <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->cattle_id;?>" style="color:#000000"><?php echo $data->cattle_no;?></a></td>
                  <td><a href="<?php echo $page;?>&<?php echo $unique;?>=<?php echo $data->cattle_id;?>" style="color:#000000"><?php echo $shed_name[$data->shed_no]?></a></td>
                  </tr>
				  <?php }?>
                </tbody>
              </table>
              <!-- End Table with stripped rows -->

            </div>
          </div>

        </div>

        <div class="col-lg-4">

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

              <!-- Multi Columns Form -->
              <form class="row g-3" action="" method="post" name="form1" id="form1" onSubmit="return check()">
                <div class="col-md-12">
                  <label for="inputName5" class="form-label">Cattle No</label>
                   <input type="text" class="form-control" id="cattle_no" name="cattle_no" value="<?php if($request_id>0){ echo $ms_data->cattle_no; } ?>" required>
                </div>
				
				<div class="col-12">
                  <label for="inputAddress2" class="form-label">Cattle Type</label>
                 
		<?php if($request_id>0){?>
		<select id="cattle_type" name="cattle_type"  required class="form-select"    >
		  
			
			<?php foreign_relation($conn,'cattle_type','id','cattle_type',$ms_data->cattle_type,'1 order by id');?>
        	</select>
			
			<?php } else {?>
		<select id="cattle_type" name="cattle_type"  required class="form-select"    >
		  <option></option>
			
			<?php foreign_relation($conn,'cattle_type','id','cattle_type',$_POST['cattle_type'],'1 order by id');?>
        	</select>
			
			<?php }?>
                </div>
				
				
				<div class="col-12">
                  <label for="inputAddress2" class="form-label">Shed No</label>
                 
		<?php if($request_id>0){?>
		<select id="shed_no" name="shed_no"  required class="form-select"    >
		  
			
			<?php foreign_relation($conn,'shed_info','id','shed_name',$ms_data->shed_no,'1 order by id');?>
        	</select>
			
			<?php } else {?>
		<select id="shed_no" name="shed_no"  required class="form-select"    >
		  <option></option>
			
			<?php foreign_relation($conn,'shed_info','id','shed_name',$_POST['shed_no'],'1 order by id');?>
        	</select>
			
			<?php }?>
                </div>
				
				
                
            
                
                
                <div class="text-center">
				<?php if($request_id>0) {?>
                 <button type="submit" class="btn btn-success btn-sm" name="update" id="update" ><i class="bi bi-check-all"></i> Update &nbsp;</button>
				 <?php }else {?>
				 <button type="submit" class="btn btn-primary btn-sm" name="insert" id="insert" ><i class="bi bi-plus"></i> Save &amp; New &nbsp;</button>
				 <?php }?>
                  <button type="reset" class="btn btn-secondary btn-sm" onClick="parent.location='<?php echo $page?>'" >Reset</button>
                </div>
              </form><!-- End Multi Columns Form -->

            </div>
          </div>

          

          

        </div>
      </div>
    </section>

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

<!-- ======= Footer ======= -->
 <?php require_once('inc_files/inc_footer.php'); ?>
<!-- ======= Footer ======= -->

</body>

</html>