<!-- 快捷添加巡视单页面专用 -->
<template>
  <view>
	<nav-bar :navbarData="navbarData"></nav-bar>
    <view :style="'margin-top: ' + (height * 2 - 10) + 'rpx; height: calc(100vh - ' + (height * 2 - 2) + 'rpx)'">
      <view class="content" :style="editFlag !== 'E' ? 'height:95%' : ''">
		  <view v-if="pdaScanStatus === 'N'" class="topinfo">
			  <view class="item">
				  <view class="bedcode">{{ patInfo.admInfo.admBedCode }}</view>
				  <view class="border"></view>
				  <view class="info">{{ patInfo.basicInfo.patName }}</view>
				  <view v-if="patInfo.admInfo.nurseLv === '特级'" class="nurse-lv-css-red">{{ patInfo.admInfo.nurseLv }}</view>
				  <view v-if="patInfo.admInfo.nurseLv === '一级'" class="nurse-lv-css-pink">{{ patInfo.admInfo.nurseLv }}</view>
				  <view v-if="patInfo.admInfo.nurseLv === '二级'" class="nurse-lv-css-blue">{{ patInfo.admInfo.nurseLv }}</view>
				  <view v-if="patInfo.admInfo.nurseLv === '三级'" class="nurse-lv-css-greenyellow">{{ patInfo.admInfo.nurseLv }}</view>
				  <view class="bedcode">{{ patInfo.basicInfo.patSexDesc }}</view>
				  <view class="border"></view>
				  <view class="info">{{ patInfo.basicInfo.patAge }}</view>
				  <view class="border"></view>
				  <view class="info">{{ patInfo.basicInfo.patNo }}</view>
				   <view class="border"></view>
				  <view class="info">入院：{{ patInfo.admInfo.admDatetime }}</view>
			  </view>
		  </view>
		  <view v-if="pdaScanStatus === 'Y'" class="topinfo">
		    <view class="scan-container">
		      <!-- 扫描提示图片 -->
		      <image 
		        class="scan-img" 
		        src="/static/pdaphone.jpeg"
		        mode="widthFix"
		      ></image>
		      <!-- 闪烁提示文字 -->
		      <view class="scan-text">请点击PDA侧边按钮扫描患者标识</view>
		    </view>
		  </view>
		  
        <view v-if="pdaScanStatus === 'N'" class="elementitem" v-for="(item, index) in elementData" :key="index">
          <van-row class="item">
            <van-col 
              v-if="item.hideTitle !== 'Y'" 
              :span="item.elementTypeCode === 'Label' ? '24' : '9'"
              :class="item.elementTypeCode === 'Label' ? 'title' : 'itemtitle'"
            >
              <view :class="item.elementTypeCode === 'Label' ? 'title' : ''">
                <view v-if="item.elementTypeCode === 'Label'" class="border"></view>
                <view>{{ item.IEMRTempElementDesc }}{{ item.elementTypeCode === 'Label' ? '' : '：' }}</view>
              </view>
            </van-col>
            <van-col :span="item.hideTitle !== 'Y' ? '15' : 24" class="itemdata">
              <view v-if="item.elementTypeCode === 'Input'" class="inputclass">
				<input 
				  :data-dataindex="index"
				  v-model="item.IEMRTempElementData"
				  type="text"
				  @input="(e) => inputOnChange(e.detail.value, { dataindex: index })"
				  @blur="(e) => inputOnBlur(e.detail.value, { dataindex: index })"
				  :placeholder="item.addItemFlag === 'Y' ? '总和' : '请输入'"
				  :disabled="item.disable === 'Y'"
				  style="margin-top: 6px;width: 100%;padding: 2px 0;border: none;outline: none;font-size: 14px;"
				/>
              </view>
              
              <view v-if="item.elementTypeCode === 'ScanCode'" class="scanCodeclass">
                <view 
                  :class="item.IEMRTempElementData === '腕带核对正确' ? 'scanViewColor_green' : 'scanViewColor_red'"
                >
                  {{ item.IEMRTempElementData }}
                </view>
                <image 
                  v-if="item.IEMRTempElementData !== '腕带核对正确'"
                  :src="imgDomain + '/images/icon/ydhl_scanBlue.png'" 
                  :data-dataindex="index"
                  class="custom-icon" 
				  @tap="(e) => inputScanOnChange(e, { dataindex: index })"
                />
              </view>
              
              <view v-if="item.elementTypeCode === 'Labelarea'" class="areaclass">
				<input
				  :data-dataindex="index"
				  v-model="item.IEMRTempElementData"
				  type="textarea"
				  @input="(e) => inputOnChange(e.detail.value, { dataindex: index })"
				  placeholder="请输入" 
				  :disabled="item.disable === 'Y'"
				  style="margin-top: 6px;width: 100%;padding: 2px 0;border: none;outline: none;font-size: 14px;"/>
              </view>
              
              <view v-if="item.elementTypeCode === 'ComboBox'" class="comboboxclass">
                <van-row>
                  <checkbox-group @change="checkChange" :data-dataindex="index">
                    <view class="radio-container">
                      <view 
                        v-for="(checkitem, checkid) in item.elmentData"
                        :span="checkitem.descripts.length > 4 ? 24 : 12" 
                        :key="checkid"
                        style="border-bottom:1px solid #e4e5e4" 
                        :class="checkitem.descripts.length > 4 ? 'rowitem2' : 'rowitem'"
                      >
                        <checkbox 
                          :value="checkitem.descripts" 
                          shape="square"
                          :disabled="item.disable === 'Y'"
                          :checked="getCheckFlag(item.IEMRTempElementData, checkitem.descripts)"
                        >
                          {{checkitem.descripts}}
                        </checkbox>
                      </view>
                    </view>
                  </checkbox-group>
                </van-row>
              </view>
              
              <view v-if="item.elementTypeCode === 'RadioBox'" class="radioboxclass">
                <van-row>
                  <radio-group @change="checkChange" :data-dataindex="index">
                    <view class="radio-container">
                      <view 
                        v-for="(checkitem, checkid) in item.elmentData" 
                        :key="checkid"
                        :class="checkitem.descripts.length > 4 ? 'rowitem2' : 'rowitem'"
                      >
                        <radio 
                          :value="checkitem.descripts"
                          :checked="item.IEMRTempElementData.value === checkitem.descripts || item.IEMRTempElementData === checkitem.descripts"
                        >
                          {{checkitem.descripts}}
                        </radio>
                      </view>
                    </view>
                  </radio-group>
                </van-row>
              </view>
              
              <view v-if="item.elementTypeCode === 'Select'" class="selectclass">
                <van-cell 
                  :is-link="item.IEMRTempElementData === ''" 
                  arrow-direction="down"
                  :value="item.IEMRTempElementData" 
                  :border="false" 
                  :data-dataindex="index"
                  @tap="(e) => selectItem(e, { dataindex: index })"
                >
					<span>{{item.IEMRTempElementData}}</span>
                  <van-icon 
                    v-if="item.IEMRTempElementData !== '' && item.disable !== 'Y'" 
                    name="cross"
                    slot="right-icon" 
                    custom-class="delicon" 
                    :data-dataindex="index"
                    @tap.stop.prevent="(e) => deleteData(e, { dataindex: index })"
                  ></van-icon>
                </van-cell>
              </view>
              
              <view v-if="item.elementTypeCode === 'MultipleSelect'" class="selectclass">
                <van-cell 
                  is-link 
                  arrow-direction="down" 
                  :value="getData(item.IEMRTempElementData)"
                  :border="false" 
                  :data-dataindex="index"
                  @tap="(e) => multipleSelectItem(e, { dataindex: index })" 
                />
              </view>
              
            <view v-if="item.elementTypeCode === 'Date'" class="dateclass">
              <uni-datetime-picker
                type="date"  
				:hide-second="true"
				:model-value="item.IEMRTempElementData"
				@show="openUniDateTimePicker(index, item)"
				@change="(e) => handleUniDateTimeConfirm(e,index)"  
                title="选择日期"        
                :border="false"
              />
			  </view>
			  
              <view v-if="item.elementTypeCode === 'Time'" class="dateclass">
				<picker 
					mode="multiSelector" 
					:range="timeRange" 
					:value="timeIndex" 
					@change="(e)=>onTimeChange(e,index)"
					@columnchange="onColumnChange">
					<view class="picker-view">{{item.IEMRTempElementData || '点击选择时间'}}</view>
				 </picker>
              </view>
              
              <view v-if="item.elementTypeCode === 'DateTime'">
				  <uni-datetime-picker
				    type="datetime"  
					:hide-second="true"
					:model-value="item.IEMRTempElementData"
					@show="openUniDateTimePicker(index, item)"
					@change="(e) => handleUniDateTimeConfirm(e,index)"  
				    title="选择日期时间"        
				    :border="false"
				  />
              </view>
            </van-col>
          </van-row>
        </view>
      </view>
      
      <view class="multiBtn-content">
		<button class="multiBtn-left" @tap="goBackPage">首页</button>
        <button v-if="pdaScanStatus === 'N'" class="multiBtn-right" @tap="addRecord">保存</button>
      </view>
    </view>
    
    <van-action-sheet 
      :show="actionShow" 
      :actions="selectData" 
      @close="onClose" 
      @cancel="onClose" 
      @select="onSelect"
      cancel-text="取消" 
    />
    
    <van-action-sheet 
      :show="mutilCheckshow" 
      title="请选择" 
      @close="onClose" 
      @cancel="onClose"
    >
      <view class="mutilCheck">
        <view class="checkData">
          <view 
            span="8" 
            class="checkitem"
            :style="'background:' + multicheckitem.color + '; border-color:' + multicheckitem.color + ';color:' + (multicheckitem.checked ? '#fff' : '#333')"
            :data-index="multicheckid" 
            @tap="multiItemCheck"
            v-for="(multicheckitem, multicheckid) in selectData" 
            :key="multicheckid"
          >
            {{ multicheckitem.descripts }}
          </view>
        </view>
        <view>
          <view class="multiBtn-content">
            <button class="multiBtn-left" @tap="onClose">取消</button>
            <button class="multiBtn-right" @tap="onConfirmMultiCheck">保存</button>
          </view>
        </view>
      </view>
    </van-action-sheet>
  </view>
