<template>
  <!-- 盒条件-条条替换-->
  <div id="topDiv">
    <van-nav-bar
        :title="$route.meta.name"
        left-text=""
        left-arrow
        @click-left="$router.go(-1)"
        style="background-color: rgba(0, 105, 83, 1)"
    >
      <template #right>
        {{ $store.state.user.name }}
      </template>
    </van-nav-bar>
    <van-cell-group class="cell-flex-1-3 cell-flex-1-3_1">
      <van-cell title="当前条码" :value="qrCodeIns.qrCode"/>
      <van-cell title="卷烟信息" :value="qrCodeIns.brandName"/>
    </van-cell-group>
    <van-cell-group class="cell-flex-1-3 cell-flex-1-3_1">
      <van-cell title="新条码" :value="newParentCode"/>
    </van-cell-group>
  </div>

  <div class="btns">
    <van-button @click="clearSubmit()">清&nbsp;&nbsp;&nbsp;空</van-button>
    <van-button @click="recombinationSubmit()" :disabled="relevanceButton">完成替换</van-button>
  </div>
</template>

<script>
import ScanOverlay from "@/components/overlay/scanOverlay/ScanOverlay";
import {getQrcodeInsByCode} from "@/api/qrcode/relation/qrcodeIns";
import {Notify, Cell, CellGroup, Loading, NavBar, Overlay, Tab, Tabs, Button, Row, Col, Dialog} from "vant";
import {changePiece, unbondQrCodeOne, updateAndAddQrCodeIns} from "@/api/qrcode/relation/qrDiscardCode";
import {QrCodeUtil} from "@/api/qrcode/device/brand/brand";
import {getStringByTwo} from "@/utils/transition";

