<?php 
include "includes/header.php"; 
include "includes/Mdme-functions.php";
$mdme = getMdmeProduct_flowEntry($db,'');	
include_once "includes/pagepermission.php";
include_once "common/dpselect-functions.php";
//check permission - START
if(!($res_modm_prm)){
	header("Location:".admin_public_url."error.php");
}
else if(trim($res_modm_prm['ViewPrm'])=="0") {
	header("Location:".admin_public_url."error.php");
}
//check permission - END
?> 

<div class="wrapper">
    <div class="container">        
		<!-- Page-Title - START -->
        <div class="row">
            <div class="col-sm-5">										
                <h4 class="page-title"><?php echo $operation; ?> Weavers Report</h4>						
            </div>
        </div>


        <!-- Page-Title - END -->

        <div class="row">
			<div class="col-sm-12">
				<form id="jvalidate" name="frmReturn" role="form" class="form-horizontal" action="#" method="post" enctype="multipart/form-data" >
				
					<div class="card-box ">				
					<div class="row">
							<div class="col-sm-2">
							<div class="form-group">
									<label>Financial Year<span class="text-danger">*</span></label>							
									<?php 
									echo getSelectBoxFinancialYearlist($db, 'financial_id', 'jsrequired');
									?>
								</div>
							</div>

							<div class="col-sm-4">
							<div class="form-group">
									<label>Select Supplier<span class="text-danger">*</span></label>							
                                    <select  name="sel_supplier" id="sel_supplier" class="form-control select2 jsrequired" <?php echo $disabled ; ?>>
										<option value="">All Supplier</option>
										<?php 
										$rslt_list = $db->get_rsltset("select sup_id AS Id,sup_name AS Name from m_supplier where IsActive = '1' order by sup_name asc");
										foreach($rslt_list as $r){
											$sel='';
											if($res_ed['sup_id']==$r['Id'])
												$sel=' selected="selected" ';
										?>
										<option value="<?php echo $r['Id']; ?>" <?php echo $sel; ?>><?php echo $r['Name']; ?></option>
										<?php	
										}
										?>
									</select>
									
								</div>	
							</div>

							<div class="col-sm-2"style="padding-top: 6px;">
							<div class="form-group">
										</br>	
									<input type="text" name="txtsrch_fmdate" id="txtsrch_fmdate" placeholder="Start Date" class="form-control" />						
								</div>
								</div>
							<div class="col-sm-2"style="padding-top: 6px;">
							<div class="form-group">
										</br>	
									<input type="text" name="txtsrch_todate" id="txtsrch_todate" placeholder="End Date" class="form-control" />						
								</div>
								</div>

							<div class="col-sm-2"style="padding-top: 6px;">
							<div class="form-group">
										</br>						
									<button class="btn btn-primary waves-effect waves-light pull-right" type="submit"><span id="Submit">Submit</span></button>
									
								</div>	

							
							</div>
						<br/>
						<div class="col-sm-12 text-right"> <!-- Add 'text-right' class here -->
					<br/>
					
				</div>
						<div class="row">
							<div class="col-md-12">
								<div class="table-responsive challan-table">
									<table class="table table-bordered table-hover" id="tbl_payment_report">
										
								<tbody id="tbl_body_report_det">	
												
								</tbody>				
								
							</table>
						</div>
					</div>
					
				</form>
            </div>
        </div> <!-- end row -->	
		<div class="row">
            <div class="col-xs-12">
                <div class="card-box">    
					
				
                    <div class="panel-body" style="display:none;"  id="pdf-iframe">
						<!-- <div id="chartdiv"></div> -->
                        <div id="loader_pie" style="position: relative; left: 600px;  display:none; height:500px;">
                            <img src="https://i.imgur.com/fXUIBfi.gif" alt="Chart will Render Here..." style="height: 85px; " />
                        </div>
						
						
                        <iframe   src="../admin/weavers_report_pdf.php?financial_id=<?php echo $_REQUEST['financial_id'];?>&sel_supplier=<?php echo $_REQUEST['sel_supplier'];?> " width="100%" style="min-height: 768px;overflow-y: scroll;"></iframe>

                    </div>

                    <div class="panel-body" id="pdf-container">
						<iframe src="../admin/weavers_report_pdf.php?financial_id=<?php echo $_REQUEST['financial_id'];?>" width="100%" style="min-height: 768px; overflow-y: scroll;"></iframe>
					</div>
                </div>
            </div>
        </div>

<script src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
<?php include "includes/footer.php";  ?> 
	 </div> <!-- container -->
</div> <!-- End wrapper -->

<?php include "includes/footer_script.php";  ?> 

<!-- Autocomplete -->
<script type="text/javascript" src="assets/plugins/autocomplete/jquery.autocomplete.min.js"></script>

<script>

$(document).ready(function(){
   $(".select2").select2({
      width: '100%', 
   }); 
});

