<template>
  <view class="Scanning_Result">
    <view class="Scanning_top">
      患者总数量：<text class="green">{{patientList.length}}</text>
    </view>

    <view class="Scanning_cont">
      <scroll-view scroll-y class="scrollContent">
        <view class="column" v-for="(item, index) in patientList" :key="item.id">
						<view class="top">
							<text class="orders green">{{index + 1}}</text>
						  <view class="name">姓名：</view>
						  <view class="value">{{item.patientName}}</view>
						</view>
						<view class="bottom">
						  <view class="name">住院号：</view>
						  <view class="value">{{item.residenceNo}}</view>
						</view>
        </view>
      </scroll-view>
    </view>

    <view class="foot_btn_spe">
      <view class="column">
        <view class="btn" @click="scan()">扫一扫</view>
        <view class="btn" @click="hand_again()">手动录入</view>
      </view>
      <view class="column" v-if="patientList.length">
        <view class="btn" @click="scanDept()">建单</view>
      </view>
    </view>
    <!-- 手动查询患者弹窗 -->
    <handViewPatient v-if="patientModels.disjunctor" :title="patientModels.title"
      :disjunctor="patientModels.disjunctor" @ok="patientOk" @cancel="patientCancel">
    </handViewPatient>
  </view>
</template>
<script>
  import {
    get,
    post,
    SM,
    webHandle
  } from "@/http/http.js";
  export default {
    data() {
      return {
        hosId: uni.getStorageSync("userData").user.currentHospital.id,
        patientList: [],//患者列表
        queryObj: {}, //路由传递过来的数据
        scanCount: '', //已扫描数量
        SMFlag: true,
        // 手动查询弹窗model
        patientModels: {
          disjunctor: false,
        },
      };
    },
    onShow() {
      this.SMFlag = true;
    },
    methods: {
			// 送回病房-扫描科室
			scanDept() {
			  if (!this.SMFlag) {
			    return;
			  }
			  this.SMFlag = false;
			  let code = "";
			  SM().then((ress1) => {
			    uni.showLoading({
			      title: "加载中",
			      mask: true,
			    });
			    //检验二维码的有效性
			    post("/dept/scanning", {
			      content: ress1,
			    }).then((result) => {
			      this.SMFlag = true;
			      if (result.state == 200 || result.state == 201) {
			        let ress = result.code;
			        if (ress) {
			          code = ress;
			          // 科室扫描
			          let postData = {
			            "type": "query",
			            "qrCode": code
			          };
			          post("/dept/scanChangeDept", postData).then((res) => {
			            uni.hideLoading();
			            if (res.status == 200) {
			              if (res.data) {
			                if(res.data.deptDTO){
			                  this.currentStartDept = res.data.deptDTO;
			                }else{
			                  this.currentStartDept = res.data;
			                }
			                this.buildOrder();
			              } else {
			                uni.hideLoading();
			                uni.showToast({
			                  icon: "none",
			                  title: "请扫描正确的科室码！",
			                });
			              }
			            } else {
			              uni.hideLoading();
			              uni.showToast({
			                icon: "none",
			                title: res.msg || "接口获取数据失败！",
			              });
			            }
			          });
			        }
			      } else {
			        uni.hideLoading();
			        uni.showToast({
			          icon: "none",
			          title: result.info || "接口获取数据失败！",
			        });
			      }
			    });
			  }).catch(err => {
			    this.SMFlag = true;
			  });
			},
      // 手动查询-确认
      patientOk(data) {
        console.log(data);
        if (!data.id) {
          //没有查询到患者
          uni.showModal({
            title: '提示',
            content: "没有查询到患者！",
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定');
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          });
          return;
        }
        this.patientModels.disjunctor = false;
        this.hand_scanning_common(data.residenceNo, 'hand');
      },
      // 手动查询-取消
      patientCancel() {
        this.patientModels.disjunctor = false;
      },
      // 手动查询弹窗
      showHandViewDrugsbag() {
        this.patientModels = {
          title: '填写患者住院号',
          disjunctor: true,
        }
      },
      // 手动录入
      hand_again() {
        this.showHandViewDrugsbag();
      },
      // 核对交接
      buildOrder(){
        uni.showModal({
          title: "提示",
          content: "您确定建单吗？",
          success: (res) => {
            if (res.confirm) {
              console.log("用户点击确定");
              uni.showLoading({
                mask: true,
                title: '加载中'
              })
              let userId = uni.getStorageSync("userData").user.id;
              let postData = {
                "workOrder": [],
              };
              this.patientList.forEach(v => {
								postData.workOrder.push({
									sourceId: 4,
									"hosId": this.hosId,
									"startDept": {
										"id": this.currentStartDept.id
									},
									"createDept": this.currentStartDept.id,
									"patient": {
										"patientCode": v.patientCode
									},
									"worker": {
										"id": userId
									},
								})
              })
              post("/workerOrder/returnSickRoom", postData).then((res) => {
                console.log(res)
                uni.hideLoading();
                if (res.status == 200) {
                  uni.showToast({
                    icon: 'success',
                    title: '建单成功',
                    duration: 2000,
                    mask: true,
                  });
                  setTimeout(() => {
                    uni.navigateTo({
                      url: `/pages/receiptpage/receiptpage`,
                    });
                  }, 2000)
                } else if(res.status == 400){
                    // 校验患者是否没有住院科室
                    uni.showToast({
                      icon: "none",
                      title: res.msg,
                      duration: 2000,
                      mask: true,
                    });
                    setTimeout(() => {
                      uni.navigateTo({
                        url: `/pages/receiptpage/receiptpage`,
                      });
                    }, 2000)
                } else {
                  uni.showToast({
                    icon: "none",
                    title: res.msg || "接口获取数据失败！",
                  });
                }
              })
            } else if (res.cancel) {
              console.log("用户点击取消");
            }
          },
        });
      },
      // 扫一扫
      scan(isFlag = false) {
        if (!this.SMFlag) {
          return;
        }
        this.SMFlag = false;
        SM().then((ress1) => {
          this.hand_scanning_common(ress1, 'scan', isFlag);
        }).catch(err => {
          this.SMFlag = true;
        });
      },
      // 手动输入和扫码公共方法
      hand_scanning_common(ress1, type, isFlag = false) {
        // ----------------
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        //检验二维码的有效性
        post("/dept/scanning", {
          content: ress1,
        }).then((result) => {
          this.SMFlag = true;
          if (result.state == 200 || result.state == 201) {
            let codes = result.code;
            if (codes) {
              this.input_common(ress1, type, isFlag);
            } else {
              uni.hideLoading();
            }
          } else {
            uni.hideLoading();
            uni.showToast({
              icon: "none",
              title: result.info || "接口获取数据失败！",
            });
          }
        });
        // ------------------------------
      },
      // 将患者添加到患者列表
      input_common(ress1, type, isFlag = false){
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/patient/patientByCode ", { code: ress1 }).then((ress) => {
          uni.hideLoading();
          if (ress.status == 200) {
            let flag = this.patientList.find(v => v.id == ress.data.id);
            if(flag){
              uni.showToast({
                icon: "none",
                title: "患者已存在！",
              });
            }else{
              this.patientList.unshift(ress.data);
              uni.showToast({
                icon: "none",
                title: "添加患者成功！",
              });
            }
          } else {
            uni.showToast({
              icon: "none",
              title: ress.msg || "接口获取数据失败！",
            });
          }
        });
      },
    },
    onLoad(options) {
      console.log(options, "options");
      this.queryObj = options;
      this.patientList = [{id: options.patientId, patientName: options.patientName, residenceNo: options.patientResidenceNo, patientCode: options.patientCode}]
      // #ifdef APP-PLUS
      webHandle("no", "app");
      // #endif
      // #ifdef H5
      webHandle("no", "wx");
      // #endif
    },
  };