</template>

<script setup>
import { ref, onMounted,onUnmounted, computed, defineExpose } from 'vue';
import { $http } from '../../config/https';
import { onLoad ,onShow } from '@dcloudio/uni-app';
import Util from '../../utils/util.js';
import navBar from '@/pages/components/navbar/index';

// 状态定义
const height = ref('');
const templateID = ref('');
const newFlag = ref('0');
const elementData = ref([]);
const selectData = ref([]);
const actionShow = ref(false);
const mutilCheckshow = ref(false);
const clickIndex = ref('-1');
const currentDate = ref(getCurrentDateTime());
const admID_SM = ref('');
const patID_SM = ref('');
const editFlag = ref('');
const checkitem = ref({ descripts: [] });
const checkid = ref(false);
const multicheckitem = ref({ checked: false, color: '', descripts: '' });
const multicheckid = ref('');
const imgDomain = ref(uni.getStorageSync('appUrlAddress'));
const navbarData = ref({
  background: '',
  title: '记录',
  height: '100',
  bottom: true,
  isGoBack: true,
  isGoHome: true,
  cleanPat: 'Y'
});
const patInfo = ref({});
const pdaScanStatus = ref('Y');	// Y:需要扫码  N：已有患者信息

// 计算属性
const getData = (dataArray) => {
  if (dataArray !== "") {
    return dataArray.join("、");
  } else {
    return "";
  }
};

