<template>
  <view class="patientBuildConfirm">
    <view class="qco_msg" v-html="patientMsg"></view>
    <view class="orderDetail_info">
      <scroll-view scroll-y class="orderDetail_infoItem">
        <view class="orderDetail_infoItem_header">
          <view class="orderDetail_infoItem_header_title">
            <view class="icon"></view>
            <view class="taskNameAndWorkerName">
              <text class="taskName">服务</text>
            </view>
          </view>
          <text class="orderDetail_infoItem_header_more">{{patientTaskType.taskName||'暂无'}}</text>
        </view>
        <view class="orderDetail_infoItem_item">
          <view class="orderDetail_infoItem_item_content">
            <text class="orderDetail_infoItem_item_name">起点科室</text>
            <text
              class="orderDetail_infoItem_item_value">{{deptDisplay == 2?patientBuildData.dept.startDept.deptalias:patientBuildData.dept.startDept.dept}}</text>
          </view>
          <view class="orderDetail_infoItem_item_content"
            v-if="patientTaskType.associationType.value == ASSOCIATION_TYPES['患者陪检业务']">
            <text class="orderDetail_infoItem_item_name">中间科室</text>
            <text class="orderDetail_infoItem_item_value">{{checkDeptsName}}</text>
          </view>
          <view class="orderDetail_infoItem_item_content">
            <text class="orderDetail_infoItem_item_name">目标科室</text>
            <text
              class="orderDetail_infoItem_item_value">{{deptDisplay == 2?patientBuildData.dept.endDept.deptalias:patientBuildData.dept.endDept.dept}}</text>
          </view>
          <view class="orderDetail_infoItem_item_content">
            <text class="orderDetail_infoItem_item_name">携带设备</text>
            <text class="orderDetail_infoItem_item_value">{{goodsName||'暂无'}}</text>
          </view>
          <view class="orderDetail_infoItem_item_content">
            <text class="orderDetail_infoItem_item_name">预约时间</text>
            <text class="orderDetail_infoItem_item_value">{{patientBuildData.yyTime||'暂无'}}</text>
          </view>
          <view class="orderDetail_infoItem_item_content">
            <text class="orderDetail_infoItem_item_name">是否紧急</text>
            <text class="orderDetail_infoItem_item_value">{{patientBuildData.urgent.isUrgent?'是':'否'}}</text>
          </view>
        </view>
      </scroll-view>
    </view>
    <!-- 底部 -->
    <seiminFooterBtn :btns="btns"></seiminFooterBtn>
    <seiminModel ref="seiminModel"></seiminModel>
  </view>
</template>

