(function($){
    var request = getRequest(),
        fd_class = request.fd_class, //随访类型，与续方类型一致
        patiCode = request.patiCode,
        fv_id = request.fv_id,
        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);
                    }else{
                        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(){
        $("#startFU").on("click", function(){
            if(fv_status != 2){
                
            }else{
                var params = {
                    id: fv_id,
                    date: $("#doneDate").val(),
                    followupType : fv_type,
                    followupClass : fv_class,
                    followupManagerStatus : $("#followupStatus").val(),
                    plandate:$("#nextDate").val()
                }
                followupAPI.startFollowup(params).then(function(res){
                    if(res.status == 200){
                        
                    }else{
                        showErrorMsg(res.msg);
                    }
                });
            }
        });
    }
})(jQuery)

