$(function () {
    // collapse click, toggle itself
    var urlParams = new URLSearchParams(window.location.search);
    var examId = urlParams.get('examId');
    var caseId = urlParams.get('caseId');
    $('table#report-list .collapse').click(function () {
        $(this).collapse('toggle');
    });
// load table
    var reportListTable = $('#report-list').DataTable({
        searching: false,
        pageLength: 25
    });
    // assign click
    $('#assign-btn').click(function () {
        // 修改assign-btn的文字以及让其处于不可用状态
        $(this).attr('disabled', true);
        $(this).find('span').text('融合中...');

        $.get('/aggregate', {examId: examId, caseId: caseId}, function () {
            setInterval(function () {
                $.get('/agg_task_status', {examId: examId, caseId: caseId}, function (result) {
                    if (result == 1) {

                        agg_success();
                    }
                });
            }, 10000)
        });
    });

    $('#deliver-btn').click(function () {
        if (confirm("是否确定全部交付？") == true) {
            $(this).attr('disabled', true);
            $(this).find('span').text('处理中...');
            $.get('/appendBugToFinalReport', {examId: examId, caseId: caseId}, function () {

                alert("处理完成");
                location.reload();
            });
        }
    });

});

function agg_success() {

    var rand = Math.round(Math.random() * 100);
    var count = $('#report-list tbody tr').length;

    $('#report-list tbody tr').each(function (i, el) {
        setTimeout(function () {
            rand = Math.round(Math.random() * 100);

            // 在此条记录最后添加loading的图标
            $(el).find('td:last').after('<td><i class=\'fa fa-spinner\'></i></td>');

            // get value
            var _id = $(el).children('td.report-id').attr('value');

            $.get('/aggregate_info', {bugId: _id}, function (data) {
                var aggregator = data.masterId;
                var printAggregator = 'ML-AG-' + data.masterId.substring(10);

                function fadeInAggreagator() {
                    // $(el).children('td.report-aggregator').hide();
                    if ($.trim(aggregator)) {//找到了重复报告
                        $(el).children('td.report-aggregator').attr('value', aggregator);
                        var aggregatorHtml = "<a href=/report?masterId=" + aggregator + "&examId=" + examId + "&caseId=" + caseId + ">" + printAggregator + "</a>";
                        $(el).children('td.report-aggregator').html(aggregatorHtml).fadeIn(rand * 2);
                    }
                }

                function fadeInStatus() {
                    $(el).find('td:last').hide();
                    // 如果data.assignee 不为None，则最后添加对勾图标，否则添加叉叉图标
                    if ($.trim(aggregator)) {//找到了负责人
                        $(el).find('td:last').html('<i class=\'fa fa-check-circle text-inverse\'></i>').fadeIn(rand * 2);
                        $(el).find('td:last').addClass('row-success');

                    } else {
                        $(el).find('td:last').html('<i class=\'fa fa-history text-inverse\'></i>').fadeIn(rand * 2);
                        $(el).find('td:last').addClass('row-danger');
                    }
                }

                setTimeout(fadeInAggreagator, 0);
                setTimeout(fadeInStatus, rand);
            });
        }, rand * 3 * i);
    });

    //假设此time之后所有assign操作均已完成
    setTimeout(function () {
        location.reload();
    }, rand * 3 * (count + 1));
};