<script type="text/javascript">
    $(document).ready(function() {
        // Lấy thông tin bài hát
        $("#ranking_true").change(function(){
            $('#keep_ranking').val($('#ranking_true option:selected').attr('ranking'));
            $('#keep_track_title').val($('#ranking_true option:selected').text().trim());
        });
        $("#ranking_false").change(function(){
            $('#ranking_id').val($('#ranking_false option:selected').attr('ranking_id'));
            $('#delete_ranking').val($('#ranking_false option:selected').attr('ranking'));
            $('#delete_track_title').val($('#ranking_false option:selected').text().trim());
        });
    });

    // Hàm lấy ds BXH và hiển thị modal báo cáo sai sót
    function showReportModal() {
        $('#data-loading').show();
        var time = ($("#input-date-picker").val()) ? $("#input-date-picker").val() : null;
        $.ajax({
            type: "POST",
            url: '<?php echo base_url("backend/cmsRanking/getRankingByAjax"); ?>',
            data: {
                csrf_name: '<?php echo $this->security->get_csrf_hash(); ?>',
                time: time,
                scope: '<?php echo (isset($tags) ? $tags : 'DAILY')?>'
            },
            dataType: "HTML",
            cache: false,
            success: function(data) {
                $('#ranking_at').html(time);
                $('#ranking_true').html(data);
                $('#ranking_false').html(data);
                //
                $('#data-loading').hide();
                $('#modal-overlapping-ranking').modal('show');
            },
            error: function () {
                $('#data-loading').hide();
            }
        });
    }

    // Hàm gửi data báo cáo sai sót
    function sendReportOverlapping() {
        var ranking_id = $('#ranking_id').val();
        var keep_ranking = $('#keep_ranking').val();
        var keep_track_id = $('#ranking_true').val();
        var keep_track_title = $('#keep_track_title').val();
        //
        var delete_ranking = $('#delete_ranking').val();
        var delete_track_id = $('#ranking_false').val();
        var delete_track_title = $('#delete_track_title').val();
        //
        if (parseInt(delete_track_id) > 0) {
            if (parseInt(keep_track_id) != parseInt(delete_track_id)) {
                $.ajax({
                    type: "POST",
                    url: '<?php echo base_url("backend/cmsRanking/reportOverlappingAjax"); ?>',
                    data: {
                        csrf_name: '<?php echo $this->security->get_csrf_hash(); ?>',
                        time: ($("#input-date-picker").val()) ? $("#input-date-picker").val() : null,
                        scope: '<?php echo (isset($tags) ? $tags : 'DAILY')?>',
                        ranking_id: ranking_id,
                        keep_ranking: keep_ranking,
                        keep_track_id: keep_track_id,
                        keep_track_title: (keep_track_title) ? keep_track_title.trim() : keep_track_title,
                        delete_ranking: delete_ranking,
                        delete_track_id: delete_track_id,
                        delete_track_title: (delete_track_title) ? delete_track_title.trim() : delete_track_title,
                    },
                    dataType: "JSON",
                    cache: false,
                    success: function(data) {
                        $('#modal-overlapping-ranking').modal('hide');
                        if (data.code == 200) {
                            //
                        } else {}
                    },
                    error: function () {
                        $('#modal-overlapping-ranking').modal('hide');
                    }
                });
            } else {
                alert('Hai bài hát phải khác nhau!')
            }
        } else {
            alert('Bạn vui lòng, chọn bài hát muốn gỡ bỏ khỏi BXH!');
        }
    }
</script>