const getCheckFlag = (a, b) => {
  if (a.value) {
    return a.value.indexOf(b) === -1 ? false : true;
  } else {
    return a.indexOf(b) === -1 ? false : true;
  }
};

// 时间范围数据
const timeRange = ref([])
// 选中的时间索引
const timeIndex = ref([0, 0])
// 初始化时间范围
const initTimeRange = () => {
  // 生成小时数据 (0-23)
  const hours = Array.from({ length: 24 }, (_, i) => i.toString().padStart(2, '0'))
  // 生成分钟数据 (0-59)
  const minutes = Array.from({ length: 60 }, (_, i) => i.toString().padStart(2, '0'))
  timeRange.value = [hours, minutes]
  // 设置默认时间为当前时间
  const now = new Date()
  timeIndex.value = [
    now.getHours(),
    now.getMinutes()
  ]
}
// 当选择器的值改变时触发
const onTimeChange = (e,index) => {
  timeIndex.value = e.detail.value
  const [hour, minute] = timeIndex.value
  const timeValue = `${timeRange.value[0][hour]}:${timeRange.value[1][minute]}`
  const item = elementData.value[index];
  if (item.elementTypeCode === 'Time') {
    item.IEMRTempElementData = timeValue;
    item.IEMRTempElementPrintData = timeValue;
    elementData.value = [...elementData.value];
  }
}

// uni-datetime-picker-默认当前日期时间
const formatDateTime = (elementTypeCode) => {
  const date = new Date();
  if (elementTypeCode === 'DateTime') {
    return `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
  } else if (elementTypeCode === 'Date') {
    return `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
  }
};

// uni-datetime-picker-打开日期时间选择器方法
const openUniDateTimePicker = (index, item) => {
  if (!item.IEMRTempElementData) { 
	  item.IEMRTempElementData = formatDateTime(item.elementTypeCode)
  }
};

