<template>
  <view class="settingCode">
    <view class="scanning_btn" hover-class="seimin-btn-hover" @click="drugsScanning()">
      <text class="icon_transport transport-saoma"></text>
    </view>
    <view class="scanFont">可通过扫一扫功能设置科室，扫描二维码后设置或替换科室</view>
    <view class="scanFont nr">
      <text v-if="queryDept">查询科室结果：{{queryDept.dept}}<text v-if="queryDept.name">（{{queryDept.name}}）</text><text v-if="queryDept.sourceQr" class="red">（配置）</text></text>
      <text v-else>查询科室结果：无</text>
    </view>
    <view class="scanFont nr red">{{msg}}</view>
    <footTool :settings="settings" @operate="operate"></footTool>
    <!-- 弹窗 -->
    <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 footTool from "../../components/footTool/footTool.vue";
  import {
    get,
    post,
    SM,
    webHandle
  } from "../../http/http.js";
  export default {
    data() {
      return {
        SMFlag:true,
        queryDept: null, //根据二维码查询到的科室
        settings: [], //底部按钮设置
        msg: '', //提示信息
        // 弹窗model
        models: {
          disjunctor: false,
        },
        options: null, //url传参
        index: -1, //当前操作的索引
        qrCode: '', //当前扫描的二维码
        resetGo: false, //是否回到初始界面
      }
    },
    components: {
      footTool
    },
    methods: {
      //抢单后知道了
      know() {
        this.models.disjunctor = false;
        if (this.resetGo) {
          uni.navigateTo({
            url: `../settingCode/settingCode`,
          });
        }
      },
      //确定
      ok() {
        this.models.disjunctor = false;
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        if (this.settings[this.index].uniName == 'replaceSave') { //替换保存
          let postData = {
            "type": "update",
            "qrCode": this.qrCode,
            "sourceQr": this.queryDept.sourceQr ? +this.queryDept.sourceQr : undefined,
            "destQr": this.options.destQr ? +this.options.destQr : undefined,
            "sourceDept": this.queryDept.sourceQr ? undefined : +this.queryDept.id,
            "destDept": this.options.destQr ? undefined : +this.options.targetId,
          };
          post("/dept/scanChangeDept", postData).then((res) => {
            uni.hideLoading();
            let msg = "保存失败";
            let icon = "error";
            this.resetGo = false;
            if (res.status == 200) {
              this.resetGo = true;
              msg = "保存成功";
              icon = "success";
            }
            this.models = {
              disjunctor: true,
              content: msg,
              icon: icon,
              operate: {
                know: "知道了",
              },
            };
          })
        } else if (this.settings[this.index].uniName == 'settingSave') { //设置保存
          let postData = {
            "type": "save",
            "qrCode": this.qrCode,
            "destDept": +this.options.targetId,
            "qrId": this.options.qrId ? +this.options.qrId : undefined,
          }
          post("/dept/scanChangeDept", postData).then((res) => {
            uni.hideLoading();
            let msg = "保存失败";
            let icon = "error";
            this.resetGo = false;
            if (res.status == 200) {
              this.resetGo = true;
              msg = "保存成功";
              icon = "success";
            }
            this.models = {
              disjunctor: true,
              content: msg,
              icon: icon,
              operate: {
                know: "知道了",
              },
            };
          })
        }
      },
      //取消
      cancel() {
        this.models.disjunctor = false;
      },
      // 底部按钮操作
      operate(i) {
        console.log(i,this.settings,'seimin')
        if (this.settings[i].uniName == 'replaceGo') { //替换,去选择科室
          uni.navigateTo({
            url: `../search/search?type=settingCode&uniName=${this.settings[i].uniName}&queryDept=${this.queryDept.dept}&queryDeptId=${this.queryDept.id}&sourceQr=${this.queryDept.sourceQr || ''}&qrCode=${this.qrCode}`,
          });
        } else if (this.settings[i].uniName == 'settingGo') { //设置,去选择科室
          uni.navigateTo({
            url: `../search/search?type=settingCode&uniName=${this.settings[i].uniName}&qrCode=${this.qrCode}`,
          });
        } else if (this.settings[i].uniName == 'replaceSave') { //替换保存
          this.index = i;
          this.models = {
            disjunctor: true,
            title: "提示",
            content: `此二维码绑定${this.options.targetDept}成功，${this.queryDept.dept}的二维码被清空`,
            icon: "warn",
            operate: {
              ok: "确定",
              cancel: "取消",
            },
          };
        } else if (this.settings[i].uniName == 'settingSave') { //设置保存
          this.index = i;
          this.models = {
            disjunctor: true,
            title: "提示",
            content: `将设置${this.options.targetDept}到此二维码上`,
            icon: "warn",
            operate: {
              ok: "确定",
              cancel: "取消",
            },
          };
        }
      },
      // 扫一扫
      drugsScanning() {
        if (!this.SMFlag) {
          return;
        }
        this.SMFlag = false;
        SM().then((ress) => {
          this.qrCode = ress;
          let postData = {
            "type": "query",
            "qrCode": ress
          };
          uni.showLoading({
            title: "加载中",
            mask: true,
          });
          post("/dept/scanChangeDept", postData).then((res) => {
            this.SMFlag = true;
            uni.hideLoading();
            if (res.status == 200) {
              if (res.data) { //替换
                if(res.data.deptDTO){
                  this.queryDept = {
                    dept: res.data.deptDTO.dept,
                    id: res.data.deptDTO.id,
                    sourceQr: res.data.id,
                    name: res.data.name,
                  };
                }else{
                  this.queryDept = {
                    dept: res.data.dept,
                    id: res.data.id
                  };
                }
                this.settings = [{
                  name: '替换',
                  uniName: 'replaceGo'
                }];
              } else { //设置
                this.queryDept = null;
                this.settings = [{
                  name: '设置',
                  uniName: 'settingGo'
                }];
              }
            } else {
              uni.showToast({
                icon: "none",
                title: res.msg || "接口获取数据失败！",
              });
            }
          })
        }).catch(err=>{
          this.SMFlag = true;
        });
      },
    },
    onShow(){
      this.SMFlag = true;
    },
    onLoad(options) {
      console.log(options,'seimin')
      this.options = options;
      if (options.uniName == "replaceGo") { //替换
        this.qrCode = options.qrCode
        this.queryDept = {
          dept: options.queryDept,
          id: options.queryDeptId,
          sourceQr: options.sourceQr,
          name: options.name,
        };
        this.msg = `此二维码绑定${options.targetDept}成功，${this.queryDept.dept}的二维码被清空`;
        this.settings = [{
          name: '替换',
          uniName: 'replaceGo'
        }, {
          name: '保存',
          uniName: 'replaceSave'
        }];
      } else if (options.uniName == "settingGo") { //设置
        this.qrCode = options.qrCode;
        this.msg = `将设置${options.targetDept}到此二维码上`;
        this.settings = [{
          name: '设置',
          uniName: 'settingGo'
        }, {
          name: '保存',
          uniName: 'settingSave'
        }];
      }
      // #ifdef APP-PLUS
      webHandle("no", "app");
      // #endif
      // #ifdef H5
      webHandle("no", "wx");
      // #endif
    }
  }
</script>

<style lang="less" scoped>
  .settingCode {
    background-color: rgb(249, 250, 251);
    padding-top: 36rpx;
    height: 100vh;
    box-sizing: border-box;

    .scanning_btn {
      margin: 0 auto;
      width: 138rpx;
      height: 138rpx;
      background: #72c172;
      border-radius: 50%;
      /*偏移*/
      line-height: 200rpx;
      color: #fff;
      font-size: 36rpx;
      display: flex;
      justify-content: center;
      align-items: center;

      text {
        font-size: 48rpx;
      }
    }

    .scanFont {
      font-size: 36rpx;
      font-weight: 700;
      margin: 32rpx;
      text-align: center;

      &.nr {
        font-weight: normal;
      }

      &.red {
        color: red;
      }
    }
  }
</style>