export default {
  name: "RecombinationPiece",
  components: {
    [NavBar.name]: NavBar,
    [CellGroup.name]: CellGroup,
    [Cell.name]: Cell,
    [Tabs.name]: Tabs,
    [Tab.name]: Tab,
    [Overlay.name]: Overlay,
    [Loading.name]: Loading,
    [Button.name]: Button,
    [Row.name]: Row,
    [Col.name]: Col,
    [Dialog.Component.name]: Dialog.Component,
  },
  data() {
    return {
      qrCodeTypeName: "烟码",
      qrCodeType: {
        '10': '盒烟码',
        '20': '条烟码',
        '30': '件烟码',
      },
      scanning: true,
      // 当前校验的二维码实例
      qrCodeIns: {
        childrenList: []
      },
      newParentCode:''
    }
  },

  mounted() {
    this.initHeight();
    // 扫描二维码
    this.$nativeQRCode.setCallBackFun((res) => {
      let qrCode = getStringByTwo(res)
      this.onScanQrCode(qrCode);
    });
    this.$nativeQRCode.setStatusBarColor("#006953", "white");
    ScanOverlay.show('请扫描二维码')

    // this.onScanQrCode("HTTPS://Y2WM.CN/99.1000.1/AC21150711041ZOLDX0WVBQ5WMPCXE6FYY23")
    // setTimeout(() => {
    //   this.onScanQrCode("HTTPS://Y2WM.CN/99.1000.1/AC21150711041ZOLDX0WVBQ5WSPCXE6FY14033333")
    // }, 1000)
  },
  computed: {
    // 判断按钮是否能够点击
    relevanceButton() {
      return this.newParentCode == null || this.newParentCode === '';
    }
  },
  methods: {
    // 初始化高度
    initHeight() {
      this.$nextTick(() => {
        // 屏幕高度
        let windowH = document.documentElement.clientHeight;
        // 顶部内容区域高度
        let topDivH = document.getElementById('topDiv').offsetHeight;
        this.tabs.height = windowH - topDivH;
        this.tabs.conHeight = this.tabs.height - 160;
      })
    },
    // 根据二维码获取二维码实例
    onScanQrCode(qrcode) {
      this.scanning = false;
      // 进入页面首次扫码
      if (!this.qrCodeIns.qrCode) {
        ScanOverlay.hide()
        getQrcodeInsByCode({qrcode}).then(res => {
          this.initQrcodeInfo(res.data);
        })
      } else {
        this.checkQrcode(qrcode)
      }
    },
    // 初始化二维码信息
    initQrcodeInfo(qrCodeInfo) {
      // 进入页面首次扫码
      if (!this.qrCodeIns.qrCode) {
        if (qrCodeInfo.type != '20'){
          Notify({ type: 'warning', message: "此功能只支持条码替换", duration: 1500 });
          return ;
        }
        if (qrCodeInfo.childrenList.length === 0) {
          Notify({type: 'warning', message: "该条没有关联关系，无法替换", duration: 1500});
          return ;
        }
        if(qrCodeInfo.bTaskId != null){
          Notify({type: 'warning', message: '该条已迁移，无法再次操作', duration: 1500});
          return
        }
        if(qrCodeInfo.unregistered == 5){
          Notify({type: 'warning', message: '该烟是黑户烟，无法进行替换，请先做解绑操作', duration: 1500});
          return
        }
        if (qrCodeInfo.status == 10){
          Notify({type: 'warning', message: '该条无关联关系，无法替换', duration: 1500});
          return
        }
        if (qrCodeInfo.parentCode != null && qrCodeInfo.parentCode !== ''){
          getQrcodeInsByCode({"qrcode":  qrCodeInfo.parentCode}).then(pres => {
            if(pres.data.operUserId === 60){
              Notify({type: 'warning', message: '该件已出库，无法再次操作', duration: 1500});
            }else {
              this.qrCodeIns = qrCodeInfo;
              this.qrCodeTypeName = this.qrCodeType[this.qrCodeIns.type];
            }
          })
        }else {
          this.qrCodeIns = qrCodeInfo;
          this.qrCodeTypeName = this.qrCodeType[this.qrCodeIns.type];
        }
      }
    },
    // 校验二维码
    checkQrcode(qrCode) {
      if (qrCode ==this.qrCodeIns.qrCode ) {
        Notify({type: 'warning', message: '请勿重复扫码'});
        return;
      }

      this.qrCodeUtil(qrCode)
    },
    // 品规校验
    qrCodeUtil(qrCode) {
      QrCodeUtil({"qrcode": qrCode}).then(res => {
        if (res.data.typeCode == 30 || res.data.typeCode == 10) {
          Notify({type: 'warning', message: "此功能只支持条码替换", duration: 1500});
        } else if (res.data.cgtCode != this.qrCodeIns.brandCode) {
          Notify({type: 'warning', message: "卷烟品规不同", duration: 1500});
        } else {
          //判断新条码是否有关联关系
          getQrcodeInsByCode({"qrcode": qrCode}).then(r => {
            if (r.data.parentCode != null && r.data.parentCode !== ''){
              getQrcodeInsByCode({"qrcode":  r.data.parentCode}).then(pres => {
                if(pres.data.operUserId == 60){
                  Notify({type: 'warning', message: '该烟已出库，无法再次操作', duration: 1500});
                  return
                }
                if(pres.data.bTaskId != null){
                  Notify({type: 'warning', message: '该烟已迁移，无法再次操作', duration: 1500});
                  return
                }
                if(pres.data.unregistered == 5){
                  Notify({type: 'warning', message: '该烟关联的件是黑户烟，无法进行替换，请先做解绑操作', duration: 1500});
                  return
                }
                if(pres.data.status == 30){
                  Notify({type: 'warning', message: '该烟关联的件是报废烟，无法进行替换', duration: 1500});
                  return
                }
                if(pres.data.status == 0){
                  Notify({type: 'warning', message: '该烟以成件，无法进行替换', duration: 1500});
                  return
                }
                if (r.data.childrenList.length > 0) {
                  Notify({type: 'warning', message: "新条已有关联关系，无法替换", duration: 1500});
                } else {
                  this.newParentCode = qrCode;
                }
              })
            }else {
              if(r.data.unregistered == 5){
                Notify({type: 'warning', message: '该烟是黑户烟，无法进行替换，请先做解绑操作', duration: 1500});
                return
              }
              if (r.data.childrenList.length > 0) {
                Notify({type: 'warning', message: "新条已有关联关系，无法替换", duration: 1500});
              } else {
                this.newParentCode = qrCode;
              }
            }

          }).catch(() => {
            Notify({type: 'warning', message: '', duration: 1});
            // 获取新件码
            this.newParentCode = qrCode;
          })
        }
      })
    },

    // 清空当前数据
    clearSubmit() {
      this.qrCodeIns = {
        childrenList: []
      }
      this.newParentCode = ''
    },
    // 关联提交
    recombinationSubmit() {
      if (!this.qrCodeIns || !this.newParentCode) return;
      ScanOverlay.show('替换中')
      changePiece({
        'type': this.qrCodeIns.type == 20 ? 1 : 2,
        'parentCode': this.newParentCode,
        'oldParentCode':this.qrCodeIns.qrCode
      }).then( res => {
        if (res.code == 200){
          Notify({type: 'success', message: '条条替换成功'});
          this.clearSubmit();
          ScanOverlay.show('请扫描二维码')
        }else {
          ScanOverlay.hide()
        }
      }).catch(()=>{
        ScanOverlay.hide()
      })
    },

  }

}
</script>

