<!DOCTYPE html>
<html>
<head>
   <?php include "comman/code_css.php"; ?>
</head>

<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">

<?php include "sidebar.php"; ?>

<div class="content-wrapper">

<section class="content-header">
   <h1><?=$page_title;?></h1>
   <ol class="breadcrumb">
      <li><a href="<?=$base_url;?>dashboard"><i class="fa fa-dashboard"></i>Home</a></li>
      <li class="active"><?=$page_title;?></li>
   </ol>
</section>

<section class="content">
<div class="row">
<div class="col-md-12">

<div class="box box-primary">
<div class="box-header with-border">
   <h3 class="box-title">Please Enter Valid Information</h3>
</div>

<form class="form-horizontal" id="report-form" onkeypress="return event.keyCode != 13;">
<input type="hidden" name="<?=$this->security->get_csrf_token_name();?>" value="<?=$this->security->get_csrf_hash();?>">
<input type="hidden" id="base_url" value="<?=$base_url;?>">

<div class="box-body">

<div class="form-group">

<?php
if(store_module() && is_admin()){
   $this->load->view('store/store_code',[
      'show_store_select_box'=>true,
      'store_id'=>get_current_store_id(),
      'div_length'=>'col-sm-3',
      'show_all'=>'true',
      'form_group_remove'=>'true'
   ]);
}else{
   echo "<input type='hidden' name='store_id' id='store_id' value='".get_current_store_id()."'>";
}
?>

<label class="col-sm-2 control-label"><?=$this->lang->line('category_name');?></label>
<div class="col-sm-3">
<select class="form-control select2" id="category_id">
   <option value="">-All-</option>
   <?=get_expense_category_select_list(null,get_current_store_id());?>
</select>
</div>

</div>

<div class="form-group">

<label class="col-sm-2 control-label"><?=$this->lang->line('from_date');?></label>
<div class="col-sm-3">
<div class="input-group date">
<div class="input-group-addon"><i class="fa fa-calendar"></i></div>
<input type="text" class="form-control datepicker" id="from_date" value="<?=show_date(date('d-m-Y'));?>">
</div>
</div>

<label class="col-sm-2 control-label"><?=$this->lang->line('to_date');?></label>
<div class="col-sm-3">
<div class="input-group date">
<div class="input-group-addon"><i class="fa fa-calendar"></i></div>
<input type="text" class="form-control datepicker" id="to_date" value="<?=show_date(date('d-m-Y'));?>">
</div>
</div>

</div>

</div>

<div class="box-footer text-center">
<button type="button" id="view" class="btn btn-success">Show</button>
<button type="button" id="print_a4" class="btn btn-info" style="display:none;">Print A4</button>
<a href="<?=base_url('dashboard');?>"><button type="button" class="btn btn-warning">Close</button></a>
</div>

</form>

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

<!-- TABLE -->
<section class="content">
<div class="row">
<div class="col-md-12">

<div class="box">
<div class="box-header">
   <h3 class="box-title">Records Table</h3>

   <?php $this->load->view('components/export_btn',['tableId'=>'report-data']); ?>
</div>

<div class="box-body table-responsive no-padding">

<table class="table table-bordered table-hover" id="report-data">

<thead>
<tr class="bg-blue">
<th>#</th>
<?php if(store_module() && is_admin()){ ?>
<th><?=$this->lang->line('store_name');?></th>
<?php } ?>
<th><?=$this->lang->line('expence_code');?></th>
<th><?=$this->lang->line('expence_date');?></th>
<th><?=$this->lang->line('category_name');?></th>
<th><?=$this->lang->line('reference_no');?></th>
<th><?=$this->lang->line('expense_for');?></th>
<th><?=$this->lang->line('amount');?></th>
<th><?=$this->lang->line('note');?></th>
<th><?=$this->lang->line('created_by');?></th>
</tr>
</thead>

<tbody id="tbodyid"></tbody>

</table>

</div>
</div>

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

</div>

<?php include "footer.php"; ?>
<div class="control-sidebar-bg"></div>
</div>

<?php include "comman/code_js_sound.php"; ?>
<?php include "comman/code_js.php"; ?>
<?php include "comman/code_js_export.php"; ?>

<?php
// ✅ GET COMPANY NAME DYNAMICALLY
$company_name = "";
if(function_exists('get_company_name')){
   $company_name = get_company_name();
}
if(empty($company_name)){
   $company_name = $this->db->query("SELECT site_name FROM db_sitesettings LIMIT 1")->row()->site_name ?? "Company Name";
}
?>

<script>
var base_url = $("#base_url").val();
var company_name = "<?=$company_name;?>";

// Load categories
$("#store_id").on("change",function(){
   $.post(base_url+"expense/get_expense_category_select_list",
   {store_id:$(this).val()},
   function(res){
      $("#category_id").html('<option value="">All</option>'+res).select2();
   });
});

// SHOW DATA
$("#view").click(function(){

let from_date=$("#from_date").val();
let to_date=$("#to_date").val();

if(!from_date){ toastr.warning("Select From Date"); return; }
if(!to_date){ toastr.warning("Select To Date"); return; }

$(".box").append('<div class="overlay"><i class="fa fa-refresh fa-spin"></i></div>');

$.post(base_url+"reports/show_expense_report",{
   from_date:from_date,
   to_date:to_date,
   category_id:$("#category_id").val(),
   store_id:$("#store_id").val()
},function(result){

   $("#tbodyid").html(result);
   $(".overlay").remove();
   $("#print_a4").show();

}).fail(function(){
   $(".overlay").remove();
   toastr.error("Failed to load data");
});

});

// PRINT A4 (✅ FIXED - NO HARDCODE)
$("#print_a4").click(function(){

let win = window.open('', '', 'width=900,height=700');

win.document.write(`
<html>
<head>
<title>Expense Report</title>
<style>
body{font-family:Arial;font-size:12px;}
table{width:100%;border-collapse:collapse;}
th,td{border:1px solid #000;padding:5px;text-align:left;}
th{background:#eee;}
.header{text-align:center;margin-bottom:10px;}
.company{font-size:18px;font-weight:bold;}
</style>
</head>
<body>

<div class="header">
   <div class="company">${company_name}</div>
   <div><b>Expense Report</b></div>
   <div><b>From:</b> ${$("#from_date").val()} <b>To:</b> ${$("#to_date").val()}</div>
</div>

${document.getElementById("report-data").outerHTML}

</body>
</html>
`);

win.document.close();
win.print();

});

</script>

<script>
$(".<?=basename(__FILE__,'.php');?>-active-li").addClass("active");
</script>

</body>
</html>