<template>
    <view>
		<nav-bar :navbarData="navbarData"></nav-bar>
        <view :style="'margin-top: ' + (height * 2 - 20) + 'rpx; height: calc(100vh - ' + (height * 2 + 100) + 'rpx)'">
            <view class="content">
                <view class="item" :data-item="item" v-for="(item, index) in recordData" :key="index">
                    <view class="data">
                        <view class="itemtitle">
                            <view class="title">更新日期：</view>
                            <view class="date">{{ item.updateDate }} {{ item.updateTime }}/{{ item.updateUserName }}</view>
                        </view>
                        <view :span="detail.width" class="detail" v-for="(detail, dindex) in item.detail" :key="dindex">
                            <view class="title">{{ detail.descripts }}：</view>
                            <view class="value" :style="'color:' + (detail.countItem == 'Y' ? '#007AFF' : '#333333')">{{ detail.data }}</view>
                        </view>
                    </view>

                   <view class="btn">
                        <button
                            plain
                            type="primary"
                            size="normal"
                            class="delete"
                            custom-class="deletecustom"
                            @click="deleteRecord($event, { item, flag: 'D' })"
                            :data-item="item"
                            data-flag="D"
                        >
                            删除
                        </button>
                        <button
                            plain
                            type="primary"
                            size="normal"
                            class="edit"
                            custom-class="editcustom"
                            @click="editRecord($event, { item, flag: 'E' })"
                            :data-item="item"
                            data-flag="E"
                        >
                            编辑
                        </button>
						<button
							plain
							type="primary"
							size="normal"
							class="find"
							custom-class="findcustom"
							@click="editRecord($event, { item, flag: 'F' })"
							:data-item="item"
							data-flag="F"
						> 查看
						</button>
                    </view>
                </view>
               <view v-if="recordData.length == 0" class="empty" :style="'background-image:url(' + imgDomain + '/images/icon/pic_emptypage.png)'"></view>
            </view>

            <view class="multiBtn-content" v-if="!(recordData.length > 0 && single === 'Y')">
                <van-button type="primary" color="#007AFF" @click="addRecord" block style="width: 100%; height: 100%" custom-class="addbtn">
                    {{ recordData.length == 0 ? '添加' : '继续添加' }}{{ addtitle }}
                </van-button>
            </view>
        </view>
    </view>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { onLoad ,onShow } from '@dcloudio/uni-app';
import { httpconfig } from '../../config/httpconfig';
import Util from '../../utils/util.js';
import Common from '../../utils/common.js';
// import drawQrcode from '../../utils/weapp-qrcode.js';
import { $http } from '../../config/https';
import navBar from '@/pages/components/navbar/index';

// 响应式数据
const height = ref('');
const templateID = ref('');
const recordData = ref([]);
const addtitle = ref('');
const single = ref('N');
const upImgShow = ref(false);
const imgDomain = ref(httpconfig.imgDomain);
const navbarData = ref({
  background: '',
  title: '列表',
  height: '100',
  bottom: true,
  isGoBack: true,
  isGoHome: true,
  cleanPat: 'Y'
});
const detail = reactive({
    width: '',
    descripts: '',
    countItem: '',
    data: ''
});

const dindex = ref(0);

// 页面加载时执行
onLoad((options) => {
    single.value = options.single;
    addtitle.value = options.title ? options.title.replace('列表', '记录') : '';
	navbarData.value.title = options.title ? options.title.replace('列表', '记录') : '';
    templateID.value = options.templateID;
});

