<?php
require ROOT . "/pages/start.php"; ?>
<style>
    #upload_file {
        display: none;
    }
</style>
<?php
require ROOT . "/pages/header.php";
?>

<main id="main" class="main p-3">
    <section class="section">
        <div class="row">
            <div class="col">
                <h3>ТАНХИМЫН БУС ЦАГИЙН ТАЙЛАН ХЭВЛЭХ</h3>
            </div>
            <div class="col-md-2">
                <select class="form form-control mb-3" id="year">
                    <?php
                    $currenton = $thison;
                    while ($currenton >= $starton) { ?>
                        <option <?php echo $currenton == $thison ? "selected" : "" ?>><?= $currenton ?></option>
                    <?php $currenton--;
                    } ?>
                </select>
            </div>
            <div class="col-md-1">
                <select class="form form-control mb-3" id="month">
                    <?php
                    $sar = 1;
                    while ($sar <= 12) { ?>
                        <option <?php echo $sar == $thismonth ? "selected" : "" ?>><?= $sar ?></option>
                    <?php $sar++;
                    } ?>
                </select>
            </div>
            <div class="col-md-1">
                <button class="btn btn-danger w-100" onclick="getreport()">ХАРАХ</button>
            </div>
            <div class="col-md-1">
                <button class="btn btn-primary w-100" onclick="printdiv('data')">ХЭВЛЭХ</button>
            </div>
            <div class="col-md-1">
                <button class="btn btn-success w-100" onclick="exportToExcel('data')">EXCEL</button>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12" id="content">

            </div>
        </div>
    </section>
</main><!-- End #main -->

<?php
require ROOT . "/pages/footer.php"; ?>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script>
    function exportToExcel(tableId, name="Танхимын бус цагийн нэгтгэл"){
        	let tableData = document.getElementById(tableId).outerHTML;
        	tableData = tableData.replace(/<A[^>]*>|<\/A>/g, ""); //remove if u want links in your table
            tableData = tableData.replace(/<input[^>]*>|<\/input>/gi, ""); //remove input params
        
        	let a = document.createElement('a');
        	a.href = `data:application/vnd.ms-excel, ${encodeURIComponent(tableData)}`
        	a.download = 'Танхимын бус цагийн нэгтгэл' + '.xls'
        	a.click()
    }
    function printdiv(printdivname) {
        var h = document.head;
        var headstr = "<html><body>";
        var footstr = "</body>";
        var newstr = document.getElementById(printdivname).innerHTML;
        var oldstr = document.body.innerHTML;
        document.body.innerHTML = headstr + newstr + footstr;
        window.print();
        document.body.innerHTML = oldstr;
        return false;
    }

    function getreport() {
        $.ajax({
            url: "report_ajax",
            type: "POST",
            data: {
                mode: 1,
                year: $('#year').val(),
                month: $('#month').val()
            },
            error: function(xhr, textStatus, errorThrown) {
                $('#content').html("Алдаа гарлаа");
            },
            beforeSend: function() {
                $('#content').html("Түр хүлээнэ үү");
            },
            success: function(data) {
                $('#content').html(data);
            },
            async: true
        });
    }
</script>
<?php
require ROOT . "/pages/end.php";
?>