<template>
	<view class="patientInformationInfo" v-if="Object.keys(infoDATA).length > 0">
		<view class="page_head">
			<view class="title">{{ infoDATA.patientName }}<text class="patientCode">{{ infoDATA.residenceNo }}</text></view>
			<view class="info">
				<view :class="(infoDATA.careLevel || infoDATA.illnessState)?'bedNum':'waitingCount'">
					<text class="info_h">床号</text>
					<text class="info_b">{{ infoDATA.bedNum }}</text>
				</view>
				<!-- <view :class="(infoDATA.careLevel || infoDATA.illnessState)?'bedNum':'waitingCount'">
					<text class="info_h">待检查数</text>
					<text class="info_b">{{ infoDATA.watingCount }}</text>
				</view> -->
				<view class="bedNum" v-if="infoDATA.careLevel">
					<text class="info_h">护理等级</text>
					<text class="info_b">{{ infoDATA.careLevel.name }}</text>
				</view>
				<view class="waitingCount" v-if="infoDATA.illnessState">
					<text class="info_h">病情级别</text>
					<text class="info_b">{{ infoDATA.illnessState.name }}</text>
				</view>
			</view>
		</view>
    <scroll-view v-if="!isEmpty" scroll-y class="scrollContent">
      <view class="page_item" v-for="(inspects, key, index) in infoDATA.inspectListMap" :key="key">
        <view class="splitLine">
          <view class="splitLineFlex">
            <view class="splitLineBox"></view>
          </view>
          <view>{{key}}</view>
          <view class="splitLineFlex">
            <view class="splitLineBox"></view>
          </view>
        </view>
        <view class="page_item_info" :class="{mt0: i === 0}" v-for="(item, i) in inspects" :key="item.id">
          <view class="page_item_info_title">检查项目：<text class="bold">{{ item.inspectName }}</text></view>
          <view class="page_item_info_title">检查科室：<text>{{ item.execDept ? item.execDept.dept : "" }}</text></view>
          <view class="page_item_info_title btn row">
            <view>预约时间：<text>{{ item.yyTime | filterSecondDate }}</text><text v-if="item.reservationNumber">（{{ item.reservationNumber }}）</text></view>
            <button @click="changeYyTime(item)" v-if="item.inspectState.value == 1 || item.inspectState.value == 30">修改</button>
          </view>
          <view class="page_item_info_title row">
            <view>是否紧急：<text :class="{red:item.priority===1||item.priority==='1'}">{{ (item.priority===1||item.priority==='1')?'是':'否' }}</text></view>
            <view>状态：<text v-if="item.inspectState">{{ item.inspectState.name }}</text></view>
          </view>
          <view class="buildOrder" @click="build(item)" v-if="!item.gdId&&(item.inspectState.value==1||item.inspectState.value==30)">建单</view>
        </view>
      </view>
    </scroll-view>
		<view v-if="isEmpty" class="zwsj">
			<image class="zwsj-img" mode="widthFix" src="../../static/img/zanwushuju.png"></image>
			<view class="zwsj-txt">暂无检查信息</view>
		</view>
    <view class="toolbar" @click="Scanning()">
      <text class="toolbar-icon newicon newicon-saoma"></text>
      <text class="toolbar-sao">扫一扫</text>
    </view>
		<!-- 弹窗 -->
		<inspectRemoveModel :title="models1.title" :icon="models1.icon" :disjunctor="models1.disjunctor" :content="models1.content" @know="know1" :operate="models1.operate" @ok="ok1" @cancel="cancel1"></inspectRemoveModel>
    <!-- 弹窗 -->
    <showModel :title="models.title" :icon="models.icon" :disjunctor="models.disjunctor" :content="models.content"
      @ok="ok" @cancel="cancel" @know="know" :operate="models.operate"></showModel>
	</view>
