<!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">
        <!-- Page Header -->
        <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>

        <!-- Filters -->
        <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">
                                    <!-- Store Selection -->
                                    <?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()."'>";
                                    }
                                    ?>
                                </div>

                                <div class="form-group">
                                    <label for="from_date" 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 pull-right datepicker" id="from_date" name="from_date" value="<?= show_date(date('d-m-Y')); ?>">
                                        </div>
                                    </div>

                                    <label for="to_date" 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 pull-right datepicker" id="to_date" name="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'); ?>" class="btn btn-warning">Close</a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </section>

        <!-- Report Table -->
        <section class="content">
            <div class="row">
                <div class="col-md-12">
                    <div class="box">
                        <div class="box-header">
                            <h3 class="box-title"><?= $this->lang->line('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('invoice_no'); ?></th>
                                        <th><?= $this->lang->line('purchase_date'); ?></th>
                                        <th><?= $this->lang->line('supplier_name'); ?></th>
                                        <th><?= $this->lang->line('tax_number'); ?></th>
                                        <th><?= $this->lang->line('rate'); ?></th>
                                        <th><?= $this->lang->line('discount_amount'); ?></th>
                                        <th><?= $this->lang->line('tax_amount'); ?></th>
                                        <th><?= $this->lang->line('round_off'); ?></th>
                                        <th><?= $this->lang->line('invoice_total'); ?></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>

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

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

// Store details for dynamic header
var store_id = $("#store_id").val() || <?= json_encode(get_current_store_id()); ?>;
var companyName    = <?= json_encode(get_store_name(get_current_store_id())); ?>;
var companyAddress = <?= json_encode(get_store_details(get_current_store_id())->address ?? ""); ?>;
var companyPhone   = <?= json_encode(get_store_details(get_current_store_id())->mobile ?? ""); ?>;
var companyEmail   = <?= json_encode(get_store_details(get_current_store_id())->email ?? ""); ?>;

// Update supplier list when store changes
$("#store_id").on("change", function(){
    store_id = $(this).val();
    $.post(base_url + "purchase/get_suppliers_select_list", {store_id: store_id}, function(result){
        $("#supplier_id").html('<option value="">All</option>' + result).select2();
    });
});

// View report
$("#view").on("click", function(){
    var from_date = $("#from_date").val();
    var 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_purchase_tax_report", {
        from_date: from_date,
        to_date: to_date,
        store_id: $("#store_id").val(),
        warehouse_id: $("#warehouse_id").val()
    }, function(result){
        $("#tbodyid").html(result);
        $(".overlay").remove();
        $("#print_a4").show();
    });
});

// Print report
$("#print_a4").on("click", function(){
    var from_date = $("#from_date").val();
    var to_date   = $("#to_date").val();
    var currentDate = new Date();
    var dateTime = currentDate.toLocaleDateString() + ' ' + currentDate.toLocaleTimeString();

    var printWindow = window.open('', '_blank');
    printWindow.document.write(`
        <!DOCTYPE html>
        <html>
        <head>
            <title>Purchase Tax Report</title>
            <style>
                body { font-family: Arial; font-size:12px; margin:10px; }
                .print-header { text-align:center; margin-bottom:20px; border-bottom:2px solid #000; padding-bottom:10px; }
                .company-name { font-size:18px; font-weight:bold; }
                .report-title { font-size:16px; font-weight:bold; margin-bottom:5px; }
                .report-period, .print-date { font-size:12px; font-weight:bold; }
                table { width:100%; border-collapse:collapse; margin-top:10px; }
                th, td { border:1px solid #000; padding:5px; font-size:11px; }
                th { background:#d0d0d0; }
                .text-right { text-align:right; }
            </style>
        </head>
        <body>
            <div class="print-header">
                <div class="company-name">${companyName}</div>
                <div>${companyAddress}</div>
                <div>${companyPhone}</div>
                <div>${companyEmail}</div>
                <div class="report-title">Purchase Tax Report</div>
                <div class="report-period">From: ${from_date} To: ${to_date}</div>
                <div class="print-date">Generated on: ${dateTime}</div>
            </div>
            <div>${document.getElementById('report-data').outerHTML}</div>
        </body>
        </html>
    `);
    printWindow.document.close();
    printWindow.focus();
    setTimeout(()=>{ printWindow.print(); printWindow.close(); }, 500);
});
</script>

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