//获取团队信息
var request = getRequest(),
    teamCode = request.id;
//记录页面控件的值
var startDate = '', 
    endDate = '',
    state = '', //状态
    dispensaryType = '', //配送方式
    page = 1,
    size = 10,
    hospital, //服务站code
    allocationType, //是否是待分配续方
    nameKey; //搜索的姓名
$(function(){
    //填充下拉框数据
    orderAPI.getFilterInfo().then(function(res){
        fillDropDown(res);
        //获得续方订单列表
        getPrescriptionList();
    });
    //获得服务站数据
    orderAPI.getHospitalList({data: {teamCode: teamCode}}).then(function(res){
        var html = template('address_tmpl', {list: res.data});
        $("#address").empty().append(html);
    });
    bindEvents();
});

//获得续方订单列表
function getPrescriptionList(){
    var params = {
        startDate: startDate,
        endDate: endDate,
        state: state,
        dispensaryType: dispensaryType,
        hospital: hospital,
        allocationType: allocationType,
        nameKey: nameKey,
        page: page,
        size: size
    };
    orderAPI.getOrderList({data: params}).then(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.action = '<a href="">操作</a>';
            return o;
        });
        var html = template('list_tmpl', {list: data})
        $("#prescriptTable tbody").empty().append(html);
    });
}

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

        var expressHtml = template('express_tmpl', {list: res.data.dispensaryTypes});
        $("#express").append(expressHtml);
    }else{

    }
}

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

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

    $("#express").on('change', function(){
        dispensaryType = $(this).val();
        getPrescriptionList();
    });
    
    $("#address").on('change', function(){
        hospital = $(this).val();
        getPrescriptionList();
    });

    $("#allocationType").on('change', function(){
        var $this = $(this);
        if($this.prop('checked')){
            dispensaryType = 3;
            allocationType = 1;
        }else{
            dispensaryType = '';
            allocationType = 0;
        }
        getPrescriptionList();
    });

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

//根据选择的tab获得开始和结束时间
function getStartEndDate(index){
    var now = new Date(),
        sDate = new Date();
    endDate = now.format('yyyy-MM-dd');
    switch (index){
        case '0':
            startDate = '';
            endDate = '';
            break;
        case '1': //近一周
            sDate.setDate(now.getDate() - 7);
            break;
        case '2': //近一个月
            sDate.setMonth(now.getMonth() -1);
            break;
        case '3': //近半年
            sDate.setMonth(now.getMonth() - 6);
            break;
        case '4': //近一年
            sDate.setFullYear(now.getFullYear() - 1);
            break;

    }
    startDate = sDate.format('yyyy-MM-dd');
}

//获得状态值
function getStatusName(status){
    var name = "",
        img = "";
    status = status + '';
    switch (status){
        case '-3':
            name = '支付过期';
            break;
        case '-2':
            name = '患者自己取消';
            break;
        case '-1':
            name = '审核不通过';
            break;
        case '0':
        case '2':
        case '3':
        case '4':
        case '10':
            name = '审核中';
            break;
        case '20':
            name = '药师审核中';
            break;
        case '21':
            name = '药师审核失败';
            break;
        case '30':
            name = '开方中';
            break;
        case '31':
            name = '开方失败';
            break;
        case '40':
            name = '待支付';
            break;
        case '41':
            name = '支付失败';
            break;
        case '50':
            name = '配药中';
            break;
        case '60':
            name = '等待领药';
            break;
        case '61':
        case '62':
        case '65':
        case '69':
            name = '配送中';
            break;
        case '100':
            name = '已完成';
            break;
        default:
            break;
    }
    return name;
}

function getExpressName(type){
    switch(type){
        case 1: 
            return '自取';
            break;
        case 2:
            return '快递配送';
            break;
        case 3:
            return '健管师配送';
            break;
    }
}