<?php 
include "includes/header.php"; 
include "includes/Mdme-functions.php";
$mdme = getMdme_Customer_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
?> 

<div class="wrapper">
    <div class="container">        
		<!-- Page-Title - START -->
        <div class="row">
            <div class="col-sm-5">										
                <h4 class="page-title"><?php echo $operation; ?> Sales Register</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 ; ?>>
										
										<?php 
										$rslt_list = $db->get_rsltset("select company_id AS Id,company_name AS Name from m_company where IsActive = '1' order by company_id 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"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/sales_register_pdf.php?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/sales_register_pdf.php?financial_id=<?php echo $_REQUEST['financial_id'];?>&sel_company=1" 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/sales_register_pdf.php?financial_id=" + financial_id + "&sel_company=1";
    $('#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/sales_register_pdf.php?financial_id=" + financial_id + "&sel_company=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_company = $('#sel_company').val();
        var financial_id = $('#financial_id').val();

        // Construct the URL for the iframe
        var iframeUrl = "../admin/sales_register_pdf.php?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();


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


</script>



