<template>
  <view class="pharmacyDetails">
    <view class="pharmacyDetails_title">
      <view class="pharmacyDetails_tab" :class="{ 'active': selectedLabelSlots === item.id }" v-for="item in tabs"
        :key="item.id" @click="clickHandler(item.id)">
        <view class="pharmacyDetails_label">
          {{ item.label }}
        </view>
      </view>
    </view>
    <view class="page_item_wrap">
      <view class="page_item">
        <view class="page_item_top">
          <view class="page_item_top-inner" v-show="selectedLabelSlots == 1">
            <view class="page_item_top_L">
              <text class="L_text">请领单:{{ infoDATA.packid }}</text>
            </view>
            <view class="page_item_top_R">
              <text class="L_iocn">{{
                infoDATA.drugsState && infoDATA.drugsState.name
              }}</text>
            </view>
          </view>
        </view>
        <view class="page_item_cont" v-show="selectedLabelSlots == 1">
          <view class="page_item_cont_B">
            <view class="page_item_cont_title">
              <text>记账时间</text>
              <text class="text_big">
                <text>{{ infoDATA.creatTime }}</text>
              </text>
            </view>
          </view>
          <view class="page_item_cont_C">
            <view class="page_item_cont_title_C">
              <text>请领科室</text>
              <text class="text_big">
                <text>{{ infoDATA.target && infoDATA.target.dept }}</text>
              </text>
            </view>
          </view>
        </view>
        <scroll-view scroll-y class="page_item_cont" v-show="selectedLabelSlots == 2">
          <view class="page_item_cont_C" v-for="(item,i) in queryProcessList" :key="i">
            <view class="page_item_cont_title_C">
              <text style="width: 5em;">{{item.operationTime}}</text>
              <text style="flex: 1;">{{item.operationType?item.operationType.name:''}}</text>
              <view class="text_big" style="text-align: center;max-width: 10em;flex: 1;">
                <text>{{ item.username || '暂无' }}</text>
                <view v-if="item.operatorAccount" style="white-space: nowrap;text-overflow: ellipsis;overflow: hidden;">({{ item.operatorAccount }})</view>
              </view>
            </view>
          </view>
        </scroll-view>
        <view class="L"></view>
        <view class="R"></view>
      </view>
      <view class="L-l"></view>
      <view class="R-l"></view>
    </view>
    <view v-if="infoDATA.drugsState && infoDATA.drugsState.value == 4&&infoDATA.launch.id==loginUser.dept.id"
      class="btn-wrap">
      <button class="cube-toolbar-item1" :loading="isLoading" :disabled="isLoading" @click="dispensing(4)"
        v-if="infoDATA.drugsState && infoDATA.drugsState.value == 4&&infoDATA.launch.id==loginUser.dept.id">
        核对完成
      </button>
      <button class="cube-toolbar-item1" :loading="isOLoading" :disabled="isOLoading" @click="nursetake()">
        护士自取
      </button>
    </view>
    <button class="cube-toolbar-item" :loading="isOLoading" :disabled="isOLoading" @click="nursetake()"
      v-if="infoDATA.drugsState && infoDATA.drugsState.value == 8&&infoDATA.launch.id==loginUser.dept.id">
      护士自取
    </button>
    <button class="cube-toolbar-item" :loading="isLoading" :disabled="isLoading" @click="dispensing(1)"
      v-if="infoDATA.drugsState && infoDATA.drugsState.value == 1&&infoDATA.launch.id==loginUser.dept.id">
      开始配药
    </button>
    <view v-if="infoDATA.drugsState && infoDATA.drugsState.value == 2&&infoDATA.launch.id==loginUser.dept.id"
      class="btn-wrap">
      <button class="cube-toolbar-item1" :loading="isLoading" :disabled="isLoading" @click="dispensing(2)">
        进行核对
      </button>
      <button class="cube-toolbar-item1" :loading="isTLoading" :disabled="isTLoading" @click="replaceOperator()">
        替换配药人
      </button>
    </view>
    <!-- 护士自取账号弹窗 -->
    <selectAccount v-if="hosModels.disjunctor" :title="hosModels.title" :disjunctor="hosModels.disjunctor" @ok="hosOk"
      @cancel="hosCancel">
    </selectAccount>
  </view>
