var request = getRequest(),
    tab = request.tab || 0,
    sessionId = request.sessionId, //续方咨询的sessionId 格式: 居民code+咨询code+咨询类型（ 8 ）
    patiCode = request.patiCode || '',
    consultCode,
    prescriptionCode = request.code || '',
    teamCode = request.teamCode || '',
    jwCode; //基卫那边存储的原处方的code

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

var fromTabIdx = request.fromTabIdx || tab; // 记录前一个Tab索引，用于返回按钮

//初始tab的链接
var links = [{
    url: 'prescription-consulting.html?from=tab&sessionId='+sessionId,
    name: '咨询',
    class: ''
},{
    url: 'prescription-detail.html?from=tab&code='+prescriptionCode+'&id='+teamCode,
    name: '续方详情',
    class: ''
},{
    url: 'order-tracking.html?from=tab&code='+prescriptionCode,
    name: '订单跟踪',
    class: '',
},{
    url: 'about:blank',
    name: '体征记录',
    class: 'hidden'
},{
    url: 'jianchajianyan.html?from=tab&patient='+patiCode,
    name: '检查检验',
    class: ''
},{
    url: 'jw-prescription-list.html?from=tab&patient='+patiCode,
    name: '诊断/处方',
    class: ''
},{
    url: 'prescription-list.html?from=tab&teamCode='+teamCode+'&patient='+patiCode,
    name: '历史续方',
    class: ''
}];

$(function(){
    if(!prescriptionCode){
        //没有续方的code，就从咨询中获取对应的code
        //这种情况下，患者的签约团队一定是医生所在的团队的id
        teamCode = docInfo.adminTeamCode;
        getPrescriptionInfo();
    }else{
        //有续方的code，通过code获得续方详情
        getPrescriptionInfoByCode();
    }
})

function initPage(){
    links[tab].class="active";
    var html = template('tab_tmp', {list: links});
    $("#tabs").append(html);
    parent.document.getElementById('main').src = links[tab].url;

    $("#tabs").on('click', 'a', function(){
        var $this = $(this),
            $li = $this.parent();
        fromTabIdx = $("#tabs li.active").index();  
        $li.siblings().removeClass("active");
        $li.addClass("active");
        tab = $this.data('index');
        parent.document.getElementById('main').src = links[tab].url+'&from=tab';
    })
}

function toPrePrescriptionTab() {
	if(fromTabIdx!=null && fromTabIdx != undefined) {
		$("#tabs li").eq(fromTabIdx).find('a').trigger('click')
	}
}

top.toPrePrescriptionTab = toPrePrescriptionTab

function getPrescriptionInfo(){
    patiCode = sessionId.split("_")[0];
    consultCode = sessionId.split("_")[1];
    var params = { consult: consultCode};
    consultingAPI.getPrescriptionInfo({data: params}).then(function(res){
        if(res.status == 200){
            prescriptionCode = res.data.code;
            jwCode = res.data.jwCode;
            updateLinkInfo();
            if(res.data.status < 50){ //支付成功前不会有订单记录
                links[2].class="hidden";
            }
            initPage();
        }else{
            toastr && toastr.warning(res.msg);
        }
    });
}

function getPrescriptionInfoByCode(){
    var params = {
        code: prescriptionCode,
        type: docInfo.isLeader == '1' ? 1 : 2
    };
    consultingAPI.getPrescriptionInfoByCode({data: params}).then(function(res){
        if(res.status == 200){
            patiCode = res.data.patient.code;
            jwCode = res.data.prescription.jwCode;
            consultCode = res.data.prescription.consult;
            sessionId = patiCode + '_' + consultCode+'_8';
            updateLinkInfo();
            //如果不是该续方所在团队的团队长，则不可以显示咨询tab
            if(res.data.prescription.doctor != docInfo.code){
                links[0].class="hidden";
            }
            if(res.data.prescription.status < 50){ //支付成功前不会有订单记录
                links[2].class="hidden";
            }
            initPage();
        }else{
            toastr && toastr.warning(res.msg);
        }
    });
}

function updateLinkInfo(){
    links = [{
        url: 'prescription-consulting.html?from=tab&sessionId='+sessionId,
        name: '咨询',
        class: ''
    },{
        url: 'prescription-detail.html?from=tab&code='+prescriptionCode+'&id='+teamCode,
        name: '续方详情',
        class: ''
    },{
        url: 'order-tracking.html?from=tab&code='+prescriptionCode,
        name: '订单跟踪',
        class: '',
    },{
        url: 'about:blank',
        name: '体征记录',
        class: 'hidden'
    },{
        url: 'jianchajianyan.html?from=tab&patient='+patiCode,
        name: '检查检验',
        class: ''
    },{
        url: 'jw-prescription-list.html?from=tab&patient='+patiCode,
        name: '诊断/处方',
        class: ''
    },{
        url: 'prescription-list.html?from=tab&teamCode='+teamCode+'&patient='+patiCode,
        name: '历史续方',
        class: ''
    }];
    console.log(links);
}