<script>
  import {
    mapState,
  } from "vuex";
  import {
    ASSOCIATION_TYPES
  } from "../../utils/enum.association_types.js";
  import {
    SOURCEID
  } from "../../utils/enum.sourceid.js";
  import {
    uniqBy
  } from 'lodash'
  import {
    reqBuild
  } from '../../request/api.js';
  export default {
    data() {
      return {
        reFresh: '',
        ASSOCIATION_TYPES,
        SOURCEID,
        // 中间科室名称
        checkDeptsName: '',
        // 设备
        goodsName: '',
        //患者建单信息展示
        patientMsg: '请您确认一下建单信息！',
        // 传递过来的参数
        queryParams: {},
        //底部按钮
        btns: [{
          name: "取消",
          type: "default",
          click: () => {
            uni.navigateBack();
          },
        }, {
          name: "确认建单",
          type: "primary",
          click: () => {
            this.buildOrder();
          },
        }, ],
      };
    },
    computed: {
      ...mapState('login', [
        'loginInfo',
      ]),
      ...mapState('other', [
        'deptDisplay',
        'patientBuildTrip',
        'patientTaskType',
        'selectedPatient',
        'patientBuildData',
      ]),
    },
    methods: {
      // 重复策略
      showReaptModal(msg, postData) {
        this.$refs.seiminModel.show({
          icon: "warn",
          content: msg,
          btns: [{
            name: "否",
            type: "primary",
            click: () => {
              this.$refs.seiminModel.close();
            },
          }, {
            name: "是",
            type: "default",
            click: () => {
              this.$refs.seiminModel.close();
              postData.tipsCreateOder = 1;
              uni.showLoading({
                mask: true,
                title: '加载中'
              })
              reqBuild(this.patientBuildData.isYY ? "appointmentOrder" : "startOrder", postData)
                .then((data) => {
                  uni.hideLoading();
                  if (data.status == 200) {
                    this.$refs.seiminModel.show({
                      skin: "toast",
                      icon: "success",
                      content: "您申请的工单已建单成功，工作人员会尽快处理！",
                      btns:[{
                        click:()=>{
                          uni.navigateTo({
                            url: '/pages/patientList/patientList'
                          })
                        }
                      }]
                    });
                  } else {
                    this.$refs.seiminModel.show({
                      skin: "toast",
                      icon: "error",
                      content: data.msg || "创建失败",
                      btns:[{
                        click:()=>{
                          uni.navigateTo({
                            url: '/pages/patientList/patientList'
                          })
                        }
                      }]
                    });
                  }
                });
            },
          }, ],
        })
      },
      // 是否需要护士医生陪同模态框
      accompany(postData, yuyue, type) {
        this.$refs.seiminModel.show({
          icon: "warn",
          content: "您选择的患者是危重或特级护理或一级护理患者，请问是否需要医护陪同检查？",
          btns: [{
            name: "否",
            type: "primary",
            click: () => {
              this.$refs.seiminModel.close();
              if (type == "patient") {
                //患者列表直接建单
                this.buildCommon(postData, 0, 'accompany3');
              } else if (type == "patient-yy") {
                //患者列表预约建单
                this.buildCommon(postData, 0, 'accompany4');
              }
            },
          }, {
            name: "是",
            type: "default",
            click: () => {
              this.$refs.seiminModel.close();
              if (type == "patient") {
                //患者列表直接建单
                this.buildCommon(postData, 0, 'accompany1');
              } else if (type == "patient-yy") {
                //患者列表预约建单
                this.buildCommon(postData, 0, 'accompany2');
              }
            },
          }, {
            name: "取消",
            flex: 1,
            type: "default",
            click: () => {
              this.$refs.seiminModel.close();
            },
          }, ]
        });
      },
      // 建单公共方法
      buildCommon(postData, type1, type2) {
        if (type1 === null) {
          if (!this.patientBuildData.isYY) {
            postData.workOrder.platform = 2;
          }
          //是否需要医护陪同检查
          if (this.selectedPatient.careLevel && this.patientTaskType.isAccompany == 1) {
            //特级护理或一级护理
            if (
              this.selectedPatient.careLevel.value === "0" ||
              this.selectedPatient.careLevel.value === "1"
            ) {
              this.accompany(postData, this.patientBuildData.isYY, type2);
              return;
            }
          }
          if (
            this.selectedPatient.illnessState &&
            this.patientTaskType.isAccompany == 1
          ) {
            //病危或病重
            if (
              this.selectedPatient.illnessState.value === "2" ||
              this.selectedPatient.illnessState.value === "3"
            ) {
              this.accompany(postData, this.patientBuildData.isYY, type2);
              return;
            }

          }
          postData.workOrder.isAccompany = 0; //是否需要医护陪同检查
        } else if (typeof type1 === 'number') {
          postData.workOrder.isAccompany = type1; //是否需要医护陪同检查
        }
        uni.showLoading({
          mask: true,
          title: '加载中'
        })
        reqBuild(this.patientBuildData.isYY ? "appointmentOrder" : "startOrder", postData)
          .then((data) => {
            uni.hideLoading();
            if (data.status == 200) {
              this.$refs.seiminModel.show({
                skin: "toast",
                icon: "success",
                content: "您申请的工单已建单成功，工作人员会尽快处理！",
                btns:[{
                  click:()=>{
                    uni.navigateTo({
                      url: '/pages/patientList/patientList'
                    })
                  }
                }]
              });
            } else if (data.status == 1000033) {
              //重复建单那策略
              this.showReaptModal(data.msg, postData);
            } else {
              this.$refs.seiminModel.show({
                skin: "toast",
                icon: "error",
                content: data.msg || "创建失败",
                btns:[{
                  click:()=>{
                    uni.navigateTo({
                      url: '/pages/patientList/patientList'
                    })
                  }
                }]
              });
            }
          });
      },
      // 确定建单
      buildOrder() {
        let postData = {
          workOrder: {
            sourceId: this.SOURCEID['PDA'],
            taskType: {
              id: this.patientTaskType.id
            },
            startDept: {
              id: this.patientBuildData.dept.startDept.id
            },
            endDepts: [{
              id: this.patientBuildData.dept.endDept.id
            }],
            createDept: this.loginInfo.user.dept.id,
            patient: {
              patientCode: this.selectedPatient.patientCode,
            },
          },
        };
        // 半程陪检
        if (this.patientTaskType.associationType.value == this.ASSOCIATION_TYPES['患者陪检业务']) {
          postData.workOrder.taskType.isHalfInspect =
            this.patientTaskType.isHalfInspect === 1 ? 1 : 0; //半程陪检
        }
        // 携带设备
        let goodIds = '';
        goodIds = this.patientBuildData.goods ? this.patientBuildData.goods.map(v => v.id).toString() :
          '';
        postData.workOrder["goods"] = goodIds;
        // 加急原因
        if (!this.patientBuildData.isYY && this.patientTaskType.allowUrgent == 1 && this.patientBuildData.urgent
          .isUrgent) {
          postData.workOrder["urgentDetails"] = {
            checkStatus: {
              id: 329
            },
            urgentReason: this.patientBuildData.urgent.urgentRemark,
          };
        }
        // ---------------------
        let yy = false; //检查上是否有预约时间
        if (this.patientBuildData.checks && this.patientBuildData.checks.length) {
          yy = this.patientBuildData.checks.some((e) => e.yyTime);

        }
        if (
          yy &&
          this.patientBuildTrip.status == 200 &&
          this.patientBuildData.checks &&
          this.patientBuildData.checks.length
        ) {
          // 有预约时间
          postData.workOrder["checkList"] = this.patientBuildData.checks || [];
          this.$refs.seiminModel.show({
            icon: "warn",
            content: "您确认建单吗？",
            btns: [{
              name: "取消",
              type: "default",
              click: () => {
                this.$refs.seiminModel.close();
              },
            }, {
              name: "确认",
              type: "primary",
              click: () => {
                this.$refs.seiminModel.close();
                if (this.patientBuildData.isYY) {
                  postData.workOrder.yyTime = this.patientBuildData.yyTime;
                }
                this.buildCommon(postData, null, 'patient-yy');
              },
            }, ]
          });
        } else {
          if (!yy && this.patientBuildTrip.status == 200) {
            postData.workOrder["checkList"] = this.patientBuildData.checks || [];
            // 添加预约时间
            if (this.patientBuildData.isYY) {
              postData.workOrder.yyTime = this.patientBuildData.yyTime;
            }
          } else {
            // 添加预约时间
            if (this.patientBuildData.isYY) {
              postData.workOrder.yyTime = this.patientBuildData.yyTime;
            }
          }
          if (this.patientBuildTrip.status != 200) {
            //微信端不需要自动送回功能
            postData.workOrder["isRemand"] = 0;

          }
          this.buildCommon(postData, null, 'patient');
        }
      },
      // 初始化
      init() {
        // 设备
        this.goodsName = this.patientBuildData.goods ? this.patientBuildData.goods.map(v => v.name).toString() :
          '';
        if (this.patientTaskType.associationType.value == this.ASSOCIATION_TYPES['患者陪检业务']) {
          // 患者陪检业务
          let execDeptArr = this.patientBuildData.checks.map(v => v.execDept);
          execDeptArr = uniqBy(execDeptArr, 'id');
          console.log(execDeptArr)
          this.checkDeptsName = execDeptArr.map(v => {
            return this.deptDisplay == 2 ? v.deptalias : v.dept;
          }).toString();
        }
      },
    },
    onLoad(queryParams) {
      this.queryParams = queryParams;
      this.init();
    },
  };