</script>
<style lang="less" scoped>
  .green{
    color:#49b856;
  }
  .Scanning_Result {
    background: #EBEBEB;
    display: flex;
    flex-direction: column;
    height: 100vh;

    .Scanning_top {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 63rpx;
      font-size: 28rpx;
      font-weight: bold;
      background-color: #fff;
      margin-top: 16rpx;
    }

    .Scanning_cont {
      flex: 1;
      min-height: 0;
      display: flex;
      flex-direction: column;
      .scrollContent{
        flex: 1;
        min-height: 0;
      }
      .column{
        padding: 24rpx 44rpx;
        font-size: 26rpx;
        margin-top: 16rpx;
        background-color: #fff;
        .top,.bottom{
          height: 50%;
          display: flex;
          align-items: center;
          .name{
            
          }
          .value{
            flex: 1;
          }
        }
        .top{
          position: relative;
					 /deep/ .uni-checkbox-input.uni-checkbox-input-checked{
						 color: #fff !important;
						 background: #49b856 !important;
						 border-color: #49b856 !important;
					 }

          .orders{
            width: 54rpx;
            text-align: center;
            // position: absolute;
            // left: -64rpx;
            // top: 50%;
            // transform: translateY(-50%);
            font-size: 28rpx;
            font-weight: bold;
          }
        }
        .bottom{
          margin-top: 16rpx;
        }
      }
    }

    .foot_btn_spe {
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 24rpx;
      font-weight: bold;
      padding: 24rpx;

      .column{
        width: 100%;
        height: 78rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 24rpx;

        .btn {
          height: 100%;
          flex: 1;
          background: linear-gradient( 90deg, #6FC073 0%, #3DB197 100%);
          color: #fff;
          border-radius: 4rpx;
          font-size: 30rpx;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }
  }
</style>