onShow(() => {
	getPatEMRRecord();
	
	  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(() => {
	getPatEMRRecord();
});

// 方法定义
const addRecord = () => {
	// 编辑标识 'E';
	// 跳转页面
	Common.getToRecordPage(templateID.value, 0, addtitle.value, 'E');
};

const getPatEMRRecord = () => {
    const patInfo = uni.getStorageSync('patInfo');
    $http.post('urlDeault', this,{
        code: '10020017',
        data: {
            params: [
                {
                    templateID: templateID.value,
                    admID: patInfo.admInfo.admID
                }
            ]
        },
        success: (res) => {
            if (+res.errorCode === 0) {
				let resultAry = res.result;
				recordData.value = res.result;
            }
        }
    });
};

const handleDataset = (e, _dataset) => {
    Object.assign(e.currentTarget.dataset, _dataset);
};

const editRecord = (e, _dataset) => {
    handleDataset(e, _dataset);
    const { item, flag } = e.currentTarget.dataset;
    Common.getToRecordPage(templateID.value, item.recordID, addtitle.value, flag);
};

const deleteRecord = (e, _dataset) => {
    handleDataset(e, _dataset);
    const { item } = e.currentTarget.dataset;
    
    uni.showModal({
        title: '温馨提醒',
        content: '是否确认删除该记录',
        confirmText: '确认',
        cancelText: '取消',
        confirmColor: '#1890FF',
        success: (res) => {
            if (res.confirm) {
                $http.post('urlDeault',this, {
                    code: '04220016',
                    data: {
                        params: [
                            {
                                ID: item.recordID
                            }
                        ]
                    },
                    success: (res) => {
                        if (+res.errorCode === 0) {
                            getPatEMRRecord();
                        }
                    }
                });
            }
        }
    });
};

const onClose = () => {
    upImgShow.value = false;
};
</script>

<style>
/* 样式部分保持不变 */
.content {
    height: calc(100% - 100rpx);
    overflow: auto;
    width: 100%;
}

.item {
    border-bottom: 10px solid #e5e4e5;
    line-height: 60rpx;
    font-size: 12px;
}

.item .data {
    width: calc(100% - 68rpx);
    display: flex;
    flex-direction: column;
    margin: 0 34rpx;
    line-height: 56rpx;
    border-bottom: 1px solid #e5e5e5;
}

.itemtitle {
    border-bottom: 1px solid #e5e4e5;
    height: 100rpx;
    line-height: 100rpx;
    border-bottom: 2rpx solid #e5e5e5;
    display: flex;
    flex-direction: row;
}

.itemtitle .title {
    font-size: 34rpx;
    font-weight: 400;
    color: #333333;
    width: 200rpx;
}

.itemtitle .date {
    width: calc(100% - 200rpx);
    font-size: 28rpx;
    font-weight: 400;
    text-align: right;
    color: #999999;
}

.data .detail {
    display: flex;
    flex-direction: row;
    font-size: 30rpx;
    font-weight: 400;
}

.detail .title {
    width: 260rpx;
    color: #666666;
    text-align: right;
}

.detail .value {
    width: calc(100% - 260rpx);
    color: #333333;
}

.item .btn {
    display: flex;
    flex-direction: row-reverse;
    height: 62rpx;
    padding: 34rpx 0;
}

.btn .find {
    width: 190rpx;
    margin: 0 6rpx !important;
    height: 100% !important;
	line-height: 100%;
	font-size: 30rpx;
	padding-top: 14rpx;
}

.findcustom {
    width: 100% !important;
    border: 1px solid #007aff !important;
    border-radius: 8rpx !important;
    height: 100% !important;
    color: #007aff !important;
}

.btn .edit {
    width: 155rpx;
    margin: 0 6rpx !important;
    height: 100% !important;
	line-height: 100%;
	font-size: 30rpx;
	padding-top: 14rpx;
}

.editcustom {
    width: 100% !important;
    border: 1px solid #999999 !important;
    border-radius: 8rpx !important;
    height: 100% !important;
    color: #333333 !important;
}

.btn .delete {
    width: 155rpx;
    margin: 0 6rpx !important;
    height: 100% !important;
	border: 1px solid red !important;
    color: red !important;
	line-height: 100%;
	font-size: 30rpx;
	padding-top: 14rpx;
}

.deletecustom {
    width: 100% !important;
    border: 1px solid red !important;
    border-radius: 8rpx !important;
    height: 100% !important;
    color: red !important;
}

.empty {
    width: 100%;
    height: 50%;
    margin-top: 40%;
    background-size: 100% auto;
    background-repeat: no-repeat;
}

.multiBtn-content {
    display: flex;
    flex-direction: row;
    width: 100%;
}

.addbtn {
    height: 100% !important;
    width: 100% !important;
}

.addbtn .van-button__text,
.addbtn .van-button__icon {
    font-size: 34rpx;
    font-weight: 400;
}

</style>