<?php 
session_start();
$title = "Power Supply";
$group = "cms";
$menu = "power_supply";
include('header.php');

$status="";
$date = date('d-m-Y');
$batchid = "";
if(isset($_SESSION['batchid']))
{
    $batchid = $_SESSION['batchid'];
}
else if(isset($_POST['batchid']))
{
    $batchid = $_POST['batchid'];
}
   // $_SESSION['batchid']=$_POST['batchid'];

    $sql="select id,date_created from stats where id=:id";
    $r=$con->select_query($sql,array(':id'=>$batchid));
    foreach($r as $value)
    {
        $date=$value['date_created'];
    }

?>
<script>

function SaveInsert()
{
	var datastring = $("#psform").serialize();	
	var l = $( '.ladda-button-demo1' ).ladda();
	l.ladda( 'start');
	$.ajax({
	            type: "POST",
	            url: "../include/insert_ajax.php",
	            data: datastring,
	            dataType: 'json',
	            cache: false,
	            success: function(data) {
	            	l.ladda( 'stop' );
	            	if(data.success == 1)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.success('Saved successfully');
	                       // $("input[type='text'], textarea, input[type='password'], input[type='number']").val("");
	                    }, 1300);
	            	}
	            	else if(data.success == 3)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('Error, please enter date');

	                    }, 1300);
	            	}
	            },
	            error: function(){
	                  alert('error handling here');
	                  l.ladda( 'stop' );
	            }
	        });
}

function SaveUpdate()
{
	var datastring = $("#psform").serialize();	
	var l = $( '.ladda-button-demo2' ).ladda();
	l.ladda( 'start');
	$.ajax({
	            type: "POST",
	            url: "../include/update_ajax.php",
	            data: datastring,
	            dataType: 'json',
	            cache: false,
	            success: function(data) {
	            	l.ladda( 'stop' );
	            	if(data.success == 1)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.success('Saved successfully');

	                    }, 1300);
	            	}
	            	else if(data.success == 3)
	            	{
	            		setTimeout(function() {
	                        toastr.options = {
	                            closeButton: true,
	                            progressBar: true,
	                            positionClass: "toast-top-full-width",
	                            showMethod: 'slideDown',
	                            timeOut: 4000
	                        };
	                        toastr.error('Error, please enter date');

	                    }, 1300);
	            	}
	            },
	            error: function(){
	                  alert('error handling here');
	                  l.ladda( 'stop' );
	            }
	        });
}


$(document).ready(function(){
	$('#data_5 .input-daterange').datepicker({
	    keyboardNavigation: false,
	    forceParse: false,
	    autoclose: true
	});
})
</script>

<div class="content-body">
        	<div class="row">
                    <div id="recent-transactions" class="col-12">
                        <div class="card">
                            <div class="card-header">
                                <h4 class="card-title">Event List</h4>
                            </div>
                            <div class="card-content">
							<form method="post" action="power_supply" id="psform" class="pform">
                                <div class="row row-pad">
                                    <!-- <div class="col-md-2">
                                        <label for="name">Date Range</label>
                                    </div> -->
                                    <input type="hidden" name="batchid" value="<?php echo $batchid?>"/>
                                    <div class="col-md-9" id="data_5">
                                        <div class="input-daterange input-group" id="datepicker" style="width: 100%">
                                            <input type="text" class="input-sm form-control" name="date" value="<?php echo $date?>"/>
                                        </div>
                                    </div>
                                </div>
                                <?php
                                $count = 0;
                                $new = false;
                                if(!empty($batchid))
                                {
                                    $sql = "select location,generation,date_created from stats where batch_id=:id order by location";
                                    $q = $con->select_query($sql,array(':id'=>$batchid));
                                }
                                else 
                                {
                                    $sql = "select location,generation,date_created from stats where batch_id in (select batch_id from stats group by batch_id order by id DESC) order by location";
                                    $q = $con->select_query($sql);
                                    $new = true;
                                }  
                                
                                    foreach($q as $r)
                                    {
                                        $location = $r['location'];
                                        $generation = $r['generation'];
                                        echo '<div class="row row-pad">
	                                            <div class="col-md-4">
	                                                <input type="text" name="location[]" value="'.$location.'" class="form-control"/>
                                                </div>
                                                <div class="col-md-5">
                                                    <input type="text" name="gen[]" class="form-control" value="'.$generation.'"  placeholder="Generation"/>
                                                </div>	                                            
                                            </div>';
                                        $count++;
                                    }
                                
                                ?>
                                <div class="row row-pad">
                                    <div class="col-md-6">
                                        <input type="hidden" name="insert" value="power_supply"/>
                                        <button type="button" onclick="SaveInsert()" class="ladda-button ladda-button-demo1 btn btn-primary"  data-style="zoom-in">Save</button>
                                        <a href="gen_list" class="btn btn-warning">Cancel</a>
                                    </div>
                                </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <!--/ Recent Transactions -->

   </div>

        
    <div class="wrapper wrapper-content animated fadeInRight">
            <div class="row">
                <div class="col-lg-10">
                     <div class="ibox float-e-margins">
                          <div class="ibox-title">
                                <h5>Power Generation</h5>
                          </div>
                          <div class="ibox-content">
                            
                            
                        </div>
                    </div>
            </div>
        </div>
   </div>
    
    <?php include('footer.php');?>

   
