<?php if($this->session->flashdata('success')): ?>
    <div class="row">
        <div class="alert alert-block alert-success" style="margin-bottom: 5px">
            <button type="button" class="close" data-dismiss="alert">
                <i class="ace-icon fa fa-times"></i>
            </button>
            <i class="ace-icon fa fa-check green"></i>
            <?php echo $this->session->flashdata('success'); ?>
        </div>
    </div>
<?php endif; ?>
<?php if($this->session->flashdata('error')): ?>
    <div class="row">
        <div class="alert alert-block alert-danger" style="margin-bottom: 5px">
            <button type="button" class="close" data-dismiss="alert">
                <i class="ace-icon fa fa-times"></i>
            </button>
            <i class="ace-icon fa fa-times"></i>
            <?php echo $this->session->flashdata('error'); ?>
        </div>
    </div>
<?php endif; ?>

<div class="row">
    <div class="col-lg-4">
    <select class="form-control" name="input-name" id="input-name">
    <?php foreach ($month_list as $key => $val): ?>
        <option value = "<?php echo $key; ?>"><?php echo $val; ?></option>
    <?php endforeach; ?>
    </select>
    </div>
    <div class="col-lg-1">
        <a href="" id="btn-export">
            <button class="btn btn-sm btn-warning" id="excel"><i class="fa fa-download"></i> Export ra Excel</button>
        </a>
    </div>
</div>
<!-- /.row -->
<div class="space-6"></div>
<div class="row" >
    <div style="text-align: center" id="data-loading">
        <i class="fa fa-refresh fa-spin bigger-200"></i> Đang tải dữ liệu...
    </div>
    <div class="col-md-12">
        <span style="color: red"><i>Lưu ý: BXH từ tháng 04/2020 sử dụng dữ liệu mới (Dữ liệu mới có từ ngày 30/3/2020)</i></span>
    </div>
    <div class="col-lg-12">
        <div id="div-response"></div>
    </div>
</div>

<script type="text/javascript">
    //Thuc hien viec ve bieu do$data
    $(document).ready(function() {
        var url = '<?php echo base_url("backend/cmsRanking/listByMonthlyAjax"); ?>';

        // Tìm kiếm theo cú pháp
        var oldTimeout2 = '';
        $('#input-name').change(function(){
            updateExportLink();
            clearTimeout(oldTimeout2);
            oldTimeout2 = setTimeout(function(){
                loadDataByAjaxFromInput(url);
            }, 250);
        });

        changePagination('0');
        updateExportLink();
    });

    //Ham chung cho cac input
    function loadDataByAjaxFromInput(url){
        $('#data-loading').show();
        var csrf_value = '<?php echo $this->security->get_csrf_hash(); ?>';
        var fByName = $("#input-name").val();
        //Ajax
        $.ajax({
            type: "POST",
            url: url,
            data: {
                csrf_name: csrf_value,
                fByName: fByName
            },
            dataType: "text",
            cache: false,
            success: function(data){
                $('#div-response').html(data);
                $('#data-loading').hide();
            }
        });
    }

    function changePagination(pageId) {
        $('#data-loading').show();
        var csrf_value = '<?php echo $this->security->get_csrf_hash(); ?>';
        var url = '<?php echo base_url()."backend/cmsRanking/listByMonthlyAjax"; ?>';
        var fByName = $("#input-name").val();
        //Ajax
        $.ajax({
            type: "POST",
            url: url,
            data: {
                csrf_name: csrf_value,
                fByName: fByName,
                pageId: pageId
            },
            dataType: "text",
            cache: false,
            success: function(data){
                $('#div-response').html(data);
                $('#data-loading').hide();
            }
        });
    }

    function updateExportLink() {
        var href = '<?php echo base_url("backend/cmsRanking/exportReport/monthly"); ?>/' + $("#input-name").val();
        $('#btn-export').attr('href', href);
    }

</script>