/**
 * 海峡计划专家评分管理初始化
 */
var ChannelplanExpertLive = {
    id: "ChannelplanExpertLiveTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
ChannelplanExpertLive.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '唯一标识', field: 'id', visible: false , align: 'center', valign: 'middle'},
            {title: '流程', field: 'step', visible: true, align: 'center', valign: 'middle',
                formatter: function (value,row,index) {
                        if(value==1){
                            return "函审"
                        }else if(value==2){
                            return "现场评审"
                        }
                }},
            {title: '专家', field: 'expertId', visible: true, align: 'center', valign: 'middle'},
            {title: '分数', field: 'score', visible: true, align: 'center', valign: 'middle'},
            {title: '推荐类型', field: 'recommendType', visible: true, align: 'center', valign: 'middle'},
            {title: '函审时间',field: 'createTime',visible: true, align: 'center', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
ChannelplanExpertLive.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        ChannelplanExpertLive.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加海峡计划专家评分
 */
ChannelplanExpertLive.openAddChannelplanExpert = function () {
    var index = layer.open({
        type: 2,
        title: '添加海峡计划专家评分',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/channelplanExpert/channelplanExpert_add?step=3'
    });
    this.layerIndex = index;
};

/**
 * 打开查看海峡计划专家评分详情
 */
ChannelplanExpertLive.openChannelplanExpertDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '海峡计划专家评分详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/channelplanExpert/channelplanExpert_updateDetail/' + ChannelplanExpertLive.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除海峡计划专家评分
 */
ChannelplanExpertLive.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/channelplanExpert/delete", function (data) {
            Feng.success("删除成功!");
            ChannelplanExpertLive.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("channelplanExpertId",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询海峡计划专家评分列表
 */
ChannelplanExpertLive.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    ChannelplanExpertLive.table.refresh({query: queryData});
};

/**
 * 函审通过操作
 */
ChannelplanExpertLive.livePass = function () {
    var score=$("#secScore").val();
    var type=$("#secType").val();
    var secMsg=$("#secMsg").val();
    $.ajax({
        url : Feng.ctxPath + "/channelplanExpert/livePass?score="+score+"&type="+type+"&secMsg="+secMsg,
        type : 'GET',
        dataType : 'json',
        success : function(data) {
            ChannelplanExpertLive.close();
        },
        error : function(msg) {
            console.log(0);
        }
    });


};

/**
 * 函审失败操作
 */
ChannelplanExpertLive.liveFail = function () {
    var score=$("#secScore").val();
    var type=$("#secType").val();
    var secMsg=$("#secMsg").val();
    $.ajax({
        url : Feng.ctxPath + "/channelplanExpert/liveFail?score="+score+"&type="+type+"&secMsg="+secMsg,
        type : 'GET',
        dataType : 'json',
        success : function(data) {
            ChannelplanExpertLive.close();
        },
        error : function(msg) {
            console.log(0);
        }
    });


};

/**
 * 求算平均分
 */
ChannelplanExpertLive.calScore = function(step){
    $.ajax({
        url : Feng.ctxPath + "/channelplanExpert/calScore?step="+step,
        type : 'GET',
        dataType : 'json',
        success : function(data) {
            $("#secScore").val(data);
        },
        error : function(msg) {
            console.log(0);
        }
    });
}

/**
 * 关闭窗口
 */
ChannelplanExpertLive.close = function(){
    parent.layer.close(window.parent.layer.getFrameIndex(window.name));
}

$(function () {
    var defaultColunms = ChannelplanExpertLive.initColumn();
    var table = new BSTable(ChannelplanExpertLive.id, "/channelplanExpert/list?step=2", defaultColunms);
    table.setPaginationType("client");
    ChannelplanExpertLive.table = table.init();
    //获取推荐类别
    Feng.addAjaxSelect({
        "id": "secType",
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/dict/findChildDictByCode?code=un_declareSort"
    });
    $("select").each(function () {
        $(this).val($(this).attr("selectVal"));
    });
    /*//求所有分数的平均数
    var num = 0;
    $("#ChannelplanExpertTable").children("tbody").children("tr").each(function (index,e) {
        var a=$(e).children("td").eq(3).text();
        $(e).children("td").each(function (index,event) {
            alert(index+$(event).text())
        });
        if(a!=null&&a!=""){
            num=num+Number(a);
        }
    })
    $("#secScore").val(num);*/
    ChannelplanExpertLive.calScore(2);
});
