var self,
methodDire,//用药字典
medicineFreqData,//用药频率数据
prescriptionCode,//续方code
diagnosis=[],//疾病
isChangeDiagnosis = 0,//判断是否改变疾病类型
usefulMsg;
mui.plusReady(function() {
self = plus.webview.currentWebview();
usefulMsg = self.jsonStr;
prescriptionCode = usefulMsg.prescriptionCode;//保存续方code
var medi_html = template('medi_tmp',{data:$.map(usefulMsg.prescriptionInfo,function(o,index){
o.jsonVal = JSON.stringify(o);
return o })
})
$("#mainContain").html(medi_html);
startDose();
//展示诊断类型
var diaArr=[]
$.map(usefulMsg.diagnosis,function(item,index){
diaArr.push(item.name)
})
$('#j-sick-choose').html(diaArr.join(','))
function startDose(){
//获取用药频率
sendPost("doctor/prescriptionInfo/getRateList",{}, function(){
mui.toast("获取用药频率数据失败");
}, function(res){
if(res.status == 200){
medicineFreqData = res.data;
addMedicineFreq($('.j-selFreq'),false)
}
},'get')
//获取药品用法
sendPost("doctor/prescriptionInfo/getUsageDict",{}, function(){
mui.toast("获取药品用法数据失败");
}, function(res){
if(res.status == 200){
methodDire = res.data;
}
},'get')
//调整原因
sendPost("doctor/prescriptionInfo/getReasonByType",{type:1}, function(){
mui.toast("请求失败");
}, function(res){
if(res.status = 200){
var str=""
$.map(res.data,function(o,i){
str += '
'+o.value+''
})
$('.nopass-list').html(str)
}
},'get')
bindEvents();
addMedicineDate($('.j-selDate'),false)
//添加药品
window.addEventListener("addMedicine", function(){
var content = JSON.parse(plus.storage.getItem("chooseMedicineList"));
var arr=[];
//判断是否重复
var again=false;
for(var i=0;i<$('.j-medi-name').length;i++){
if($('.j-medi-name').eq(i).attr('data-id') == content.drugCode){
again = true;
}
}
if(again){
mui.toast('重复添加药品')
}else{
arr.push(content);
var medi_html = template('medi_tmp',{data:$.map(arr,function(o,index){
o.jsonVal = JSON.stringify(o);
return o })
})
$("#mainContain").append(medi_html);
addMedicineFreq($('.j-selFreq:last'),true)
addMedicineDate($('.j-selDate:last'),true)
}
plus.storage.removeItem("chooseMedicineList")
})
//修改诊断
window.addEventListener("reviseDiagnosis", function(){
var content = JSON.parse(plus.storage.getItem("reviseDiagnosis"));
diagnosis = content;
isChangeDiagnosis = 1;
var diaArr=[]
$.map(content,function(item,index){
diaArr.push(item.name)
})
$('#j-sick-choose').html(diaArr.join(','))
plus.storage.removeItem("reviseDiagnosis")
})
//采用模板
window.addEventListener("chooseTemplate", function(){
var content = JSON.parse(plus.storage.getItem("chooseTemplate"));
content.prescriptionInfo = _.uniq(content.prescriptionInfo)//去重
plus.nativeUI.showWaiting();
//整理药品的数据
var countNum = 1;
var newData=[];
$.map(content.prescriptionInfo,function(o,index){
toMediMsg(o.drugCode,function(msg){
o.subjectClass = msg.subjectClass;
o.storageConditions = msg.storageConditions;
o.physicAmount = '';
o.physicAmountUnit = msg.packUnit;
o.physicAmountUnitName = msg.packUnitName;
o.jsonVal = JSON.stringify(o);
newData.push(o);
if(content.prescriptionInfo.length == countNum){
plus.nativeUI.closeWaiting();
var medi_html = template('medi_tmp',{data:newData})
$("#mainContain").empty().append(medi_html);
addMedicineFreq($('.j-selFreq'),false)
addMedicineDate($('.j-selDate'),false)
plus.storage.removeItem("chooseTemplate")
}else{
countNum++;
}
})
})
})
}
})
function bindEvents(){
var $text_count = $("#text_count");
//文本框
$("textarea").on('keyup', function(){
var count = $(this).val().length;
$text_count.text(count);
})
$("textarea").on('change', function(){
var count = $(this).val().length;
$text_count.text(count);
})
//调整原因
$('.nopass-list').on('click','li',function(){
var $this = $(this)
if($this.hasClass('active')){
$this.removeClass('active')
}else{
$this.addClass('active')
}
fillWord()
})
function fillWord(){
var str=''
$.map($('.nopass-list li.active'),function(o,i){
str += $('.nopass-list li.active').eq(i).text()+',';
})
$('textarea').val(str.substr(0,str.length-1))
$text_count.text(str.length-1);
}
//增减
$("#mainContain").on('click','.add-icon',function(){
var $this = $(this)
var $val = $this.siblings('.j-medi-single').val().trim()
$this.siblings('.j-medi-single').val($val*1+0.25)
countMediSum($(this).closest('.cc-ul'))
})
$("#mainContain").on('click','.desc-icon',function(){
var $this = $(this)
var $val = $this.siblings('.j-medi-single').val().trim()
var num = $val*1-0.25
if(num>=0){
$this.siblings('.j-medi-single').val($val*1-0.25)
}else{
$this.siblings('.j-medi-single').val(0)
}
countMediSum($(this).closest('.cc-ul'))
})
//用法选择
$("#mainContain").on('click','.j-method',function(){
var $this = $(this)
var num = $this.closest('.cc-ul').index()
add_selected_fun(methodDire,$('.j-method').eq(num),'name','code')
})
//删除
$("#medicineType").on('click','.j-delete',function(){
var $this = $(this).closest('.cc-ul');
$this.remove();
})
//添加药品
$('.cc-add-drug').click(function(){
mui.openWindow({
id: "yaopinsousuo",
url: "yaopinsousuo.html",
extras: {
}
})
})
//选择模板
$('.cc-link').click(function(){
mui.openWindow({
id: "chufangmuban",
url: "chufangmuban.html",
extras: {
}
})
})
//修改疾病类型
$('#j-sick-choose').click(function(){
mui.openWindow({
id: "linchuangzhenduan",
url: "linchuangzhenduan.html",
extras: {
diagnosis:isChangeDiagnosis?diagnosis:usefulMsg.diagnosis
}
})
})
//提交
$('#confirm').click(function(){
var $sum = $(".j-sumMedicine");
if($sum.length > 0){
for(var i=0;i<$sum.length;i++){
if(! $sum.eq(i).attr('data-num')){
mui.toast('请完善药品信息')
return
}
}
}else{
mui.toast('请添加药品')
return
}
plus.nativeUI.showWaiting();
var msgArr = [],
$ul = $('.cc-ul');
for(var i=0;i<$ul.length;i++){
var data = {};
var saveData = JSON.parse($ul.eq(i).attr('data-info'));
data.prescriptionCode = prescriptionCode;
data.drugCode = saveData.drugCode;
data.drugName = $ul.eq(i).find('.j-medi-name').val().replace(/\(/,'(').replace(/\)/,')');
data.drugRate = $ul.eq(i).find('.j-selFreq').attr('data-code');
data.drugRateName = $ul.eq(i).find('.j-selFreq').attr('data-name');
data.drugFormat=saveData.drugFormat.replace(/\(/,'(').replace(/\)/,')');
data.subjectClass = $ul.eq(i).find('.j-medi-name').attr('data-class');
data.isRefrigerate = $ul.eq(i).find('.j-medi-name').attr('data-storage')==2?1:0;
data.physicAmount = "";
data.physicAmountUnit = saveData.physicAmountUnit;
data.physicAmountUnitName = saveData.physicAmountUnitName;
data.physicDose = $ul.eq(i).find('.j-medi-single').val().trim();
data.physicDoseUnit = $ul.eq(i).find('.j-medi-unit').attr('data-unit');
data.physicDoseUnitName = $ul.eq(i).find('.j-medi-unit').text();
data.drugNumUnit = $ul.eq(i).find('.j-sumMedicine').attr('data-unit');//取当次用量单位
data.drugNumUnitName = $ul.eq(i).find('.j-sumMedicine').attr('data-name');
data.drugUsage= $ul.eq(i).find('.j-method').attr('data-id');
data.usageName= $ul.eq(i).find('.j-method').val();
data.num= $ul.eq(i).find(".j-sumMedicine").attr('data-num');//取总量
data.dayCount=$ul.eq(i).find('.j-selDate').attr('data-id');
msgArr.push(data);
}
//调整CODE
var reasonCode=[];
$.map($('.nopass-list li.active'),function(o,i){
reasonCode.push($('.nopass-list li.active').eq(i).attr('data-code'))
})
var params= {
code:prescriptionCode,
infos:JSON.stringify(msgArr),
reason:$('textarea').val().trim()?$('textarea').val().trim():'-',
reasonCode:reasonCode.join(','),
diagnosis:isChangeDiagnosis?JSON.stringify(diagnosis):''
}
sendPost("doctor/prescriptionInfo/updatePresInfo",params, function(){
plus.nativeUI.closeWaiting();
mui.toast("保存失败");
}, function(res){
if(res.status == 200){
mui.toast("保存成功");
refreshHz();
setTimeout(function(){
mui.back()
},1000)
}else{
plus.nativeUI.closeWaiting();
mui.toast("保存失败");
}
})
})
//填写药品
$("#mainContain").on("blur",".j-medi-name",function(){
countMediSum($(this).closest('.cc-ul'))
})
//填写次剂量
$("#mainContain").on("blur",".j-medi-single",function(){
countMediSum($(this).closest('.cc-ul'))
})
$("#mainContain").on("change",".j-medi-single",function(){
var $val = $(this).val().trim()
var reg = /^[0-9]+(.[0-9]{1,5})?$/;
if($val && reg.test($val)){
}else{
$(this).val('')
}
})
//刷新页面
function refreshHz(){
var page3 = plus.webview.getWebviewById("xufangxiangqing");
if(page3){
mui.fire(page3, "refreshXufang");
}
var page4 = plus.webview.getWebviewById("juminxufangxiangqing");
if(page4){
mui.fire(page4, "refreshXufang");
}
var page = plus.webview.getWebviewById("xufangguanli");
if(page){
mui.fire(page, "refreshXufang");
}
var page1 = plus.webview.getWebviewById("juminxufangjilu");
if(page1){
mui.fire(page1, "refreshXufang");
}
}
}
//获取频率换算
function toFreqRate(code,fun){
var data={
code:code
}
sendPost("doctor/prescriptionInfo/drugToRate",data, function(){
plus.nativeUI.closeWaiting();
mui.toast("获取用药频率数据失败");
}, function(res){
if(res.status == 200){
fun&&fun.call(this,res.data.rate)
}
},'get')
}
//获取药品信息
function toMediMsg(code,fun){
var data={
code:code
}
sendPost("doctor/prescriptionInfo/getPhysicInfo",data, function(){
mui.toast("获取药品信息失败");
}, function(res){
if(res.status == 200){
fun&&fun.call(this,res.data[0])
}
},'get')
}
//用药频率
function addMedicineFreq($id,newADD){
for(i=0; i<$id.length; i++){
var html = '';
//取频率code
if(newADD){
var obj = JSON.parse($('.cc-ul:last').attr('data-info'));
}else{
var obj = JSON.parse($('.cc-ul').eq(i).attr('data-info'));
}
var code = obj.drugRate
for(j=0;j'+medicineFreqData[j].name+'';
}else{
html += '';
}
}
$id.eq(i).html(html);
$id.eq(i).mobiscroll().select({
theme: 'ios',
lang: 'zh',
display: 'bottom',
placeholder:'请选择用药频次',
rows:7,
onSelect: function ( valueText, inst) {
var selectedData = {};
$.map(medicineFreqData,function(item,index){
if(item.name == valueText){
selectedData = item;
return;
}
})
$(this).closest('.j-selFreq').attr('data-code',selectedData.code);
$(this).closest('.j-selFreq').attr('data-name',selectedData.name);
countMediSum($(this).closest('.cc-ul'))
}
})
}
}
//用药天数
function addMedicineDate($id,newADD){
for(i=0; i<$id.length; i++){
var html = ''
//取天数
if(newADD){
var obj = JSON.parse($('.cc-ul:last').attr('data-info'));
}else{
var obj = JSON.parse($('.cc-ul').eq(i).attr('data-info'));
}
var num = obj.dayCount;
for(j=1;j<31;j++){
if(num == j){
html += '';
}else{
html += '';
}
}
$id.eq(i).html(html);
$id.eq(i).mobiscroll().select({
theme: 'ios',
lang: 'zh',
display: 'bottom',
placeholder:'请选择用药天数',
rows:7,
onSelect: function ( valueText, inst) {
$(this).closest('.j-selDate').attr('data-id',valueText);
countMediSum($(this).closest('.cc-ul'))
}
})
}
}
//计算药品总量
function countMediSum($ul){
$ul.find(".j-sumMedicine").html("")//清空
$ul.find(".j-sumMedicine").attr('data-num','')//清空
var $name = $ul.find(".j-medi-name").val().trim(),
$single = $ul.find(".j-medi-single").val().trim(),
$freq = $ul.find(".j-selFreq").attr('data-code'),
$date = $ul.find(".j-selDate").attr('data-id'),
$info = JSON.parse($ul.attr('data-info'));
if($name && $single && $freq && $date){
toFreqRate($freq,function(rate){
var num = $single*rate*$date
num = Math.round(num*100)/100
var sum="";
if($info.pakeSpec){
//新增药品
sum = Math.ceil(num/$info.minDose)
showResultSum(sum);
}else{
var drugNumUnit = $info.drugNumUnit,
physicDoseUnit = $info.physicDoseUnit;
//请求药品信息
var drugCode = $info.drugCode;
toMediMsg(drugCode,function(msg){
$ul.find(".j-medi-name").attr('data-storage',msg.storageConditions);//冷藏
$ul.find(".j-medi-name").attr('data-class',msg.subjectClass);
if(drugNumUnit == physicDoseUnit){
sum = num
}else{
if(drugNumUnit == msg.pack_unit){
sum = Math.ceil(num/msg.packSpec)
}else{
sum = Math.ceil(num/msg.minDose)
}
}
showResultSum(sum);
})
}
})
}
function showResultSum(sum){
$ul.find(".j-sumMedicine").html(sum+$info.drugNumUnitName);
$ul.find(".j-sumMedicine").attr('data-num',sum);
$ul.find(".j-sumMedicine").attr('data-name',$info.drugNumUnitName);
$ul.find(".j-sumMedicine").attr('data-unit',$info.drugNumUnit);
}
}