</template>
<script>
  import selectAccount from "@/components/selectAccount/selectAccount.vue";
  import {
    get,
    post,
    SM,
    webHandle
  } from "../../http/http.js";
  export default {
    data() {
      return {
        selectedLabelSlots: 1,
        tabs: [{
            id: 1,
            label: "基础信息",
          },
          {
            id: 2,
            label: "流程信息",
          }
        ],
        infoDATA: [],
        isLoading: false,
        isTLoading: false,
        isOLoading: false,
        loginUser: {}, //当前登录用户
        // 护士自取弹窗model
        hosModels: {
          disjunctor: false,
        },
        queryProcessList: [],
      };
    },
    components: {
      selectAccount
    },
    methods: {
      // tab切换
      clickHandler(id) {
        this.selectedLabelSlots = id;
        if (id == 1) {
          this.queryBase(this.queryObj);
        } else if (id == 2) {
          this.queryProcess(this.queryObj);
        }
      },
      // 确认
      hosOk(data) {
        console.log(data);
        const {
          accountName,
          account,
          accountId
        } = data;
        if (!accountName && !account) {
          //没有填写交接人
          uni.showModal({
            title: '提示',
            content: "请填写账号!",
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定');
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          });
          return;
        } else if (!accountName && account || accountName && !account) {
          //没有填写
          uni.showModal({
            title: '提示',
            content: "请填写正确的账号!",
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定');
              } else if (res.cancel) {
                console.log('用户点击取消');
              }
            }
          });
          return;
        }
        this.hosModels.disjunctor = false;
        //请求接口
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        let postData = {
          id: this.infoDATA.id,
          packId: this.infoDATA.packid,
          recipient: accountId,
          recipientName: accountName
        };
        if (this.infoDATA.gdId) {
          postData.gdId = this.infoDATA.gdId;
        }
        post('/drugsBag/nurseByHerself', postData).then(result => {
          uni.hideLoading();
          if (result.state == 200) {
            uni.showModal({
              title: '提示',
              content: result.info || "护士自取成功!",
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  uni.navigateTo({
                    url: "../pharmacy/pharmacy",
                  });
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }
            });
          } else {
            uni.showModal({
              title: '提示',
              content: "请求失败!",
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  console.log('用户点击确定');
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }
            });
          }
        })
      },
      // 取消
      hosCancel() {
        this.hosModels.disjunctor = false;
      },
      // 护士自取
      nursetake() {
        this.hosModels = {
          title: '填写账号',
          disjunctor: true,
        }
      },
      // 替换配药人
      replaceOperator() {
        this.isTLoading = true;
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/drugsBag/replaceOperator", {
          packId: this.infoDATA.packid,
        }).then((result) => {
          this.isTLoading = false;
          uni.hideLoading();
          if (result.state == 200) {
            uni.showModal({
              title: '提示',
              content: result.info,
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  console.log('用户点击确定');
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }
            });
          } else {
            uni.showModal({
              title: '提示',
              content: "请求失败!",
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  console.log('用户点击确定');
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }
            });
          }
        })
      },
      // 开始配药,进行核对,1是待配药,2是配药中,4是核对中
      dispensing(type) {
        let msg = '';
        if (type == 1) {
          msg = '是否开始配药?'
        } else if (type == 2) {
          msg = '是否进行核对?'
        } else if (type == 4) {
          msg = '是否核对完成?'
        }
        uni.showModal({
          title: '提示',
          content: msg,
          success: res => {
            if (res.confirm) {
              console.log('用户点击确定');
              this.isLoading = true;
              uni.showLoading({
                title: "加载中",
                mask: true,
              });
              if (type == 1) {
                post("/drugsBag/changeToIng", {
                  id: this.infoDATA.id,
                }).then((result) => {
                  this.isLoading = false;
                  uni.hideLoading();
                  if (result.state == 200) {
                    uni.showModal({
                      title: '提示',
                      content: result.info || "开始配药成功!",
                      showCancel: false,
                      success: function(res) {
                        if (res.confirm) {
                          uni.navigateTo({
                            url: "../pharmacy/pharmacy",
                          });
                        } else if (res.cancel) {
                          console.log('用户点击取消');
                        }
                      }
                    });
                    // let data = {
                    //   launchId: this.infoDATA.launch.id,
                    //   drugsBagId: this.infoDATA.id,
                    // };
                    // post("/drugsBag/autoDrugsBags", data).then((result1) => {
                    //   this.isLoading = false;
                    //   if (result1.status == 200) {
                    //     uni.showToast({
                    //       title: "操作成功!",
                    //       success() {
                    //         uni.navigateTo({
                    //           url: "../pharmacy/pharmacy",
                    //         });
                    //       },
                    //     });
                    //   }else if(result1.status == 501){
                    //     uni.showToast({
                    //       icon: "none",
                    //       title: result1.error,
                    //     });
                    //   }else {
                    //     uni.showToast({
                    //       icon: "none",
                    //       title: "请求失败!",
                    //     });
                    //   }
                    // });
                    // } else if(result.state == 503){//未建单
                    //   uni.showToast({
                    //     icon: "none",
                    //     title: result.error,
                    //   });
                  } else {
                    // this.isLoading = false;
                    uni.showModal({
                      title: '提示',
                      content: "请求失败!",
                      showCancel: false,
                      success: function(res) {
                        if (res.confirm) {
                          console.log('用户点击确定');
                        } else if (res.cancel) {
                          console.log('用户点击取消');
                        }
                      }
                    });
                  }
                });
              } else if (type == 2) {
                post("/drugsBag/changeToCheck", {
                  id: this.infoDATA.id,
                }).then((result) => {
                  this.isLoading = false;
                  uni.hideLoading();
                  if (result.state == 200) {
                    uni.showModal({
                      title: '提示',
                      content: result.info || "进行核对成功!",
                      showCancel: false,
                      success: function(res) {
                        if (res.confirm) {
                          uni.navigateTo({
                            url: "../pharmacy/pharmacy",
                          });
                        } else if (res.cancel) {
                          console.log('用户点击取消');
                        }
                      }
                    });
                  } else {
                    uni.showModal({
                      title: '提示',
                      content: "请求失败!",
                      showCancel: false,
                      success: function(res) {
                        if (res.confirm) {
                          console.log('用户点击确定');
                        } else if (res.cancel) {
                          console.log('用户点击取消');
                        }
                      }
                    });
                  }
                });
              } else if (type == 4) {
                post("/drugsBag/checkComplete", {
                  id: this.infoDATA.id,
                  packId: this.infoDATA.packid
                }).then((result) => {
                  this.isLoading = false;
                  uni.hideLoading();
                  if (result.state == 200) {
                    uni.showModal({
                      title: '提示',
                      content: result.info || "核对完成成功!",
                      showCancel: false,
                      success: function(res) {
                        if (res.confirm) {
                          uni.navigateTo({
                            url: "../pharmacy/pharmacy",
                          });
                        } else if (res.cancel) {
                          console.log('用户点击取消');
                        }
                      }
                    });
                  } else {
                    uni.showModal({
                      title: '提示',
                      content: "请求失败!",
                      showCancel: false,
                      success: function(res) {
                        if (res.confirm) {
                          console.log('用户点击确定');
                        } else if (res.cancel) {
                          console.log('用户点击取消');
                        }
                      }
                    });
                  }
                });
              }
            } else if (res.cancel) {
              console.log('用户点击取消');
            }
          }
        });
      },
      //基础信息查询
      queryBase(options) {
        //扫码进入详情
        let qrcode = options.qrcode;
        if (qrcode) {
          uni.showLoading({
            title: "加载中",
            mask: true,
          });
          post("/drugsBag/getDrugsBagByQr", {
            qrcode,
          }).then((result) => {
            uni.hideLoading();
            if (result.state == 200) {
              this.infoDATA = result.result;
              if (this.loginUser.dept.id != this.infoDATA.launch.id) {
                uni.showModal({
                  title: '提示',
                  content: "您现在所在的科室是【" + this.loginUser.dept.dept + "】,药包所在的科室是【" + this.infoDATA.launch.dept +
                    "】,您不能进行操作",
                  showCancel: false,
                  success: function(res) {
                    if (res.confirm) {
                      console.log('用户点击确定');
                    } else if (res.cancel) {
                      console.log('用户点击取消');
                    }
                  }
                });
              }
            } else {
              uni.showToast({
                icon: "none",
                title: "请求失败!",
              });
            }
          });
        }
        // 点击列表进入详情
        let id = options.id;
        if (id) {
          uni.showLoading({
            title: "加载中",
            mask: true,
          });
          get("/api/fetchData/drugsBag/" + id).then((res) => {
            uni.hideLoading();
            if (res.status == 200) {
              this.infoDATA = res.data;
              if (this.loginUser.dept.id != this.infoDATA.launch.id) {
                uni.showModal({
                  title: '提示',
                  content: "您现在所在的科室是【" + this.loginUser.dept.dept + "】,药包所在的科室是【" + this.infoDATA.launch.dept +
                    "】,您不能进行操作",
                  showCancel: false,
                  success: function(res) {
                    if (res.confirm) {
                      console.log('用户点击确定');
                    } else if (res.cancel) {
                      console.log('用户点击取消');
                    }
                  }
                });
              }
            } else {
              uni.showToast({
                icon: "none",
                title: "请求失败!",
              });
            }
          });
        }
      },
      // 流程信息查询
      queryProcess(options) {
        //扫码进入详情
        let qrcode = options.qrcode;
        if (qrcode) {
          uni.showLoading({
            title: "加载中",
            mask: true,
          });
          let postData = {
            idx: 0,
            sum: 9999,
            drugsBagOperationLog: {
              qrcode,
              hosId: this.loginUser.currentHospital.id
            }
          }
          post("/simple/data/fetchDataList/drugsBagOperationLog", postData).then((result) => {
            uni.hideLoading();
            if (result.status == 200) {
              result.list.forEach(v => {
                if (v.operationTime) {
                  v.operationTime = (new Date(v.operationTime)).Format("MM-dd hh:mm:ss");
                }
              });
              this.queryProcessList = result.list;
            } else {
              uni.showToast({
                icon: "none",
                title: "请求失败!",
              });
            }
          });
        }
        // 点击列表进入详情
        let packid = options.packid;
        if (packid) {
          uni.showLoading({
            title: "加载中",
            mask: true,
          });
          let postData = {
            idx: 0,
            sum: 9999,
            drugsBagOperationLog: {
              packid: options.packid,
              hosId: this.loginUser.currentHospital.id
            }
          }
          post("/simple/data/fetchDataList/drugsBagOperationLog", postData).then((res) => {
            uni.hideLoading();
            if (res.status == 200) {
              res.list.forEach(v => {
                if (v.operationTime) {
                  v.operationTime = (new Date(v.operationTime)).Format("MM-dd hh:mm:ss");
                }
              });
              this.queryProcessList = res.list;
            } else {
              uni.showToast({
                icon: "none",
                title: "请求失败!",
              });
            }
          });
        }
      }
    },
    onLoad(options) {
      this.loginUser = uni.getStorageSync("userData").user;
      console.log(this.loginUser, 'loginUserloginUser')
      // #ifdef APP-PLUS
      webHandle("no", "app");
      // #endif
      // #ifdef H5
      webHandle("no", "wx");
      // #endif
      this.queryObj = options;
      this.queryBase(this.queryObj);
    },
  };
