<?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">
    <?php echo form_open(base_url().'backend/cmsRanking/listByMonthlyAjax'); ?>
    <div class="col-lg-3">
    <select class="form-control" name="input-name" id="input-name">
    <?php
    $listSearch= $this->report_views_monthly_model->selectSearch();
        foreach ($listSearch as $data):
    ?>
        <option value = "<?php echo $data['month'].'-'.$data['year']; ?>">Tháng <?php echo $data['month']; ?>/<?php echo $data['year']; ?></option>
    <?php
        endforeach;
    ?>
    </select>
        <!-- <input
            class="form-control" type="text"
            name="input-name"
            id="input-name"
            placeholder="Nhập tên bài hát..."/> -->
    </div>
    <?php echo form_close(); ?>
    <div class="col-lg-1">
        <button class="btn btn-sm btn-default" id="excel">Xuất Excel</button>
    </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-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"); ?>';
        var url1 = '<?php echo base_url("backend/cmsRanking/exportExcel"); ?>';

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

        });
        changePagination('0');
    });
    function exportExcel(url1){
        $('#data-loading').show();
        var csrf_value = '<?php echo $this->security->get_csrf_hash(); ?>';
        var fByName = $("#input-name").val();
        //Ajax ve bieu do
        $.ajax({
            type: "POST",
            url: url1,
            data: {
                csrf_name: csrf_value,
                fByName: fByName
            },
            dataType: "text",
            cache: false,
            success: function(data){
                $('#data-loading').hide();
            changePagination('0');
            }
        });
    }
    //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 ve bieu do
        $.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();
            }
        });
    }
    //Ham chung cho cac SelectBox
    function loadDataByAjaxFromSelectBox(id, url){
        $('#'+id).change(function(){
            $('#data-loading').show();
            var csrf_value = '<?php echo $this->security->get_csrf_hash(); ?>';
            var fByName = $("#input-name").val();
            //Ajax ve bieu do
            $.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 loadDataByAjaxDateRange(dtFrom, dtTo, 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 ve bieu do\
        $.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();
            }
        });
    }

</script>