<style scoped>
:deep(.van-cell-group){
  width: 660px;
  margin: auto;
  background: none !important;
}
:deep(.cell-flex-1-3_1 .van-cell){
  height: 200px;
  margin-bottom: 20px;

  font-size: 26px;
  font-family: 'SourceHanSansCN-Medium',sans-serif;
  font-weight: 500;
  color: #000000;
}

:deep(.cell-flex-1-3_1 .van-cell:nth-of-type(1)){
  height: 200px;
  margin-bottom: 20px;
  line-height: 147px;
  margin-top: 20px;
  border-radius: 15px;
  align-items: center;
}
:deep(.cell-flex-1-3_1 .van-cell:nth-of-type(1) .van-cell__value){
    display: block;
    height: 100%;
    line-height: 35px;
    text-align: left;
    padding-top: 50px;

}
:deep(.cell-flex-1-3_1 .van-cell:nth-of-type(2)){
  height: 104px;
  margin-bottom: 20px;
  line-height: 104px;
  margin-top: 20px;
  border-radius: 15px;
  align-items: center;
}
:deep(.van-cell__value){
  font-family: 'SourceHanSansCN-Regular',sans-serif;
  color: rgba(67, 67, 67, 1);
}
:deep(.van-tabs__wrap){
  width: 660px;
  margin: auto;
  height: 95px;
  border-radius: 15px 15px 0 0 ;
}
:deep(.van-tab){
    font-size: 26px;
    font-family: 'SourceHanSansCN-Medium',sans-serif;
    font-weight: 500;
    color: #000000;
  }
  :deep(.van-tab__text--ellipsis){
    overflow: visible;
  }
  :deep(.van-tabs__line){
    width: 115px;
    background-color: rgba(0, 105, 83, 1);
    height: 4px;
  }
  :deep(.van-tabs__content){
    height: 600px !important;
    box-sizing: border-box;
    border-radius: 0px 0px 15px 15px;
    overflow: hidden;
  }
  :deep(.van-tabs__content .van-cell__value){
    height: 127px;
    line-height: 50px;
    font-size: 26px;
    font-family: Source Han Sans CN,sans-serif;
    font-weight: 400;
    color: #434343;
    padding-top: 5px;
  }
  :deep(.van-cell:after){
    border-bottom: 2px solid rgba(231, 231, 231, 1);
    height: 3px;
  }

  .box1-3-1{
    border-radius:0 0 15px 15px ;
    /* height: 600px !important; */
  }
  .btns{
    position: absolute;
    bottom: 0;
    width: 720px;
    height: 94px;
    background: #FFFFFF;
    display: flex;
    justify-content: center;
  }
  .btns>button{
    width: 271px;
    height: 70px;
    background: #E4E9E8;
    border-radius: 35px;
    margin: auto;
    font-size: 26px;
    font-family: 'SourceHanSansCN-Medium',sans-serif;
    font-weight: 500;
    color: #006953;
  }
  .btns>button:nth-of-type(2){
    background-color: rgba(33, 143, 120, 1);

color: #FFFFFF;
  }
  :deep(.van-nav-bar__right){
    font-size: 32px;
    font-family: Source Han Sans CN,sans-serif;
    font-weight: 400;
    color: #FFFFFF;
  }
</style>