</script>

<style lang="scss" scoped>
  .patientBuildConfirm {
    margin-bottom: 100rpx;

    .qco_msg {
      padding: 32rpx;
      color: #999;
      line-height: 40rpx;
      font-size: 32rpx;
      text-align: center;
    }

    .orderDetail_info {
      padding: 0 24rpx;

      .orderDetail_infoItem {
        width: 702rpx;
        height: 80vh;
        background-color: #fff;
        margin-top: 8rpx;
        border-radius: 8rpx;
        position: relative;
        padding: 0 24rpx 24rpx;
        font-size: 32rpx;
        @include border;
        @include semicircle(#f9fafb, 82rpx);
        @include flex(flex-start, stretch, column);

        .ji,
        .jiaji {
          width: 60rpx;
          position: absolute;
          right: 0;
          top: 0;
        }

        .orderDetail_infoItem_header {
          height: 86rpx;
          @include border($directive:bottom, $style:dashed);
          @include flex(space-between, center);

          .orderDetail_infoItem_header_title {
            color: #333;
            flex: 1;
            @include flex(flex-start, center);

            .icon {
              width: 10rpx;
              height: 46rpx;
              border-radius: 2rpx;
              background-color: #F0F6ED;
              @include btn_background;
            }

            .taskNameAndWorkerName {
              flex: 1;
              @include flex;

              .taskName {
                max-width: 10em;
                margin-left: 8rpx;
                font-size: 38rpx;
                font-weight: bold;
                @include clamp;
              }
            }
          }

          .orderDetail_infoItem_header_more {
            color: $defaultColor;
            font-weight: bold;
            font-size: 38rpx;
            @include clamp;
          }
        }

        .orderDetail_infoItem_item {
          padding-top: 12rpx;
          padding-bottom: 12rpx;
          color: #333;
          font-size: 30rpx;
          flex: 1;
          @include border(bottom);
          @include flex(flex-start, stretch, column);

          &.process {
            padding-top: 90rpx;
            padding-bottom: 90rpx;
          }

          &:last-of-type {
            border-bottom: none;
          }

          // 工单信息
          .orderDetail_infoItem_item_content {
            margin-top: 20rpx;
            @include flex(space-between, stretch);

            .orderDetail_infoItem_item_name {
              font-size: 34rpx;
              color: #666;
              max-width: 4em;
            }

            .orderDetail_infoItem_item_value {
              font-size: 38rpx;
              color: #333;
              font-weight: bold;
              max-width: 420rpx;
              text-align: justify;
              word-break: break-all;
            }
          }

          // 流程信息
          .orderDetail_process_item {
            min-height: 120rpx;
            line-height: 50rpx;
            color: #333;
            @include flex(center);

            &:last-of-type {
              .step_icon {
                &::after {
                  display: none;
                }
              }
            }

            .step_infoStart {
              font-size: 28rpx;
              flex: 1;
              @include flex(flex-end);

              .step_time {
                margin-left: 16rpx;
              }
            }

            .step_icon {
              font-size: 38rpx;
              margin-left: 30rpx;
              margin-right: 30rpx;
              position: relative;
              color: #E5E9ED;

              &.active {
                color: #07863C;
              }

              &::after {
                content: '';
                position: absolute;
                top: 60rpx;
                left: 18rpx;
                width: 1px;
                height: calc(100% - 70rpx);
                background-color: #DDE1E5;
              }
            }

            .step_infoEnd {
              font-size: 34rpx;
              flex: 1;
              padding-bottom: 16rpx;
            }
          }

          // 业务信息-检查
          &.business_inspect {
            .inspect_info {
              font-size: 34rpx;
              color: #333;
              padding-top: 20rpx;
              padding-bottom: 20rpx;
              @include border($directive:bottom, $style:dashed);

              .inspect_info_block {
                height: 60rpx;
                @include flex(space-between, center);

                .inspect_info_left {
                  @include flex;

                  .inspect_info_icon {
                    width: 50rpx;
                    height: 50rpx;
                    line-height: 50rpx;
                    border-radius: 50%;
                    font-size: 28rpx;
                    margin-right: 8rpx;
                    @include flex(center, center);

                    &.green {
                      color: $defaultColor;
                      border: 1px solid $defaultColor;
                      background-color: rgba(73, 184, 86, 0.1);
                    }

                    &.red {
                      color: #FF3B53;
                      border: 1px solid #FF3B53;
                      background-color: #FFE8EB;
                    }
                  }

                  .inspect_info_name {
                    font-weight: bold;
                  }
                }

                .inspect_info_right {
                  font-weight: bold;
                }
              }
            }

            .inspect_item {
              color: #333;
              font-size: 34rpx;
              line-height: 48rpx;
              padding-top: 26rpx;
              padding-bottom: 26rpx;
              @include border($directive:bottom, $style:dashed);

              .inspect_item_name {
                font-weight: bold;
              }

              .inspect_item_yytime {
                font-weight: bold;
              }

              .inspect_item_info {
                margin-top: 16rpx;
                margin-bottom: 16rpx;
                @include flex(space-between, center);

                .inspect_item_dept {
                  flex: 1;
                  word-break: break-all;
                  @include clamp;
                }

                .inspect_item_number {
                  flex: 1;
                  text-align: right;
                  word-break: break-all;
                  @include clamp;
                }
              }
            }
          }

          // 业务信息-标本
          &.business_specimen {
            font-size: 34rpx;

            .th {
              background-color: red;
              @include btn_background;

              th {
                color: #fff;
              }
            }

            .td {
              position: relative;

              .urgent {
                width: 60rpx;
                position: absolute !important;
                right: 0;
                top: 0;
              }
            }

            .table--border {
              border: none;
            }

            ::v-deep .uni-table {
              min-width: 0;
            }

            ::v-deep .uni-table-td {
              word-break: break-all;
            }
          }

          // 业务信息-药品
          &.business_drugsBag {
            .drugsBag_item {
              color: #333;
              font-size: 34rpx;
              margin-top: 20rpx;
              @include flex(space-between, center);

              .drugsBag_item_name {}

              .drugsBag_item_value {
                font-weight: bold;
              }
            }
          }
        }
      }
    }
  }
</style>