</script>
<style lang="less">
  .pharmacyDetails {
    background-color: rgb(249, 250, 251);
    padding-top: 32rpx;

    .pharmacyDetails_title {
      height: 100rpx;
      display: flex;
      align-items: center;
      background-color: #fff;

      .pharmacyDetails_tab {
        flex: 1;
        display: flex;
        align-items: center;

        .pharmacyDetails_label {
          display: flex;
          justify-content: center;
          align-items: center;
          font-size: 40rpx;
          width: 100%;
          height: 66rpx;
          border-radius: 32rpx;
          color: #666;
        }

        &.active {
          .pharmacyDetails_label {
            color: #333;
            background-color: #e5e9ed;
          }
        }
      }
    }

    .page_item_wrap {
      position: relative;
      margin-top: 32rpx;

      .page_item {
        margin-top: 16rpx;
        margin-bottom: 124rpx;
        background: #fff;
        border-radius: 8rpx;
        margin: 0 20rpx;
        border: 2rpx solid #e5e9ed;
        position: relative;
        overflow: hidden;
        padding: 0 16rpx;

        .L {
          width: 40rpx;
          height: 40rpx;
          border-radius: 50%;
          background: #f9fafb;
          position: absolute;
          left: -20rpx;
          top: 68rpx;
          border: 2rpx solid #e5e9ed;
        }

        .R {
          width: 40rpx;
          height: 40rpx;
          border-radius: 50%;
          background: #f9fafb;
          position: absolute;
          float: right;
          right: -20rpx;
          top: 68rpx;
          border: 2rpx solid #e5e9ed;
        }

        .page_item_top {
          height: 88rpx;
          border-bottom: 2rpx dashed #e5e9ed;
          padding: 0 16rpx;

          .page_item_top-inner {
            display: flex;
            justify-content: space-between;
            align-items: center;
            height: 100%;

            .page_item_top_L {
              .L_text {
                font-size: 32rpx;
                font-weight: 700;
              }
            }

            .page_item_top_R {
              font-size: 32rpx;

              .L_iocn {
                color: rgb(7, 134, 60);
                font-size: 36rpx;
                font-weight: 700;
              }
            }
          }
        }

        .page_item_cont {
          box-sizing: border-box;
          min-height: 180rpx;
          max-height: calc(100vh - 534rpx);
          padding: 0 16rpx;
          text-align: left;
          position: relative;

          .text_big {
            font-size: 32rpx;
            font-weight: 700;

            text {
              font-weight: 700;
              line-height: 1.5;
            }
          }

          .line {
            height: 20rpx;
            width: 2rpx;
            border-left: 2rpx solid #e5e9ed;
            position: absolute;
            top: 82rpx;
            left: 40rpx;
          }

          .page_item_cont_T {
            padding-top: 28rpx;
            font-size: 28rpx;

            .page_item_cont_title {
              height: 100%;
              font-size: 32rpx;
              display: flex;
              justify-content: space-between;
            }
          }

          .page_item_cont_B {
            padding-top: 28rpx;
            margin-bottom: 28rpx;

            .page_item_cont_title {
              font-size: 32rpx;
              display: flex;
              justify-content: space-between;
              align-items: center;
            }
          }

          .page_item_cont_C {
            margin-top: 14rpx;
            margin-bottom: 14rpx;

            .page_item_cont_title_C {
              font-size: 32rpx;
              display: flex;
              justify-content: space-between;
              align-items: center;
            }
          }

          #infos {
            display: none;
          }
        }
      }

      .L-l {
        width: 2rpx;
        height: 40rpx;
        background: #f9fafb;
        position: absolute;
        left: 20rpx;
        top: 72rpx;
      }

      .R-l {
        width: 2rpx;
        height: 40rpx;
        background: #f9fafb;
        position: absolute;
        right: 20rpx;
        top: 72rpx;
      }
    }

    .cube-toolbar-item {
      width: 710rpx;
      height: 68rpx;
      line-height: 68rpx;
      position: fixed;
      left: 20rpx;
      bottom: 160rpx;
      border-radius: 8rpx;
      background: linear-gradient(to right, #72c172, #3bb197);
      font-size: 36rpx;
      color: #fff;
      text-align: center;
    }

    .btn-wrap {
      width: 100%;
      display: flex;
      justify-content: space-between;
      position: fixed;
      left: 0;
      bottom: 160rpx;
    }

    .cube-toolbar-item1 {
      flex: 1;
      height: 68rpx;
      line-height: 68rpx;
      border-radius: 8rpx;
      margin: 0 1%;
      background: linear-gradient(to right, #72c172, #3bb197);
      font-size: 36rpx;
      color: #fff;
      text-align: center;
    }
  }
</style>