</template>
<script>
	import {
		get,
		post,
		SM,
		webHandle
	} from "../../http/http.js";
	export default {
		data() {
			return {
        SMFlag: true,
        isEmpty: true,
				options: {},
				infoDATA: {},
				// 弹窗model1
				models1: {
				  disjunctor: false,
				},
        // 建单的对象
        jdItem: {},
        // 弹窗model
        models: {
          disjunctor: false,
        },
			};
		},
		methods: {
      // 建单
      build(data) {
        console.log(data);
        this.jdItem = data;
        this.models = {
          disjunctor: true,
          title: "提示",
          content: `您即将创建【${data.patientName || ""}】患者前往【${
          data.execDept ? data.execDept.dept : ""
        }】进行【${data.inspectName || ""}】检查，您确认吗？`,
          icon: "warn",
          operate: {
            ok: "确定",
            cancel: "取消",
          },
        };
      },
      //建单后知道了
      know() {
        this.models.disjunctor = false;
        this.getInfo(this.jdItem.patientCode);
      },
      //建单确定
      ok() {
        this.models.disjunctor = false;
        let postData = {
          deptId: this.infoDATA.department.id
        };
        let taskId;
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/nurse/getDeptTaskType", postData).then((result) => {
          if (result.status == 200) {
            let obj = result.data.allTaskTypes.filter(
              (item) => item.associationType.value == "inspect"
            );
            if (obj.length) {
              taskId = obj[0].id;
              let postData = {
                workOrder: {
                  sourceId: 4,
                  taskType: {
                    id: taskId,
                    isHalfInspect: obj[0].isHalfInspect === 1 ? 1 : 0
                  },
                  createDept: this.infoDATA.department.id,
                  startDept: {
                    id: this.infoDATA.department.id
                  },
                  patient: {
                    patientCode: this.jdItem.patientCode
                  },
                  checkList: [{
                    id: this.jdItem.id
                  }],
                  goods: "",
                  worker: {
                    id: uni.getStorageSync("userData").user.id
                  },
                  workerStartOrderFlag: 1,
                },
              };
              //根据检查的紧急度加急
              if (this.jdItem.priority === 1 || this.jdItem.priority === '1') {
                postData.workOrder["urgentDetails"] = {
                  checkStatus: {
                    id: 329
                  },
                  urgentReason: '系统根据检查信息，自动进行加急',
                };
              }
              post("/api/startOrder", postData).then((res) => {
                uni.hideLoading();
                let msg = res.msg;
                let icon = "error";
                if (res.status == 200) {
                  msg = "建单成功";
                  icon = "success";
                }
                this.models = {
                  disjunctor: true,
                  content: msg,
                  icon: icon,
                  operate: {
                    know: "知道了",
                  },
                };
              });
            } else {
              uni.hideLoading();
              this.models = {
                disjunctor: true,
                content: "建单失败,科室未开通！",
                icon: "error",
                operate: {
                  know: "知道了",
                },
              };
            }
          } else {
            uni.hideLoading();
            uni.showToast({
              icon: "none",
              title: result.msg || "接口获取数据失败！",
            });
          }
        });
      },
      //建单取消
      cancel() {
        this.models.disjunctor = false;
      },
      // 扫一扫
      Scanning(qrcodePrefix = '') {
        if (!this.SMFlag) {
          return;
        }
        this.SMFlag = false;
        SM().then((content) => {
          this.SMFlag = true;
          this.getInfo(content);
        }).catch(err => {
          this.SMFlag = true;
        });
      },
			// 修改预约时间->知道了
			know1() {
			  this.models1.disjunctor = false;
			},
			// 修改预约时间->确定
			ok1(data) {
				console.log(data);
				const { yyTime} = data;//暂时这样
				if (!yyTime) {
				  //没有填写预约时间
				  uni.showModal({
				    title: "提示",
				    content: "请填写预约时间！",
				    showCancel: false,
				    success: function(res) {
				      if (res.confirm) {
				        console.log("用户点击确定");
				      } else if (res.cancel) {
				        console.log("用户点击取消");
				      }
				    },
				  });
				  return;
				}
			  this.models1.disjunctor = false;
			  let postData = {
			    inspectCode: this.currentInspect.inspectCode,
			  	yyTime
			  };
			  uni.showLoading({
			    title: '加载中',
			    mask: true,
			  })
			  post('/workerOrder/updateInspectYytime', postData).then(res => {
			    uni.hideLoading();
			    if (res.status == 200) {
						this.getInfo(this.options.patientCode);
			    } else {
			      uni.showToast({
			        icon: "none",
			        title: "修改失败",
			      });
			    }
			  })
			},
			// 修改预约时间->取消
			cancel1() {
			  this.models1.disjunctor = false;
			},
			// 修改预约时间
			changeYyTime(item) {
			  this.currentInspect = item;
			  this.models1 = {
			    disjunctor: true,
			    content: `您要修改[${item.inspectName||''}]的预约时间`,
			    icon: "warn",
			    operate: {
			      ok: "确定",
			      cancel: "取消",
			    },
			  };
			},
			// 请求详细页面的数据
			getInfo(patientCode) {
				uni.showLoading({
					title: "加载中",
					mask: true,
				});
				post("/nurse/workOrder/getPatientInspectInfo", {
					patientCode,
				}).then((res) => {
					uni.hideLoading();
					if (res.status == 200) {
            res.data.inspectListMap = res.data.inspectListMap || {};
            this.isEmpty = Object.keys(res.data.inspectListMap).length === 0;
						this.infoDATA = res.data;
					} else {
						uni.showToast({
							icon: "none",
							title: res.msg || "接口获取数据失败！",
						});
					}
				});
			},
		},
		onLoad(options) {
			this.options = options;
			this.getInfo(this.options.patientCode);
			// #ifdef APP-PLUS
			webHandle("no", "app");
			// #endif
			// #ifdef H5
			webHandle("no", "wx");
			// #endif
		},
    onShow() {
      this.SMFlag = true;
    }
	};
