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

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

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

$(function(){
    if(!prescriptionCode){
        //没有续方的code，就从咨询中获取对应的code
        getPrescriptionInfo();
    }else{
        //有续方的code，通过code获得续方详情
        getPrescriptionInfoByCode();
    }
})

function initPage(){
    if(!(docInfo.isLeader == '1')){
        links[0].class="hidden";
    }
    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();
        $li.siblings().removeClass("active");
        $li.addClass("active");
        parent.document.getElementById('main').src = links[tab].url;
    })
}

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;
            updateLinkInfo();
            if(res.data.prescription.status < 50){ //支付成功前不会有订单记录
                links[2].class="hidden";
            }
            initPage();
        }else{
            toastr && toastr.warning(res.msg);
        }
    });
}

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