<?php 
include "includes/header.php"; 
include "includes/Mdme-functions.php";
$mdme = getMdme_Supplier_outstanding_Report($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
$Srch_FilterOptions = array( array('ID' => 'supplier_name', 'Name' => 'Supplier Name'), array('ID' => 'bill_no', 'Name' => 'Bill No'));
?> 

<div class="wrapper">
    <div class="container">        
		<!-- Page-Title - START -->
        <div class="row">
            <div class="col-sm-5">										
                <h4 class="page-title"><?php echo $operation; ?> Supplier Outstanding</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-4">
							<div class="form-group">
									<label>Financial Year<span class="text-danger">*</span></label>							
									<select  name="financial_id" id="financial_id" class="form-control select2 jsrequired">
										<option value="">Select</option>
										<?php 
										$rslt_fy_list = $db->get_rsltset("select financial_id,f_start_date,f_end_date from m_financial_year order by f_start_date asc");
										$current_fy_id = '';
										
										foreach($rslt_fy_list as $r){
											$financial_year_label = date('Y',strtotime($r['f_start_date'])).'-'.date('Y',strtotime($r['f_end_date']));
											if($act=='update')
												$sel= ($res_ed['financial_id']==$r['financial_id'])?' selected="selected" ':'';
											else
												$sel= ($current_fy_id==$r['financial_id'])?' selected="selected" ':'selected="selected"';
										?>
										<option value="<?php echo $r['financial_id']; ?>" <?php echo $sel; ?>><?php echo $financial_year_label; ?></option>
										<?php	
										}
										?>
									</select>
								</div>
							</div>

							<div class="col-sm-4">
							<div class="form-group">
									<label>Select Company<span class="text-danger">*</span></label>							
                                    <select  name="sel_company" id="sel_company" class="form-control select2 jsrequired" <?php echo $disabled ; ?>>
										<option value="">All Company</option>
										<?php 
										$rslt_list = $db->get_rsltset("select company_id AS Id,company_name AS Name from m_company where IsActive = '1' order by company_name asc");
										foreach($rslt_list as $r){
											$sel='';
											if($res_ed['company_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-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">
									<option value="">All Supplier</option>
										<?php 
										$rslt_list = $db->get_rsltset("select m_supplier.*, sup_id AS Id,sup_name AS Name from m_supplier where IsActive = '1' and sup_product = '2'  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>
						<br/>
						<div class="col-sm-12 text-right"> <!-- Add 'text-right' class here -->
					<br/>
					<button class="btn btn-primary waves-effect waves-light pull-right" type="submit"><span id="Submit">Submit</span></button>
				</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/supplier_outstanding_pdf.php?sel_supplier=<?php echo $_REQUEST['sel_supplier'];?>&financial_id=<?php echo $_REQUEST['financial_id'];?>&sel_company=<?php echo $_REQUEST['sel_company'];?> " width="100%" style="min-height: 768px;overflow-y: scroll;"></iframe>

                    </div>

					<div class="panel-body" id="pdf-container">
						<iframe src="../admin/supplier_outstanding_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%', 
   }); 
});

$(document).ready(function() {
    var financial_id = $('#financial_id').val();
    var pdfUrl = "../admin/supplier_outstanding_pdf.php?financial_id=" + financial_id;
    $('#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();
    console.log('Financial ID on page load:', financial_id);

    // Construct the URL for the PDF download
    var pdfDownloadUrl = "../admin/supplier_outstanding_pdf.php?financial_id=" + financial_id;

    // 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_company = $('#sel_company').val();
        var sel_supplier = $('#sel_supplier').val();

        // Construct the URL for the iframe
        var iframeUrl = "../admin/supplier_outstanding_pdf.php?sel_supplier=" + sel_supplier + "&financial_id=" + financial_id + "&sel_company=" + sel_company;

        // 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_company = $('#sel_company').val();
        var sel_supplier = $('#sel_supplier').val();

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


</script>



