//获取团队信息
var request = getRequest(),
    teamCode = request.id || '',
    type = request.type;
//记录页面控件的值
var startDate = '', 
    endDate = '',
    state = '', //状态
    dispensaryType = '', //配送方式
    page = 1,
    size = 10,
    hospital, //服务站code
    allocationType, //是否是待分配续方
    nameKey, //搜索的姓名
    totalOrderCount = 0,
    selectDoctor; //选中的医生
$(function(){
    //填充下拉框数据
    orderAPI.getHFilterInfo().then(function(res){
        fillDropDown(res);
        //获得续方订单列表
        getPrescriptionList();
    });
    bindEvents();
});

//获得续方订单列表
function getPrescriptionList( refresh){
    var params = {
        type: type,
        teamCode: teamCode,
        startDate: startDate,
        endDate: endDate,
        state: state,
        dispensaryType: 3,
        hospital: hospital,
        nameKey: nameKey
    };
    //先请求获得所有的数量
    orderAPI.getHOrderListCount({data:params}).then(function(res){
        if(res.status == 200){
            totalOrderCount = res.data.total;
            if(refresh){
                $("#prescriptTable").bootstrapTable('refresh');
            }
            var options = $.extend(orderAPI.getHOrderListTableAjaxObj, {
                queryParams: queryParams,
                queryParamsType: "limit",
                pagination: true,
                paginationLoop: true,
                sidePagination: 'server',
                pageNumber: 1,
                pageSize: 10,
                responseHandler: function (res) {
                    var data = _.map(res.data, function(o){
                        var result = "";
                        for(var i=0; i<o.prescriptionDt.length; i++){
                            if(i>0){
                                result += ','+o.prescriptionDt[i].name;
                            }else{
                                result += o.prescriptionDt[i].name;
                            }
                        }
                        o.result = result;
                        o.statusName = getStatusName(o.status);
                        o.express = getExpressName(o.dispensaryType);
                        o.address = address;
                        o.action = '<a class="c-12b7f5" href="prescription-main.html?code='+o.code+'&patiCode='+o.patientCode+'&teamCode='+teamCode+'&tab=2">查看</a>';
                        return o;
                    });
                    
                    return {
                        rows: data,
                        total: totalOrderCount
                    }
                }
            })
            $("#prescriptTable").bootstrapTable(options);
        }
    });
    
}

function queryParams(params) {
    return {
        type: type,
        page: params.offset/params.limit + 1,
        size: params.limit,
        startDate: startDate,
        endDate: endDate,
        state: state,
        hospital: hospital,
        dispensaryType: 3,
        teamCode: teamCode,
        nameKey: nameKey
    };
}

function fillDropDown(res){
    if(res.status == 200){
        var stateHtml = template('state_tmpl', {list: res.data.states});
        $("#orderStatus").append(stateHtml);

        var expressHtml = template('address_tmpl', {list: res.data.hospitals});
        $("#address").append(expressHtml);
    }else{

    }
}

//绑定事件
function bindEvents(){
    $(".n-tab").on('click', function(){
        if($(this).hasClass("active")){
            return false;
        }else{
            $(this).addClass("active");
            $(this).siblings().removeClass("active");
            var seDate = getStartEndDate($(this).attr("data-val"));
            startDate = seDate.startDate;
            endDate = seDate.endDate;
            getPrescriptionList(true);
        }
    });

    $("#orderStatus").on('change', function(){
        var $this = $(this);
        state = $this.val();
        getPrescriptionList(true);
    });
    
    $("#address").on('change', function(){
        hospital = $(this).val();
        getPrescriptionList(true);
    });

    $("#searchBtn").on('click', function(){
        var $input = $("#searchName"),
            text = $.trim($input.val());
        nameKey = text;
        getPrescriptionList(true);
    });

}

