<template>
  <view>
    <uni-nav-bar
      fixed="true"
      statusBar="true"
      right-icon="scan"
      title="工作台"
      @clickLeft="back"
      @clickRight="HandlScanCode"
    ></uni-nav-bar>
    <!-- <uni-nav-bar fixed="true" statusBar="true" title="工作台"></uni-nav-bar> -->
    <navigation ref="navigationRef"></navigation>
    <!-- 设备主管 -->
    <equipment v-if="roleId == 102" ref="equipmentRef"></equipment>
    <!-- 运维 -->
    <Mocha v-if="roleId == 157" ref="mochaRef"></Mocha>
    <!-- 模具舟皿 -->
    <mould v-if="roleId == 155" ref="mouldRef"></mould>
    <!-- 备品备件 -->
    <sparePart v-if="roleId == 150" ref="sparePartRef"></sparePart>
    <!-- <ScanCode ref="ScanCode" :model="'uni'"></ScanCode> -->
  </view>
</template>

<script>
import navigation from './components/navigation.vue'
import equipment from './pages/equipment/equipment.vue'
import Mocha from './pages/Mocha/Mocha.vue'
import mould from './pages/mould/mould.vue'
import sparePart from './pages/sparePart/sparePart.vue'
import ScanCode from '@/components/ScanCode.vue'
import { postJ, post, get } from '@/utils/api'
export default {
  components: {
    navigation,
    equipment,
    Mocha,
    mould,
    sparePart,
    ScanCode
  },
  onShow () {
    this.init()
    let _this = this
    uni.$off('scancodedate') // 每次进来先 移除全局自定义事件监听器
    uni.$on('scancodedate', function (data) {
      console.log(data, '-----scancodedate')
      _this.cbScancodedate(data)
    })
    // this.Scancodedate()
  },
  onUnload () {
    uni.$off('scancodedate')
  },
  onHide () {
    uni.$off('scancodedate')
  },
  data () {
    return {
      roleId: null,
      timer: null,
      qrContent: null,
      barType: null,
      // 扫码后的设备信息
      equipmentInfo: '',
      qrContent: '',
      barType: ''
    }
  },
  created () {
    uni.getStorage({
      key: 'userInfo',
      success: res => {
        this.roleId = res.data.role.id
      }
    })
  },
  methods: {
    init () {
      this.$refs.navigationRef && this.$refs.navigationRef.getCount()
      this.$refs.equipmentRef && this.$refs.equipmentRef.getData()
      this.$refs.mochaRef && this.$refs.mochaRef.getData()
      this.$refs.mouldRef &&
        (this.$refs.mouldRef.getBoatInventoryCountList(),
        this.$refs.mouldRef.getModeInventoryCountList())
      this.$refs.sparePartRef && this.$refs.sparePartRef.getData()
      this.timer = setTimeout(() => {
        this.init()
      }, 6000) 
    },
    // 相机扫码
    HandlScanCode () {
      let _this = this
      uni.scanCode({
        success: function (res) {
          _this.Scancodedate(res.result)
        }
      })
      //_this.Scancodedate('res')
    },
    // 根据条码请求设备数据
    getData () {
      let par = {
        barType: this.barType,
        qrContent: this.qrContent
      }
      this.equipmentInfo = {}
      return postJ(this.apiUrl + '/scan/getAssetInfo', par)
        .then(res => {
          console.log(res.data)
          this.equipmentInfo = res.data
        })
        .catch(err => {
          console.log(err, 'err-----------------')
          uni.showToast({
            title: '系统错误！',
            icon: 'none',
            duration: 2000
          })
        })
    },
    // 扫码枪扫码
    cbScancodedate (data) {
      this.Scancodedate(data.code)
    },
    async Scancodedate (code) {
      this.qrContent = code.trim()
      // this.qrContent = 'w01000002100001@_@0' //code.trim()
      //this.qrContent = '005/w01000001120041/锤锤专用02-不拆包-批量/规格01//成型/成型工序@_@0'
      this.barType = this.setBarType(this.qrContent)
      await this.getData()

      if (this.equipmentInfo.assetCode) {
        if (this.equipmentInfo.assetType != 1) {
          uni.showToast({
            title: '请扫描生产设备码！',
            icon: 'none',
            duration: 2000
          })

          return
        }
        let par = {
          info: encodeURIComponent(JSON.stringify(this.equipmentInfo)),
          qrContent: this.qrContent
        }
        par = this.URLSearchParams(par)
        console.log('qrwewett')
        uni.navigateTo({
          url: '/pages/equipment_ledger/index?' + par
        })
      }
    },
    // 设置barType
    setBarType (val) {
      let index = val.indexOf('@_@')
      let result = 0
      if (index !== -1) {
        let item = val.substr(index + 3, 1)
        if (item) {
          result = Number(item)
        }
      }
      return result
    }
  }
}
</script>

<style lang="scss" scoped>
page {
  background-color: $page-bg;
}
</style>