function get_date() {
    $('#financial_id').change(function () {
		var financial_id = $(this).val();
		// console.log('Financial ID:', financial_id);
        var selectedOption = $(this).find(':selected');
		// console.log('Selected Option:', selectedOption);
        var startDate = selectedOption.attr('data-start-date');
        var endDate = selectedOption.attr('data-end-date');

        // Set the start and end date attributes for validation
        $('#txtsrch_fmdate').attr('data-start-date', startDate);
        $('#txtsrch_todate').attr('data-end-date', endDate);

        // Set the datepicker options for the from and to date fields
        $('#txtsrch_fmdate').datepicker('setStartDate', startDate);
        $('#txtsrch_fmdate').datepicker('setEndDate', endDate);
        $('#txtsrch_todate').datepicker('setStartDate', startDate);
        $('#txtsrch_todate').datepicker('setEndDate', endDate);

        // Set the selected start and end dates in the input fields
        $('#txtsrch_fmdate').val(startDate);
        $('#txtsrch_todate').val(endDate);

        console.log('Start Date:', startDate);
        console.log('End Date:', endDate);
    });

    $('#txtsrch_fmdate').datepicker({
        format: 'yyyy-mm-dd',
        // Use the data attribute for minimum date
        startDate: $('#txtsrch_fmdate').attr('data-start-date'),
        // Use the data attribute for maximum date
        endDate: $('#txtsrch_todate').attr('data-end-date'),
    }).on('changeDate', function (ev) {
        var selectedStartDate = new Date($('#txtsrch_fmdate').val());
        $('#txtsrch_todate').datepicker('setStartDate', selectedStartDate);
    });

    $('#txtsrch_todate').datepicker({
        format: 'yyyy-mm-dd',
        // Use the data attribute for minimum date
        startDate: $('#txtsrch_fmdate').attr('data-start-date'),
        // Use the data attribute for maximum date
        endDate: $('#txtsrch_todate').attr('data-end-date'),
    }).on('changeDate', function (ev) {
        var selectedEndDate = new Date($('#txtsrch_todate').val());
        $('#txtsrch_fmdate').datepicker('setEndDate', selectedEndDate);
    });

    $('#txtsrch_fmdate, #txtsrch_todate').bind('keyup keydown keypress', function (evt) {
        return false;
    });

  
}


$(document).ready(function() {
	get_date();
	function getTodayDate() {
        var today = new Date();
        var dd = String(today.getDate()).padStart(2, '0');
        var mm = String(today.getMonth() + 1).padStart(2, '0'); // January is 0!
        var yyyy = today.getFullYear();

        return yyyy + '-' + mm + '-' + dd;
        var formattedDate = dd + '-' + mm + '-' + yyyy;
        $('#txtsrch_fmdate').val(formattedDate);
    $('#txtsrch_todate').val(formattedDate);

    }

    var financial_id = $('#financial_id').val();
	var todayDate = getTodayDate();
    var pdfUrl = "../admin/weavers_report_pdf.php?financial_id=" + financial_id + "&frmdate=" + todayDate + "&todate=" + todayDate;
    $('#pdf-container iframe').attr('src', pdfUrl);
});

$(document).ready(function() {
    // Show pdf-all on page load
    $('#pdf-container').show();

    // Hide pdf-iframe on page load
    $('#pdf-iframe').hide();

    var financial_id = $('#financial_id').val();
	var FromDate = $('#txtsrch_fmdate').val();
	var ToDate = $('#txtsrch_todate').val();
    console.log('Financial ID on page load:', financial_id);
  
        var today = new Date();
        var dd = String(today.getDate()).padStart(2, '0');
        var mm = String(today.getMonth() + 1).padStart(2, '0'); // January is 0!
        var yyyy = today.getFullYear();


        var formattedDate = dd + '-' + mm + '-' + yyyy;
        $('#txtsrch_fmdate').val(formattedDate);
    $('#txtsrch_todate').val(formattedDate);


    

    // Construct the URL for the PDF download
    var pdfDownloadUrl = "../admin/weavers_report_pdf.php?financial_id=" + financial_id + "&sel_supplier=1";

    // Redirect to the PDF download URL
    // window.location.href = pdfDownloadUrl;

    // Attach a submit event handler to the form
    $('#jvalidate').submit(function (e) {
        e.preventDefault();

        var sel_supplier = $('#sel_supplier').val();
		var financial_id = $('#financial_id').val();
		var FromDate = $('#txtsrch_fmdate').val();
		var ToDate = $('#txtsrch_todate').val();
        
console.log(FromDate);
        // Construct the URL for the iframe
        var iframeUrl = "../admin/weavers_report_pdf.php?financial_id=" + financial_id + "&sel_supplier=" + sel_supplier+ "&frmdate=" + FromDate + "&todate=" + ToDate;

        // Show loader
        $('#loader_pie').show();

        // Hide pdf-all
        $('#pdf-container').hide();

        // Show pdf-iframe
        $('#pdf-iframe').show();

        // Set iframe src
        $('#pdf-iframe iframe').attr('src', iframeUrl);

        // Hide loader after 500ms
        setTimeout(function () {
            $('#loader_pie').hide();
        }, 500);
    });

    // Attach a click event handler to the download icon
    $('#downloadIcon').click(function() {
        // Get financial ID
        var financial_id = $('#financial_id').val();
        var sel_supplier = $('#sel_supplier').val();


        // Create a URL with query parameters for the selected values
        var pdfDownloadUrl = "../admin/weavers_report_pdf.php?financial_id=" + financial_id + "&sel_supplier=" + sel_supplier;
        
        // Redirect to the PDF generation page
        window.location.href = pdfDownloadUrl;
    });
});


</script>



