var request = getRequest(),
    prescriptionCode = request.code,
    from = request.from;

var docInfo = window.localStorage.getItem("docInfo");
docInfo = JSON.parse(docInfo);

$(function(){
    getPrescriptionFollow();
    bindEvents();
})

function getPrescriptionFollow(){
    var params = {
        prescriptionCode: prescriptionCode
    };
    orderAPI.getPrescriptionFollow({data: params}).then(function(res){
        if(res.status == 200){
            //如果是健管师配送，且未分配健管师，则显示‘分配’按钮
            if((docInfo.code == res.data.leadCode) && (res.data.dispensaryDispensaryType == 3) && (!res.data.prescriptionExpressage.expressageName)){
                $(".btn-1").removeClass("hidden");
            }

            $("#applyDate").text(new Date(res.data.createTime).format('yyyy-MM-dd HH:mm:ss'));
            $("#checkDoc").text(res.data.doctorName);

            //填充收药信息的内容
            var obj = $.extend({
                dispensaryDispensaryType: res.data.dispensaryDispensaryType,
                dispensaryDispensaryTypeName: res.data.dispensaryDispensaryTypeName
            }, res.data.prescriptionExpressage);
            var syInfoHtml = template('syinfo_tmp', obj);
            $("#syInfo").empty().append(syInfoHtml);

            //填充订单记录信息
            var obj2 = {
                prescriptionStatusName: res.data.prescriptionStatusName,
                orderCode: res.data.prescriptionExpressage.code,
                prescriptionHospital: res.data.prescriptionHospital
            };
            var orderInfoHtml = template('record_tmp', obj2);
            $("#recordInfo").empty().append(orderInfoHtml);

            //填充药品信息
            var drugHtml = template('drug_tmp', {list: res.data.prescriptionInfos});
            $("#drugInfo").empty().append(drugHtml);

           
        }else{

        }
    })
}

function getOrderFollow(){
    var params = {
        prescriptionCode: prescriptionCode
    };
    orderAPI.getOrderFollow({data: params}).then(function(res){
        if(res.status == 200){
            var html = template('flow_tmp', {list: res.data});
            $("#orderLogs").empty().append(html);
        }else{

        }
    });
}

//获得健管师列表
function getHealthDoctorList(isRefresh){
    if(isRefresh){
        $("#hDoctor").bootstrapTable('refresh');
    }
    var options = $.extend(orderAPI.getHealthDoctorTableAjaxObj, {
        queryParams: queryParams2,
        queryParamsType: "limit",
        pagination: true,
        paginationLoop: true,
        sidePagination: 'server',
        pageNumber: 1,
        pageSize: 8,
        pageList: [5,8,10],
        responseHandler: function (res) {
            return {
                rows: res.data.doctors,
                total: res.data.total
            }
        },
        columns: [{
            field: 'photo',
            title: '',
            width: '50',
            formatter: function(val, row, index){
                var url = "img/d-male.png";
                if(val.indexOf("http")>-1 || val.indexOf("https")>-1){
                    url = val;
                }
                return '<img src="'+url+'" class="img-circle" width="40">';
            }
        }, {
            field: 'name',
            title: '',
            align: 'left'
        },{
            field: 'jobName',
            title: '职称',
            align: 'right'
        }],
        onClickRow: function(row, $el){
            $("#docInfo").text(row.name+row.jobName);
            $("#confirmModal").modal('toggle');
            selectDoctor = row.code;
        }
    });
    $("#hDoctor").bootstrapTable(options);
}

function queryParams2(params) {
    //当表格数据变化的时候，则取消全选按钮，然后之前的选中的信息将取消选中
    return {
        page: params.offset/params.limit + 1,
        size: params.limit,
        name: $.trim($("#doctorName").val())
    };
}

function getExpressInfo(){
    var params = {
        prescriptionCode: prescriptionCode
    };
    orderAPI.getOrderExpressInfo({data: params}).then(function(res){
        if(res.status == 200){
            var html = template("expressInfo_tmp", res.data);
            $("#expressInfo").empty().append(html);
        }else{

        }
    });
}

function getExpressRouteInfo(){
    var params = { prescriptionCode: prescriptionCode};
    orderAPI.getSfRouteService({data: params}).then(function(res){
        if(res.status == 200){
            var list = _.map(res.data.list, function(o){
                o.statusName = o.acceptRemark;
                o.createTime = o.acceptTime;
                return o;
            });
            var html = tempalte("flow_tmp", {list: list});
            $("#expressLogs").empty().append("flow_tmp");
        }else{

        }
    });
}

function showOrderFollow(){
    $("#myModal").modal("toggle");
    getOrderFollow();
}

function showExpressFollow(){
    $("#expressModal").modal('toggle');
    getExpressInfo();
    getExpressRouteInfo();
}

function bindEvents(){
    $(".btn-1").on('click', function(){
        $('#docModal').modal('toggle');
        getHealthDoctorList();
    })

    $(".btn-2").on('click', function(){
//      history.go(-1);
        if(from == 'tab') {
            top.toPrePrescriptionTab && top.toPrePrescriptionTab()
        } else {
            history.go(-1);
        }
    })

    $("#doctorName").on('keyup', function(e){
        if (e.which === 13) {
            getHealthDoctorList(true);
        }
    });

    $("#confirmBtn").on('click', function(){
        //发送请求确认的请求
        var params = {
            codes: prescriptionCode,
            healthDoctor: selectDoctor
        }
        orderAPI.distributeHealthDoctor({data: params}).then(function(res){
            if(res.status == 200){
                $('#docModal').modal('toggle');
                $('#confirmModal').modal('toggle');
                //更新数据
                getPrescriptionFollow();
            }else{

            }
        })
    });
}