<template>
  <view class="smallScreen">
    <view class="smallScreen_scan" @click="Scan_ss(sData,sType)">
      <slot></slot>
    </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 {
    post,
    SM
  } from "../../http/http.js";
  export default {
    data() {
      return {
        currentCode: '', //当前小扫描的科室二维码
        currentData: {}, //当前小扫描的工单对象
        // 填写交接人账号弹窗model
        hosModels: {
          disjunctor: false,
        },
        // SMFlag:true,
      }
    },
    props: {
      sData: {
        type: Object
      },
      sType: {
        type: Number
      }
    },
    methods: {
      // 如果不是静配，药配，标本配送,标本轮巡
      // 科室签到（小扫描）
      nextDeptOrder_ss(data, accountObj) {
        let isKs = 0;
        let ids = [];
        let id = data.id;
        ids.push(id);
        let list = {
          code: "",
          ids: ids,
        };
        let code = "";
        let type = "";
        if (this.currentCode) {
          let gdStateValue = data.gdState.value; //工单状态value
          let associationTypeValue = data.taskType.associationType.value; //关联类型value
          // 患者陪检，并且工单状态是待到达
          if (
            gdStateValue == "4" &&
            associationTypeValue == "inspect"
          ) {
            list.code = this.currentCode; //其他
            type = "handleIns";
          }
          // 其他临床服务，并且工单状态是待到达||工单状态待送达
          if (
            (associationTypeValue == "other" &&
              gdStateValue == "4") ||
            gdStateValue == "5"
          ) {
            code = this.currentCode;
            type = "orderSign/" + code;
            list = {
              ids
            };
            if (accountObj) {
              list.handover = [accountObj.accountId];
            }
            isKs = 1;
          }
          if (
            (gdStateValue == "5" &&
              associationTypeValue == "inspect") ||
            associationTypeValue == "patientTransport"
          ) {
            code = this.currentCode;
            type = "orderSign/" + code;
            list = {
              ids
            };
            if (accountObj) {
              list.handover = [accountObj.accountId];
            }
            isKs = 1;
          }
          if (gdStateValue == "8") {
            code = this.currentCode; //患者陪检--检查
            type = "orderSign/" + code;
            list = {
              ids
            };
            if (accountObj) {
              list.handover = [accountObj.accountId];
            }
            isKs = 1;
          }
          if (
            gdStateValue == "4" &&
            associationTypeValue == "patientTransport"
          ) {
            list = {
              code: this.currentCode, //患者转运--检查
              ids: ids,
            };
            type = "handleTrans";
          }
          post("/workerOrder/" + type, list).then((res) => {
            uni.hideLoading();
            if (res) {
              if (res.status == 200) {
                if (
                  gdStateValue == "4" &&
                  associationTypeValue == "other"
                ) {
                  uni.navigateTo({
                    url: `../../pages/scanning_code/scanning_code?type=${associationTypeValue}&type1=${res.type}&id=${data.id}&deptCode=${code}&dept=${res.dept}&accountObj=${encodeURIComponent(JSON.stringify(accountObj))}`,
                  });
                }
                if (
                  gdStateValue == "4" &&
                  associationTypeValue != "other"
                ) {
                  uni.navigateTo({
                    url: `../../pages/scanning_Result/scanning_Result?type=${
                    associationTypeValue
                  }&type1=${res.type}&id=${data.id}&deptCode=${
                    list.code
                  }&patient=${res.patient}&patientCode=${
                    res.patientCode
                  }&deptName=${res.deptName}&bedNum=${
                    res.bedNum
                  }&status=${res.status}&msg=${
                    res.msg
                  }&model=${encodeURIComponent(JSON.stringify(res))}&accountObj=${encodeURIComponent(JSON.stringify(accountObj))}`,
                  });
                }
                if (gdStateValue == "8") {
                  uni.navigateTo({
                    url: `../../pages/scanning_code/scanning_code?type=${associationTypeValue}&type1=${res.type}&id=${data.id}&deptCode=${code}&dept=${res.dept}&accountObj=${encodeURIComponent(JSON.stringify(accountObj))}`,
                  });
                }
                if (gdStateValue == "5") {
                  uni.navigateTo({
                    url: `../../pages/scanning_code/scanning_code?type=${associationTypeValue}&type1=${res.type}&id=${data.id}&deptCode=${code}&dept=${res.dept}&accountObj=${encodeURIComponent(JSON.stringify(accountObj))}`,
                  });
                }
              } else {
                console.log(res, "扫描失败");
                if (res.type == "trans-ddd-1") {
                  //起点科室扫描腕带码失败
                  uni.navigateTo({
                    url: `../../pages/scanning_Result/scanning_Result?type=${associationTypeValue}&type1=${res.type}&id=${data.id}&status=600&msg=${res.msg}`,
                  });
                } else {
                  uni.navigateTo({
                    url: `../../pages/scanning_Result/scanning_Result?type=${
                    associationTypeValue
                  }&type1=${res.type}&id=${data.id}&status=600&msg=${
                    res.msg
                  }&isKs=${isKs}&model=${encodeURIComponent(
                    JSON.stringify(res)
                  )}`,
                  });
                }
              }
            } else {
              uni.navigateTo({
                url: `../../pages/scanning_Result/scanning_Result?id=${data.id}&status=600&msg=扫码失败！请扫描正确的二维码！&isKs=${isKs}`,
              });
            }
          });
        }
      },
      // 如果不是患者陪检或患者转运或其他
      // 科室签到
      nextDeptOrder_s(data, accountObj) {
        console.log(data, accountObj)
        let ids = [];
        let id = data.id;
        ids.push(id);
        let code = "";
        let postData = {
          ids
        };
        if (accountObj) {
          postData.handover = [accountObj.accountId];
        }
        if (this.currentCode) {
          code = this.currentCode;
          // 科室签到
          post("/workerOrder/orderSign/" + code, postData).then((res) => {
            uni.hideLoading();
            if (res.status == 200) {
              // 跳转到扫描科室
              // type1: res.type, //type类型
              // id: data.id, //工单ID
              // deptCode: code, //二维码
              // dept: res.dept //科室名称
              uni.navigateTo({
                url: `../../pages/scanning_code/scanning_code?type=${data.taskType.associationType.value}&type1=${res.type}&id=${data.id}&deptCode=${code}&dept=${res.dept}&accountObj=${encodeURIComponent(JSON.stringify(accountObj))}&deptId=${res.deptId}`,
              });
            } else {
              uni.navigateTo({
                url: `../../pages/scanning_Result/scanning_Result?type=${data.taskType.associationType.value}&type1=${res.type}&id=${data.id}&status=600&msg=${res.msg}&isKs=1`,
              });
            }
          });
        }
      },
      // type==1  如果不是静配，药配，标本配送,标本轮巡
      // type==2  如果不是患者陪检，患者转运，其他
      // 扫码（小扫描）
      Scan_ss(data, type) {
        // if (!this.SMFlag) {
        //   return;
        // }
        // this.SMFlag = false;
        this.currentData = data;
        console.log(data, 'data');
        SM().then((ress1) => {
          uni.showLoading({
            title: "加载中",
            mask: true,
          });
          //检验二维码的有效性
          post("/dept/scanning", {
            content: ress1,
            taskTypeId: data.taskType.id,
            gdState: data.gdState.id,
          }).then((result) => {
            // this.SMFlag = true;
            this.currentCode = result.code;
            if (result.state == 200 || result.state == 201) {
              if (result.account) {
                if (type == 1) {
                  this.nextDeptOrder_ss(this.currentData, {
                    account: result.account,
                    accountName: result.name,
                    accountId: result.id,
                  });
                } else if (type == 2) {
                  this.nextDeptOrder_s(this.currentData, {
                    account: result.account,
                    accountName: result.name,
                    accountId: result.id,
                  });
                }
              } else {
                if (type == 1) {
                  this.nextDeptOrder_ss(this.currentData);
                } else if (type == 2) {
                  this.nextDeptOrder_s(this.currentData);
                }
              }
            } else if (result.state == '0000') {
              uni.hideLoading();
              this.showSelectAccount(); //yeye
            } else {
              uni.hideLoading();
              uni.showToast({
                icon: "none",
                title: "请求失败！",
              });
            }
          });
        }).catch(err=>{
          // this.SMFlag = true;
        });
      },
      // 填写交接人账号-确认
      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;
        let associationTypeValue = this.currentData.taskType.associationType.value;
        console.log(associationTypeValue)
        if (
          associationTypeValue !== "patientTransport" &&
          associationTypeValue !== "inspect" &&
          associationTypeValue !== "other"
        ) {
          this.nextDeptOrder_s(this.currentData, data);
        }
        if (
          associationTypeValue !== "jPBag" &&
          associationTypeValue !== "drugsBag" &&
          associationTypeValue !== "specimen" &&
          associationTypeValue !== "specimenPlan"
        ) {
          this.nextDeptOrder_ss(this.currentData, data);
        }
      },
      // 填写交接人账号-取消
      hosCancel() {
        this.hosModels.disjunctor = false;
      },
      // 填写交接人账号弹窗
      showSelectAccount() {
        this.hosModels = {
          title: '填写交接人账号',
          disjunctor: true,
        }
      },
    },
    components: {
      selectAccount
    },
    onShow(){
      // this.SMFlag = true;
    }
  };
</script>

<style lang="less">
  .smallScreen {
    width: 100%;
    height: 100%;

    .smallScreen_scan {
      width: 100%;
      height: 100%;
    }
  }
</style>
