(function($){
    var request = getRequest(),
        fd_class = request.fd_class, //随访类型，与续方类型一致
        patiCode = request.patiCode,
        fv_id = request.fv_id,
        prescriptionCode = request.prescriptionCode,
        fv_status,
        fv_type;

    var docInfo = window.localStorage.getItem('docInfo'),
        level;
    if(docInfo){
        docInfo = JSON.parse(docInfo);
        level = docInfo.level;
    }
     
    //随访类型
    var followupClass = {"1": "高血压", "2": "糖尿病", "3": "高血压， 糖尿病"},
        //随访方式， 对应医生的level
        followupType = {"1": {"key": 13, "value": "专科医生随访"},
                        "2": {"key": 22, "value": "家庭（全科）"},
                        "3": {"key": 10, "value": "健管师随访"}
                    };
    
    $(function(){
        bindEvents();
        if(fv_id){
            followupAPI.getFollowup({id: fv_id}).then(function(res){
                if(res.status == 200){
                    var data = res.data;
                    fv_status = data.status; //随访状态 1-已完成，2-未开始，3-进行中
                    fv_class = data.followupClass;
                    fv_type = data.followupType;
                    
                    $("#doneDate").val(data.followupDate.substr(0, 16));
                    $("#followupType").val(data.followupTypeName);
                    $("#followupClass").val(data.followupClassName);
                    $("#nextDate").val(data.followupNextDate.substr(0, 16));
                    $("#followupStatus").val(data.followupManagerStatusName)
                    
                    if(fv_status != 2){
                        $("#followupStatus").attr("disabled", true);
                        parent.clickOtherTab = true;
                        $("#startFU").attr("disabled", false);
                    }else{
                        $("#startFU").attr("disabled", true);
                        initDatePicker();
                    }
                    getDict(); //获得随访状态下拉框数据
                }else{
                    showErrorMsg(res.msg)
                }
            });
            return;
        }
        
        getDict(); //获得随访状态下拉框数据
        initDatePicker();
        
        var date = new Date();
        $("#doneDate").val(date.format("yyyy-MM-dd HH:mm"));
        $("#followupClass").val(followupClass[fd_class]);
        $("#followupType").val(followupType[level]['value']);
        fv_type = followupType[level]['key'];
    });
    
    function initDatePicker(){
        $(".form_datetime").datetimepicker({
            format: 'yyyy-mm-dd hh:ii',
            language: 'zh-CN',
            weekStart: 1,
            todayBtn:  1,
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0
        });
        
    }
    
    //获得随访状态下拉框数据
    function getDict(){
        followupAPI.getDictByDictName().then(function(res){
            if(res.status == 200){
                var html = "";
                for(i=0; i<res.list.length; i++){
                    var item = res.list[i];
                    html += '<option value="'+ item.code +'">'+ item.value + '</option>';
                }
                $("#followupStatus").empty().append(html);
                $(".my_select_box").chosen({
                    disable_search: true,
//                  inherit_select_classes: true, //将设定的样式添加到插件的控件中
                    disable_search_threshold: 10,
                    width: "100%"
                });
            }else{
                showErrorMsg(res.msg);
            }
        });
    }
    
    function bindEvents(){
        $("input").on("change", function(){
            checkBtnEnable();
        })
        $("#startFU").on("click", function(){
            if(fv_status != 2){
                parent.goFollowupNextQuestionaire();
            }else{
                if($(this).attr("disabled")){
                    return false;
                }
                var doneDate = $("#doneDate").val() ? $("#doneDate").val()+":00" : "",
                    planDate = $("#nextDate").val() ? $("#nextDate").val()+":00" : "";
                var params = {
                    id: fv_id,
                    date: doneDate,
                    followupType : fv_type,
                    followupClass : fv_class,
                    followupManagerStatus : $("#followupStatus").val(),
                    plandate: planDate
                }
                if(prescriptionCode){ //续方相关的随访需要带上续方code
                    params.prescriptioncode = prescriptionCode;
                }
                followupAPI.startFollowup(params).then(function(res){
                    if(res.status == 200){
                        parent.clickOtherTab = true;
                        parent.goFollowupNextQuestionaire();
                    }else{
                        showErrorMsg(res.msg);
                    }
                });
            }
        });
    }
    
    function checkBtnEnable(){
        //判断完成时间是否有值
        var doneDate = $("#doneDate").val(),
            planDate = $("#nextDate").val();
        
        if(doneDate == "" || planDate == ""){
            $("#startFU").attr("disabled", true);
        }else{
            $("#startFU").attr("disabled", false);
        }
    }
})(jQuery)

