/**
 *
 * @param lawyerCaseSnapshotBeanList
 * @param table_id "#lawyerCaseList'
 */
function proceedLawyerCase(lawyerCaseSnapshotBeanList, table_id, only_show_previous_year_case) {
    if (only_show_previous_year_case) {
        only_show_previous_year_case_proceed(lawyerCaseSnapshotBeanList, table_id)
    } else {
        show_all_case_proceed(lawyerCaseSnapshotBeanList, table_id)
    }
}

function show_all_case_proceed(lawyerCaseSnapshotBeanList, table_id) {
    // 案例信息填充
    var index = 0;
    var sub_html = "";
    for (var i in lawyerCaseSnapshotBeanList) {
        if (i == "remove" || i >= 5) {
            continue;
        }
        var v = lawyerCaseSnapshotBeanList[i];
        if (v) {
            index += 1;
            sub_html += "                    <tr>\n" +
                "                                <td>" + index + "</td>\n" +
                "                                <td>" + v.caseTypeDesc + "</td>\n" +
                "                                <td>" + v.caseNum + "</td>\n" +
                "                                <td>" + "<input id='openCaseBtn' class='movingup' type='button' value='查看' onclick='showCaseDetail(" + i + ",false);'>" + " </td>\n" +
                "                            </tr>"
        }
    }
    $(table_id).html(sub_html);
}

var global_arr = []

function only_show_previous_year_case_proceed(lawyerCaseSnapshotBeanList, table_id) {
    // 案例信息填充
    var index = 0;
    var sub_html = "";
    var arr = filter(lawyerCaseSnapshotBeanList);
    global_arr = arr;
    for (var idx in arr) {
        if (idx == "remove") {
            continue;
        }
        var bean = arr[idx];
        var v = lawyerCaseSnapshotBeanList[bean.bean_index];
        index += 1;
        sub_html += "                    <tr>\n" +
            "                                <td>" + index + "</td>\n" +
            "                                <td>" + bean.case_year + "</td>\n" +
            "                                <td>" + bean.num + "</td>\n" +
            "                                <td>" + "<input id='openCaseBtn' class='movingup' type='button' value='查看' onclick='showGroupCaseDetail(" + idx + ");'>" + " </td>\n" +
            "                            </tr>"
    }
    $(table_id).html(sub_html);
}


function filter(lawyerCaseSnapshotBeanList) {
    var arr = [];
    for (var i = 0; i < lawyerCaseSnapshotBeanList.length; i++) {
        var bean = lawyerCaseSnapshotBeanList[i];
        for (var k = 0; k < bean.lawyercaseWebSiteBeans.length; k++) {
            var data = bean.lawyercaseWebSiteBeans[k];
            var caseNumString = data.caseNum;
            var point = {// 具体案例的位置。
                index_1: i,
                index_2: k,
            };
            var case_year = "其它年份"
            try {
                case_year = parseInt(caseNumString.slice(1, 5)) > 1000 ? parseInt(caseNumString.slice(1, 5)) : "其它年份";
            } catch (e) {
                console.warn(caseNumString)
            }
            var container = extract(arr, case_year);
            if (!container) {
                container = {
                    case_year: case_year,
                    num: 0,
                    data: []
                }
                arr.push(container);
            }
            container.num += 1;
            container.data.push(point);
        }

    }

    function extract(arr, case_year) {
        var ret = undefined;
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].case_year == case_year) {
                ret = arr[i];
            }
        }
        return ret;
    }

    function compare(property) {
        return function (a, b) {
            var value1 = a[property];
            var value2 = b[property];
            if (isNaN(value1)) {
                return 1;
            }
            if (isNaN(value2)) {
                return -1;
            }
            return -(value1 - value2);// 从大到小.
        }
    }

    arr.sort(compare('case_year'))
    return arr;
}

function showCaseDetail(index, only_show_previous_year_case) {
    var previous_year = new Date().getFullYear() - 1 + "";
    if (lawyerCaseSnapshotBeanList) {
        var bean = lawyerCaseSnapshotBeanList[index]
        var _index = 0;
        var sub_html = "";
        for (var i=0;i<bean.lawyercaseWebSiteBeans.length;i++){
            if (i == "remove") {
                continue;
            }
            var v = bean.lawyercaseWebSiteBeans[i];
            var caseNumString = v.caseNum;
            if (only_show_previous_year_case) {
                if (caseNumString && caseNumString.indexOf(previous_year) == -1) {
                    continue;
                }
            }
            _index += 1;
            sub_html += "                    <tr>\n" +
                "                                <td>" + _index + "</td>\n" +
                "                                <td>" + bean.caseTypeDesc + "</td>\n" +
                "                                <td>" + v.title + "</td>\n" +
                "                                <td><a href='" + v.url + "' target='_blank'>查看</a></td>\n" +
                "                            </tr>"
        }
        $("#lawyer-case-detail-tbody").html(sub_html);
        $("#modal-view-lawyer-case-detail").modal("toggle");
    }
}

function showGroupCaseDetail(idx) {
    var bean = global_arr[idx]
    if (bean) {
        var data = bean.data;
        var _index = 0;
        var sub_html = "";
        for (var i = 0; i < data.length; i++) {
            var point = data[i]
            _index += 1;
            sub_html += "                    <tr>\n" +
                "                                <td>" + _index + "</td>\n" +
                "                                <td>" + lawyerCaseSnapshotBeanList[point.index_1]['caseTypeDesc'] + "</td>\n" +
                "                                <td>" + lawyerCaseSnapshotBeanList[point.index_1]['lawyercaseWebSiteBeans'][point.index_2].title + "</td>\n" +
                "                                <td><a href='" + lawyerCaseSnapshotBeanList[point.index_1]['lawyercaseWebSiteBeans'][point.index_2].url + "' target='_blank'>查看</a></td>\n" +
                "                            </tr>"
        }
        $("#lawyer-case-detail-tbody").html(sub_html);
        $("#modal-view-lawyer-case-detail").modal("toggle");
    }
}

function isEmpty(obj) {
    if (typeof obj == "undefined" || obj == null || obj == "") {
        return true;
    } else {
        return false;
    }
}

function isNotEmpty(obj) {
    return !isEmpty(obj);
}

function show(show_text, show_id) {
    if (isNotEmpty(show_text)) {
        $(show_id).text(show_text);
    } else {
        $(show_id).parent().parent().hide();
    }
}

function showCallback(ret) {
    show(ret.loanInfoBean.loanPurpose, '#view-loanPurpose');
    // show(ret.loanInfoBean.education, '#view-education');
    // show(ret.loanInfoBean.incomeRange, '#view-incomeRange');
    // show(ret.loanInfoBean.email, '#view-email');
    // show(ret.loanInfoBean.address, '#view-address');
    // show(ret.loanInfoBean.realName, '#view-bank-regist-name');
    // show(ret.loanInfoBean.idCard, '#view-idCard');
    // show(ret.loanInfoBean.bankName, '#view-bankName');
    // show(ret.loanInfoBean.bankNum, '#view-bankNum');
    // show(ret.loanInfoBean.bankMobile, '#view-bankMobile');
    // show(ret.loanInfoBean.lawOffice, '#view-lawOffice');
    // show(ret.loanInfoBean.lawAddress, '#view-lawAddress');
    // show(ret.loanInfoBean.lawPhone, '#view-lawPhone');
    // show(ret.loanInfoBean.sfzAddress, '#view-sfzAddress');
    // show(ret.loanInfoBean.certDate, '#view-certDate');
    // show(ret.loanInfoBean.certNum, '#view-certNum');

}