// uni-datetime-picker确认选择
const handleUniDateTimeConfirm = (e,index) => {
  if (index === -1) return;
  const item = elementData.value[index];
	item.IEMRTempElementData = e;
	item.IEMRTempElementPrintData = e;
	elementData.value = [...elementData.value];
};


onLoad((options) => {
    const app = getApp();
    newFlag.value = options.newFlag;
    editFlag.value = options.editFlag;
    templateID.value = options.templateID;
    admID_SM.value = options.admID;
    patID_SM.value = options.patID;
	navbarData.value.title = options.title;
    imgDomain.value = uni.getStorageSync('appUrlAddress');
});

onShow(() => {
  try {
    const pages = getCurrentPages()
    if (pages.length > 0) {
      const currentPage = pages[pages.length - 1]
      const currentRoute = currentPage.route // 当前页面路由
      // 更新全局变量
      if (uni.$appGlobal) {
        uni.$appGlobal.globalData.value.currentRoute = currentRoute
      }
    }
  } catch (err) {
    console.error('更新当前路由失败：', err)
  }
})


// 生命周期
onMounted(() => {
	 initTimeRange();
	 // 注册全局事件
	   uni.$on('callGetPDAScanInfo', (code) => {
	     getPDAScanInfo(code);
	   });
});
onUnmounted(() => {
  // 销毁全局事件，避免内存泄漏
  uni.$off('callGetPDAScanInfo');
});

