var request = getRequest(),
    prescriptionCode = request.code,
    fv_id = request.fv_id;
    
$(function(){
    getList();
    bindEvents();
})

function getList(){
    var params = {
        prescriptioncode: prescriptionCode
    };
    consultingAPI.getFollowupCountList(params).then(function(res){
        if(res.status == 200){
            var list = _.map(res.data, function(o){
                o.jsonStr = JSON.stringify(o);
                return o;
            });
            
            var html = template('info_tmp', {list: list});
            $("#container").empty().append(html);
            getConsultStatus()
        }else{
            showErrorMsg(res.msg);
        }
    });
}

function getConsultStatus(){
    var params = {
        prescriptionCode: prescriptionCode
    }
    consultingAPI.getConsultStatusByXFcode({data: params}).then(function(res){
        if(res.status == 200){
            //咨询状态（0进行中，1已完成，-1患者取消，-2超时未响应自动关闭）
            if(res.data == 0) { //咨询未结束
                $('.send-survey').show()
            } else { //咨询已结束
                $('.send-survey').hide()
            }
        }else{
        	showErrorMsg('获取咨询信息失败');
        }
    })
}

function bindEvents(){
    $("body").on('click', ".import-fv", function(e){
        e.stopPropagation();
        var $this = $(this),
            $box = $this.closest(".box"),
            jsonObj = $box.data('json'),
            params = {
                followupid: fv_id,
                prescriptioncode: prescriptionCode,
                type: jsonObj.type //1身体异常症状问卷，2体征及生活方式调查问卷
            };
        
        consultingAPI.importfollowupcontent(params).then(function(res){
            if(res.status == 200){
                showSuccessMsg("已填入");
            }else{
                showErrorMsg(res.msg);
            }
        })
    });
    
    $("body").on('click', '.send-survey', function(e){
        e.stopPropagation();
        var $this = $(this),
            $box = $this.closest(".box"),
            jsonObj = $box.data('json'),
            params = {
                followupid: fv_id,
                prescriptionCode: prescriptionCode,
                type: jsonObj.type //1身份异常症状问卷，2体征及生活方式调查问卷
            };
        consultingAPI.addPrescriptionFollowupContentConsult(params).then(function(res){
                if(res.status == 200){
                   showSuccessMsg("已发送");
                }else{
                    showErrorMsg(res.msg);
                }
            })
    });
    
    $("body").on('click', '.box', function(){
        var $this = $(this),
            json = $this.data('json');
         
        location.href = "survey-detail.html?prescriptionCode="+prescriptionCode+"&fv_id="+fv_id+"&type="+json.type+"&projectId="+json.typevalue+'&state='+json.statue;
    });
}