</script>
<style lang="less" scoped>
  .toolbar {
    margin-bottom: 16rpx;
    z-index: 999;
    height: 88rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    box-sizing: border-box;
    border-radius: 4rpx;
    background-color: #E5E8ED;

    .toolbar-icon {
      font-size: 52rpx;
      margin-right: 16rpx;
      color: #07863c;
    }

    .toolbar-sao {
      font-size: 36rpx;
      color: #333;
    }
  }
	.patientInformationInfo {
    background-color: #F7F7F7;
    display: flex;
    flex-direction: column;
    height: 100vh;
		.zwsj {
      flex: 1;
      min-height: 0;
			text-align: center;

			.zwsj-img {
				width: 560rpx;
			}

			.zwsj-txt {
				font-size: 36rpx;
				font-weight: 700;
				margin-top: 20rpx;
				text-align: center;
			}
		}

		.page_head {
			background-color: #49b856;
			color: #fff;

			.title {
				font-size: 48rpx;
				padding-top: 24rpx;
				text-align: center;
			}

			.patientCode {
				margin-left: 12rpx;
				font-size: 28rpx;
			}

			.info {
				padding-bottom: 24rpx;
				margin: 8rpx 0;
				height: 80rpx;
				display: flex;
				justify-content: center;
				align-items: center;

				.bedNum,
				.waitingCount {
					width: 300rpx;
					height: 100%;
					display: flex;
					flex-direction: column;
					justify-content: center;
					align-items: center;

					.info_h {
						font-size: 30rpx;
					}

					.info_b {
						font-size: 26rpx;
					}
				}

				.bedNum {
					position: relative;

					&:after {
						content: "";
						position: absolute;
						width: 4rpx;
						height: 50rpx;
						right: -2rpx;
						top: 15rpx;
						background-color: #fff;
					}
				}
			}
		}

    .scrollContent{
      flex: 1;
      min-height: 0;
    }
		.page_item {
			// margin-bottom: 10rpx;
			border-radius: 8rpx;
			overflow: hidden;

      .splitLine{
        height: 50rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #8F8F8F;
        .splitLineFlex{
          flex: 1;
          display: flex;
          align-items: center;
          justify-content: center;
          .splitLineBox{
            width: calc(100% - 48rpx);
            height: 1rpx;
            background-color: #8F8F8F;
          }
        }
      }

			.page_item_info {
        margin-top: 10rpx;
				padding: 20rpx 16rpx;
				text-align: left;
				line-height: 60rpx;
				font-size: 30rpx;
        background: #fff;

        &.mt0{
          margin-top: 0;
        }

        .buildOrder{
          width: 258rpx;
          height: 72rpx;
          background: #49B856;
          border-radius: 6rpx;
          margin: 0 auto;
          color: #FFFFFF;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 30rpx;
        }

				.page_item_info_title {
					font-weight: 700;

					&.btn {
						display: flex;
						button {
							font-size: 28rpx;
							color: #49b856;
							height: 50rpx;
							line-height: 50rpx;
							margin: 0;
						}
					}
          &.row{
            display: flex;
            align-items: center;
            justify-content: space-between;
          }

					text {
						font-weight: normal;

						&.red {
							color: red;
						}
            
            &.bold{
              font-weight: 700;
            }
					}
				}
			}
		}
	}
</style>