// 方法定义
function getCurrentDateTime() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0');
  const day = String(now.getDate()).padStart(2, '0');
  const hours = String(now.getHours()).padStart(2, '0');
  const minutes = String(now.getMinutes()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}`;
}
//获取单据数据
function getTemplateData(templateID, recordID, linktemplateID, disable) {
  const patInfo = uni.getStorageSync('patInfo');
  $http.post('urlDeault',this, {
    code: '10020016',
    data: {
      params: [{
        IEMRRecordID: recordID,
        IEMRTemplateID: templateID,
        status: 'Y',
        linktemplateID,
        disable,
        admID: patInfo.admInfo.admID
      }]
    },
    success: function(res) {
      if (+res.errorCode === 0) {
        elementData.value = res.result;
        addValue();
      }
    }
  });
}
//获取单据目录
function getTemplateElement(templateID,scanPatNo) {
  const patInfo = uni.getStorageSync('patInfo');
  $http.post('urlDeault',this, {
    code: '10020015',
    data: {
      params: [{
        IEMRTemplateID: templateID,
        admID: patInfo.admInfo.admID
      }]
    },
    success: function(res) {
      if (+res.errorCode === 0) {
        elementData.value = res.result;
		//新巡视单，直接核对腕带数据
		if(templateID === '306'){
			const item = elementData.value[0];
			const result = scanPatNo === patInfo.basicInfo.patNo 
			  ? '腕带核对正确' : '腕带核对不正确';
			item.IEMRTempElementData = result;
			item.IEMRTempElementPrintData = result;
			elementData.value = [...elementData.value];
		}
        addValue();
      }
    }
  });
}

function selectItem(e, _dataset) {
  handleDataset(e, _dataset);
  const { dataindex } = e.currentTarget.dataset;
  const item = elementData.value[dataindex];
  if (item.disable === 'Y') {
    showToast({
      icon: 'none',
      title: '该元素已被禁用，不可编辑'
    });
    return;
  }
  const newSelectData = item.elmentData.map(data => {
    const isSelected = item.IEMRTempElementData === data.descripts || item.IEMRTempElementData === data.value;
    return {
      name: data.descripts,
      color: isSelected ? '#1890FF' : '#000'
    };
  });
  selectData.value = newSelectData;
  actionShow.value = true;
  clickIndex.value = dataindex;
}

function multipleSelectItem(e, _dataset) {
  handleDataset(e, _dataset);
  const { dataindex } = _dataset;
  const item = elementData.value[dataindex];
  const value = item.IEMRTempElementData || [];
  const newSelectData = item.elmentData.map(data => {
    const isChecked = value.some(v => v === data.descripts);
    return {
      ...data,
      color: isChecked ? '#1890FF' : '#e4e5e4',
      checked: isChecked
    };
  });
  selectData.value = newSelectData;
  mutilCheckshow.value = true;
  clickIndex.value = dataindex;
}

function multiItemCheck(e) {
  const { index } = e.currentTarget.dataset;
  const item = selectData.value[index];
  item.checked = !item.checked;
  item.color = item.checked ? '#1890FF' : '#e4e5e4';
  selectData.value = [...selectData.value];
}

function onConfirmMultiCheck() {
  const dataindex = clickIndex.value;
  const item = elementData.value[dataindex];
  let printData = '';
  const data = [];
  selectData.value.forEach(selectItem => {
    if (selectItem.checked) {
      data.push(selectItem.descripts);
      printData = printData 
        ? `${printData};${item.printValue === 'Y' ? selectItem.value : selectItem.descripts}`
        : `${item.printValue === 'Y' ? selectItem.value : selectItem.descripts}`;
    }
  });
  item.IEMRTempElementData = data;
  item.IEMRTempElementPrintData = printData;
  elementData.value = [...elementData.value];
  mutilCheckshow.value = false;
  addValue();
}

function onSelect(e) {
  const dataindex = clickIndex.value;
  const item = elementData.value[dataindex];
  const data = e.name;
  let printData = data;
  if (item.printValue === 'Y') {
    const found = item.elmentData.find(d => d.descripts === data);
    if (found) printData = found.value;
  }
  item.IEMRTempElementData = data;
  item.IEMRTempElementPrintData = printData;
  elementData.value = [...elementData.value];
  onClose();
  addValue();
}

function onClose() {
  mutilCheckshow.value = false;
  actionShow.value = false;
  selectData.value = [];
  clickIndex.value = '-1';
}

//暴露给外部函数，获取PDA扫码反馈
function getPDAScanInfo(code) {
	Util.getPatInfoNew(code, (res) => {
		const newPatInfo = res;
		if (Object.keys(patInfo.value).length === 0) {
			patInfo.value = { ...newPatInfo }; // 初始化响应式对象
		} else {
			Object.assign(patInfo.value, newPatInfo);
		}
		pdaScanStatus.value = 'N';
		getTemplateElement(templateID.value,code);
	  });
}

function inputScanOnChange(e, _dataset) {
  const patInfo = uni.getStorageSync('patInfo');
  const { dataindex } = e.currentTarget.dataset;
  uni.scanCode({
    success(res) {
      const item = elementData.value[dataindex];
      const result = res.result === patInfo.basicInfo.patNo 
        ? '腕带核对正确' 
        : '腕带核对不正确';
      item.IEMRTempElementData = result;
      item.IEMRTempElementPrintData = result;
      elementData.value = [...elementData.value];
      addValue();
    }
  });
}

// v-model绑定了变量 item.IEMRTempElementData,绑定的变量已自动更新输入框内容
function inputOnChange(e, { dataindex }) {
  const item = elementData.value[dataindex];
  item.IEMRTempElementPrintData = item.IEMRTempElementData;
  elementData.value = [...elementData.value];
  addValue();
}

function inputOnBlur(e, { dataindex }) {
  const item = elementData.value[dataindex];
  let value = item.IEMRTempElementData.replace(item.unit, '');
  if (item.unit && !isNaN(value) && value !== '') {
    value += item.unit;
  }
  item.IEMRTempElementData = value;
  item.IEMRTempElementPrintData = value;
  elementData.value = [...elementData.value];
  addValue();
}

function deleteData(e, { dataindex }) {
  handleDataset(e, { dataindex });
  const item = elementData.value[dataindex];
  item.IEMRTempElementData = '';
  item.IEMRTempElementPrintData = '';
  elementData.value = [...elementData.value];
  addValue();
}

function checkChange(e, _dataset) {
  handleDataset(e, _dataset);
  const { dataindex } = e.currentTarget.dataset;
  const item = elementData.value[dataindex];
  const data = e.detail.value;
  const printData = [];
  if (item.printValue === 'Y') {
    item.elmentData.forEach(el => {
      data.forEach(d => {
        if (el.descripts === d) {
          printData.push(el.value);
        }
      });
    });
  } else {
    item.elmentData.forEach(el => {
		if (Array.isArray(data)) {
		  data.forEach(d => {
		    if (el.descripts === d) {
		      printData.push(el.descripts);
		    }
		  });
		}
    });
  }
  if (Array.isArray(data) && data.length > 1) {
    item.IEMRTempElementData = printData;
    item.IEMRTempElementPrintData = printData;
  } else {
    item.IEMRTempElementData = e.detail.value;
    item.IEMRTempElementPrintData = e.detail.value;
  }
  
  elementData.value = [...elementData.value];
  addValue();
}
// 回到首页
function goBackPage(){
	uni.reLaunch({ url: '../index/index' });
}
// 保存事件
function addRecord() {
  addValue();
  const patInfo = uni.getStorageSync('patInfo');
  const userData = uni.getStorageSync('userData');
  const recordID = newFlag.value !== '0' ? newFlag.value : '';
  $http.post('urlDeault',this, {
    code: '10020003',
    data: {
      params: [{
        IEMRTemplateID: templateID.value,
        admID: admID_SM.value || patInfo.admInfo?.admID,
        patID: patID_SM.value || patInfo.basicInfo?.patID,
        updateUserID: userData.userID,
        IEMRRecordID: recordID,
        detailData: elementData.value
      }]
    },
    success: function(res) {
      if (+res.errorCode === 0) {
        uni.showToast({
          icon: 'none',
          title: '保存成功！',
          success: () => {
			patInfo.value = {};
			pdaScanStatus.value = 'Y';	// Y:需要扫码  N：已有患者信息
          }
        });
      } else {
        uni.showToast({
          icon: 'none',
          title: res.errorMessage
        });
      }
    }
  });
}

function addValue() {
  var addElement = [];
  let elementDataN = elementData.value;
  elementData.value.map((item, index) => {
  	if (item.addItemFlag == 'Y') {
  		item.index = index;
  		addElement[item.IEMRTempElementID] = item;
  		elementData.value[index].IEMRTempElementData = elementData.value[index].defaultValue || 0;
  		elementData.value[index].IEMRTempElementPrintData = elementData.value[index].defaultValue || 0;
  	}
  });
  elementData.value.map((item) => {
  	if (item.addItemID != null && item.addItemID != '') {
  		var value = '';
  		var valueDesc = item.IEMRTempElementData;
  		if (Array.isArray(valueDesc)) {
  			item.elmentData.map((detail) => {
  				valueDesc.map((valueitem) => {
  					if ((detail.descripts == valueitem || detail.code ==
  							valueitem || detail.value == valueitem) &&
  						valueitem != '') {
  						if (isNaN(valueitem) && value == '0') {
  							value = '';
  						}
  						if (value != '') {
  							value = value + ';';
  						}
  						if (addElement[item.addItemID].printValue == 'Y') {
  							value = value + detail.value;
  						} else {
  							value = value + detail.descripts;
  						}
  					}
  				});
  			});
  		} else if (item.elmentData.length > 0) {
  			item.elmentData.map((detail) => {
  				if (detail.descripts == valueDesc) {
  					value = detail.value;
  				}
  			});
  		} else {
  			value = valueDesc;
  		}
  		if (value != '' && value != 0) {
  			var tempvalue = [];
  			if ((value + '').indexOf(';') < 0) {
  				tempvalue.push(value);
  			} else {
  				tempvalue = value.split(';');
  			}
  			for (var i = 0; i < tempvalue.length; i++) {
  				value = tempvalue[i];
  				if (addElement && addElement.length > 0 && item.addItemID && addElement[item
  						.addItemID] && addElement[item.addItemID].index) {
  					if (
  						addElement[item.addItemID] &&
  						addElement[item.addItemID].IEMRTempElementData != '' &&
  						addElement[item.addItemID].IEMRTempElementData != undefined
  					) {
  						if (!isNaN(value)) {
  							elementData.value[addElement[item.addItemID].index].IEMRTempElementData =
  								Number(elementData.value[addElement[item.addItemID].index]
  									.IEMRTempElementData) + Number(value);
  							elementData.value[addElement[item.addItemID].index]
  								.IEMRTempElementPrintData = elementData.value[addElement[item
  									.addItemID].index].IEMRTempElementData;
  						} else {
  							elementData.value[addElement[item.addItemID].index].IEMRTempElementData =
  								elementData.value[addElement[item.addItemID].index]
  								.IEMRTempElementData + ';' + value;
  							elementData.value[addElement[item.addItemID].index]
  								.IEMRTempElementPrintData = elementData.value[addElement[item
  									.addItemID].index].IEMRTempElementData;
  						}
  					} else {
  						if (!isNaN(value)) {
  							elementData.value[addElement[item.addItemID].index].IEMRTempElementData =
  								Number(value);
  							elementData.value[addElement[item.addItemID].index]
  								.IEMRTempElementPrintData = Number(value);
  						} else {
  							elementData.value[addElement[item.addItemID].index].IEMRTempElementData =
  								value;
  							elementData.value[addElement[item.addItemID].index]
  								.IEMRTempElementPrintData = value;
  						}
  					}
  				}
  			}
  		}
  	}
  });
  //遍历总和项
  addElement.map((item, itemIndex) => {
  	var index = item.index;
  	if (item.elementTypeCode == 'Select') {
  		var data = elementData.value[index].IEMRTempElementData;
  		var printData = elementData.value[index].IEMRTempElementPrintData;
  		elementData.value[index].elmentData.map((dataitem, dataIndex) => {
  			if (dataitem.value.indexOf('-') < 0) {
  				if (dataitem.value == data) {
  					elementData.value[index].IEMRTempElementData = dataitem.descripts;
  					if (elementData.value[index].printValue == 'Y') {
  						//打印值
  						elementData.value[index].IEMRTempElementPrintData = dataitem.value;
  					} else {
  						//打印描述
  						elementData.value[index].IEMRTempElementPrintData = dataitem
  							.descripts;
  					}
  				}
  			} else {
  				var valuerange = dataitem.value.split('-');
  				if (data >= valuerange[0] && data <= valuerange[1]) {
  					elementData.value[index].IEMRTempElementData = dataitem.descripts;
  					if (elementData.value[index].printValue == 'Y') {
  						//打印值
  						elementData.value[index].IEMRTempElementPrintData = dataitem.value;
  					} else {
  						//打印描述
  						elementData.value[index].IEMRTempElementPrintData = dataitem
  							.descripts;
  					}
  				}
  			}
  		});
  	}
  });
  setGradeFun();
}

function setGradeFun() {
  let totleValue;
  elementData.value.forEach(item => {
    if (item.addItemFlag === 'Y') {
      totleValue = item.IEMRTempElementData;
    }
    
    if (item.IEMRTempElementDesc === '自理能力等级') {
      item.elmentData.forEach(child => {
        const myValue = child.value.split('-');
        
        if (!myValue[1]) {
          if (Number(totleValue) === Number(myValue[0])) {
            item.IEMRTempElementData = child.descripts;
            item.IEMRTempElementPrintData = child.descripts;
          }
        } else {
          if (Number(totleValue) >= Number(myValue[0]) && Number(totleValue) <= Number(myValue[1])) {
            item.IEMRTempElementData = child.descripts;
            item.IEMRTempElementPrintData = child.descripts;
          }
        }
      });
    }
  });
  
  elementData.value = [...elementData.value];
}

function handleDataset(e, _dataset) {
  // 处理数据集合并到事件对象中
  if (_dataset) {
    Object.keys(_dataset).forEach(key => {
      if (!e.currentTarget.dataset[key]) {
        e.currentTarget.dataset[key] = _dataset[key];
      }
    });
  }
}
</script>

<style>
/* 保持原有样式不变 */
.content {
  height: calc(100% - 200rpx);
  padding: 4rpx 15rpx;
  overflow: auto;
}

.elementitem {
  border-bottom: 1px solid #e5e5e4;
}

.item .title {
  height: 85rpx;
  line-height: 85rpx;
  background: #f6f6f6;
  font-size: 28rpx;
  font-weight: 400;
  color: #333333;
  display: flex;
  flex-direction: row;
}

.title .border {
  border: 8rpx solid #007aff;
  border-radius: 4rpx;
  height: 28rpx;
  margin: 20rpx 10rpx 20rpx 0;
}

.itemtitle {
  line-height: 85rpx;
}

.itemdata {
  line-height: 85rpx;
}

.itemdata .van-cell {
  padding: 0 !important;
}

.inputclass {
  border: 1px solid #999999;
  margin: 18rpx 8rpx;
  line-height: 80rpx;
}

.inputclass .van-cell {
  padding: 0 !important;
}

.scanCodeclass {
  line-height: 80rpx;
  display: flex;
  text-align: center;
}

.scanViewColor_green {
  color: #66bb6a;
}

.scanViewColor_red {
  color: #f44336;
}

.custom-icon {
  width: 30px;
  height: 30px;
  margin-top: 10rpx;
  margin-left: 15rpx;
}

.comboboxclass {
  margin: 8rpx;
}

.radioboxclass {
  margin: 8rpx;
  line-height: 80rpx;
}

.areaclass {
  min-height: 185rpx;
  border: 1px solid #999999;
  margin: 8rpx;
}

.selectclass {
  min-height: 60rpx;
  border: 1px solid #999999;
  margin: 8rpx;
  vertical-align: middle;
}

.delicon {
  margin-right: 10rpx;
}

.selectclass .van-cell__right-icon-wrap {
  /* padding-top: 14rpx;
  padding-right: 10rpx; */
}

.content .van-cell__value:empty {
  display: block !important;
}

.label {
  width: 100%;
  height: 78rpx;
  background: #f6f6f6;
  display: flex;
  flex-direction: row;
}


.radio-container {
  display: flex;
  flex-wrap: wrap;
}

.rowitem {
  line-height: 50rpx;
  width: 50%;
  box-sizing: border-box;
  padding: 5px;
}
.rowitem2 {
  line-height: 50rpx;
  width: 100%;
  box-sizing: border-box;
  padding: 5px;
}

.mutilCheck {
  height: 500rpx;
  width: calc(100% - 48rpx);
  padding: 24rpx;
}

.mutilCheck .checkData {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  overflow: auto;
  height: 434rpx;
  font-size: 30rpx;
  font-weight: bold;
  align-content: start;
}

.checkitem {
  width: calc(33% - 26rpx);
  word-break: break-all;
  margin: 10rpx 12rpx;
  min-height: 60rpx;
  border: 1px solid #e4e5e4;
  border-radius: 5px;
  background: #e4e5e4;
  height: 90rpx;
}

/* 外层容器：淡蓝色边框 + 圆角 */
.topinfo {
  /* 淡蓝色边框，宽度1px，线条类型为实线 */
  border: 1px solid #7cb5ff;
  /* 边角圆润，数值可根据需求调整 */
  border-radius: 8px;
  /* 内部留白，避免内容贴边 */
  padding: 10px;
  /* 可选：添加轻微阴影增强层次感 */
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  margin: 10px; /* 与其他元素保持间距，可按需删除 */
}

/* 内部内容容器：弹性布局确保排列整齐 */
.topinfo .item {
  /* 弹性布局，让子元素横向排列 */
  display: flex;
  /* 子元素垂直居中对齐 */
  align-items: center;
  /* 子元素之间均匀分布，两端无多余空间 */
  justify-content: flex-start;
  /* 自动换行，避免屏幕过小时内容溢出 */
  flex-wrap: wrap;
  /* 子元素之间的间距 */
  gap: 12px;
}

/* 基础文本样式：统一字体、放大字号、清晰显示 */
.topinfo .bedcode,
.topinfo .info {
  /* 统一字体，适配不同设备 */
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  /* 字体放大，比默认字号稍大 */
  font-size: 16px;
  /* 字体加粗，提升清晰度 */
  font-weight: 500;
  /* 字体颜色：深灰色，避免过黑刺眼 */
  color: #333333;
}

/* 分割线样式：灰色细线条，区分内容 */
.topinfo .border {
  /* 分割线为竖线 */
  width: 1px;
  height: 18px;
  /* 灰色线条，颜色比边框稍深 */
  background-color: #dcdcdc;
}

/* 护理级别样式：不同级别对应不同颜色，突出显示 */
.topinfo .nurse-lv-css-red {
  font-size: 16px;
  font-weight: 600;
  color: #ff4d4f; /* 特级 - 红色 */
  padding: 2px 8px;
  border-radius: 4px;
  background-color: #fff1f0; /* 浅色背景衬托 */
}

.topinfo .nurse-lv-css-pink {
  font-size: 16px;
  font-weight: 600;
  color: #ff7a75; /* 一级 - 粉色 */
  padding: 2px 8px;
  border-radius: 4px;
  background-color: #fff2f0;
}

.topinfo .nurse-lv-css-blue {
  font-size: 16px;
  font-weight: 600;
  color: #1890ff; /* 二级 - 蓝色 */
  padding: 2px 8px;
  border-radius: 4px;
  background-color: #e6f7ff;
}

.topinfo .nurse-lv-css-greenyellow {
  font-size: 16px;
  font-weight: 600;
  color: #52c41a; /* 三级 - 黄绿色 */
  padding: 2px 8px;
  border-radius: 4px;
  background-color: #f6ffed;
}

/* 内部容器布局 */
.scan-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 15px; /* 图片与文字间距 */
}

/* 图片样式 */
.scan-img {
  width: 100%; /* 图片宽度，可按需调整 */
  height: auto;
  opacity: 0.9;
}

/* 文字样式 - 高亮、放大、闪烁 */
.scan-text {
  font-size: 20px; /* 大字体 */
  font-weight: bold;
  color: #1890ff; /* 高亮蓝色 */
  text-align: center;
  padding: 10px 15px;
  border-radius: 6px;
  background-color: rgba(255, 255, 255, 0.8); /* 白色背景增强对比度 */
  /* 闪烁动画 */
  animation: blink 1.5s infinite;
}

/* 闪烁动画定义 */
@keyframes blink {
  0%, 100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.6;
    transform: scale(1.05); /* 轻微放大增强闪烁感 */
  }
}
</style>