<template>
<div class="container">
  <my-nav></my-nav>
  <div class="content">
    <div class="reading" v-if="reading">
      出库扫描中<br/>
      请前往B区下架所需要的物料<br/>
      并将物料推出感应门
    </div>
    <div v-else style="height: 100%">
      <div class="title">扫描完成</div>
      <div class="list-wrap">
        <div class="list">
          <div class="sub-title">领料信息</div>
          <div class="row">
            <div class="col">物料编码：{{ data.materialCode }}</div>
          </div>
          <div class="row">
            <div class="col">物料名称：{{ data.materialName }}</div>
          </div>
          <div class="row">
            <div class="col">申请数量：{{ data.quantity }}</div>
          </div>
        </div>
        <div class="list">
          <div class="sub-title">出库信息</div>
          <div class="row">
            <div class="col">物料编码：{{ scanData.materialCode }}</div>
          </div>
          <div class="row">
            <div class="col">物料名称：{{ scanData.materialName }}</div>
          </div>
          <div class="row">
            <div class="col">出库数量：{{ scanData.quantity }}</div>
          </div>
          <div class="row">
            <div class="col">出库件数：{{ scanData.tagCount }}</div>
          </div>
        </div>
      </div>
      <div class="footer">
        <Button type="error" size="large" style="margin-right: 20px" @click="pass(0)">重新扫描</Button>
        <Button type="success" size="large" @click="pass(1)">确认无误</Button>
      </div>
    </div>
  </div>
</div>
</template>

<script>
import MyNav from '@/components/my-nav'
import * as signalR from "@microsoft/signalr";
import withUrl from "@/config/withUrl";
import {MessagePackHubProtocol} from "@microsoft/signalr-protocol-msgpack";
import {GetScanRfid, PlcStorageOut, ReScan} from "@/api/integrated";
export default {
  name: "store-in",
  data() {
    return {
      data: {},
      scanData: {},
      connection: null,
      reading: false
    }
  },
  components: {
    MyNav
  },
  methods: {
    // SignalR长连接
    SRConnection() {
      let _this = this
      this.connection = new signalR.HubConnectionBuilder()
          .withUrl(`${withUrl}/plcHub?groupName=storage${sessionStorage.getItem('plcCode')}&door=${sessionStorage.getItem('door')}`)
          .withAutomaticReconnect()
          .withHubProtocol(new MessagePackHubProtocol())
          .build()
      // 监听扫码
      this.connection.on('outStorageDataCheck', res => {
        console.log('出库----------------')
        console.log(res)
        this.connection.invoke('ReturnSureMes', res.MesId)
        this.getList()
      })
      this.connection.start().then(() => {
        console.log('连接成功--')
      }).catch(err => {
        console.log('err--')
        console.log(err)
        re_start()
      })
      // 监听网络断开
      this.connection.onclose(() => {
        this.connection = null
        console.log('网络已断开！')
        // this.$Message.error('网络连接断开')
        // this.$router.go(0)
      })
      // 异常重启
      async function re_start() {
        try {
          await _this.connection.start()
        } catch (err) {
          setTimeout(() => re_start(), 5000)
        }
      }
    },
    /* 是否通过 0否 1是 */
    pass(type) {
      if (type === 0) {
        this.$Message.info('请重新扫描入库！')
        ReScan({
          reScanType:1,
          storageType: sessionStorage.getItem('plcCode'),
          storageDoor: sessionStorage.getItem('door')
        })
        setTimeout(() => {
          this.reading = true
          this.scanData = {}
        }, 500)
      } else if (type === 1) {
        console.log(this.data.inOutStorageNo)
        return
        // 出库
        PlcStorageOut({
          inOutStorageNo: this.data.inOutStorageNo,
          storageDoor: sessionStorage.getItem('door'),
          plcStationCode: sessionStorage.getItem('plcCode'),
          rfidList: this.scanData.scanTagMesList.map(item => item.rfidCode)
        }).then(res => {
          if (res.code === 0) {
            this.$Message.info('出库成功！')
            setTimeout(() => {
              this.$router.push({
                name: 'Home',
                query: {
                  plcCode: sessionStorage.getItem('plcCode'),
                  door: sessionStorage.getItem('door')
                }
              })
            }, 500)
          }
        })
      }
    },
    getList () {
      GetScanRfid({
        e_StoragePlcCode: sessionStorage.getItem('plcCode')
      }).then(scanRes => {
        if (scanRes.code === 0 && scanRes.result.materialCode) {
          this.scanData = scanRes.result
          this.reading = false
        }
      })
    }
  },
  mounted() {
    this.data = JSON.parse(this.$route.query.data)
    this.getList()
    this.time = this.$dayjs().format('YYYY年MM月DD日 HH:mm')
    this.timer = setInterval(() => {
      this.time = this.$dayjs().format('YYYY年MM月DD日 HH:mm')
    }, 1000)
    this.SRConnection()
    console.log(this.data)
  },
  destroyed() {
    this.connection.stop()
    clearInterval(this.timer)
  }
}
</script>

<style lang="scss" scoped>
  .container {
    width: 100vw;
    height: 100vh;
    overflow: auto;
    //background: rgba(2, 125, 180, 1);
    background-image: linear-gradient(270deg,
        #2c3034 0%,
        #50575e 49%,
        #2c3034 100%);
    .header {
      padding: 40px 0;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left, .center, .right {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        color: #d7d7d7;
        font-size: 28px;
        font-weight: bold;
      }
      .left {
        .avator {
          padding: 0 10px;
          display: flex;
          align-items: center;
        }
      }
      .right {
        cursor: pointer;
      }
    }
    .content {
      margin: 10px;
      height: calc(100% - 142px);
      //background: rgba(1, 84, 120, 1);
      border-radius: 10px;
      .reading {
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 36px;
        font-weight: bold;
        color: #00FFFF;
      }
      .title {
        padding: 20px 0;
        text-align: center;
        font-size: 36px;
        font-weight: bold;
        color: #2dc57c;
      }
      .list-wrap {
        padding: 0 10px;
        width: 100%;
        height: calc(100% - 154px);
        display: flex;
        justify-content: space-between;
        .list {
          position: relative;
          padding: 20px 10px;
          flex: 1;
          height: 250px;
          //background: rgba(2, 125, 180, 0.486274509803922);
          background: #2c3034;
          border-radius: 10px;
          border: solid 1px #3879e7;
          .sub-title {
            padding: 5px 0;
            font-size: 24px;
            color: #95F204;
            font-weight: bold;
          }
          .row {
            margin-bottom: 10px;
            color: #95F204;
            font-size: 18px;
            font-weight: bold;
          }
          &:first-child {
            margin-right: 20px;
            .sub-title {
              color: #EC808D;
            }
            .row {
              color: #EC808D;
            }
          }
          &:last-child {
             margin-bottom: 0;
          }
        }
      }
      .footer {
        padding: 10px 0;
        /deep/ .ivu-btn-large {
          border-radius: 20px;
          font-weight: bold;
          width: 150px;
        }
      }
    }
  }
</style>
