<template>
  <view>
    <u-popup mode="bottom" v-model="isShow" :mask-close-able="false">
      <view class="popup_box">
        <view class="pop_title uni-flex space-between">
          <view class="" style="font-size: 35rpx"> 扫描{{ title }} </view>

          <view class="">
            <image class="fr icons_scan_close" src="/static/icons/icons_scan_close.svg" @click="closeScanPopup()"></image>
          </view>
        </view>
        <view class="uni-flex uni-row" style="align-items: center; background-color: #fff; margin-left: 20rpx; margin-right: 20rpx; border-radius: 8rpx; height: 30px">
          <view class="uni-center" style="width: 25%"> 位置 </view>
          <view class="" style="width: 75%; padding: 8rpx">
            <view class="uni-flex u-col-center uni-row" @click="showSelect">
              <view class="" style="margin-left: 20rpx">
                {{ positionInfo }}
              </view>
              <u-select v-model="show" mode="mutil-column-auto" :list="positionList" @confirm="confirmSelect"></u-select>
            </view>
          </view>
        </view>
        <view class="">
          <view class="">
            <win-com-scan ref="comscan" :placeholder="title" @getResult="getScanResult" :headerType="headerType" :isShowHistory="isShowHistory" :clearResult="true"></win-com-scan>
          </view>
        </view>
      </view>
    </u-popup>
  </view>
  <com-message ref="comMessageRef" />
</template>

<script setup lang="ts">
import { ref, watch, nextTick, onMounted, getCurrentInstance } from 'vue'
import winComScan from '@/mycomponents/scan/winComScan.vue'

import { getWorkShopLineStation } from '@/api/request2.js'

const { proxy } = getCurrentInstance()

const props = defineProps({
  title: {
    type: String,
    default: '箱标签'
  },
  isShowHistory: {
    type: Boolean,
    default: true
  },
  headerType: {
    type: String,
    default: 'HPQ,HMQ'
  }
})
const show = ref(false)
const isShow = ref(false)
const positionInfo = ref('请选择位置')
const positionList = ref([])
const productionLineCode = ref('')
const rawLocationCode = ref('')
const fgLocationCode = ref('')
const workshopCode = ref('')
const workStationCode = ref('')
const workShopName = ref('')
const productionLineName = ref('')
const workStationName = ref('')
const isEditPosition = ref(true)
const comMessageRef = ref()
const comscan = ref()
const openScanPopup = () => {
  if (positionList.value.length == 0) {
    proxy.$modal.loading('数据加载中....')
    getWorkShopLineStation()
      .then((res) => {
        uni.hideLoading()
        positionList.value = res.data
      })
      .catch((error) => {
        uni.hideLoading()
        showErrorMessage(error)
      })
  }
  setTimeout((res) => {
    isShow.value = true
  }, 500)
}

const initData = () => {
  positionInfo.value = '请选择位置'
  positionList.value = []
  productionLineCode.value = ''
  rawLocationCode.value = ''
  fgLocationCode.value = ''
  workshopCode.value = ''
  workStationCode.value = ''
  workShopName.value = ''
  productionLineName.value = ''
  workStationName.value = ''
  show.value = false
  isEditPosition.value = true
}

const closeScanPopup = () => {
  isShow.value = false
  emit('close', '')
}
const scanClick = () => {
  comscan.value.clickScanMsg()
}

const cancelClick = () => {
  comscan.value.clearScanValue()
}
const getScanResult = (result) => {
  if (positionInfo.value == '请选择位置') {
    showErrorMessage('请先选择位置')
    return
  }
  const param = {
    positionInfo: positionInfo.value,
    workshopCode: workshopCode.value,
    productionLineCode: productionLineCode.value,
    workStationCode: workStationCode.value,
    rawLocationCode: rawLocationCode.value,
    fgLocationCode: fgLocationCode.value
  }
  if (result.success) {
    isEditPosition.value = false
    emit('getResult', result, param)
  } else {
    showErrorMessage(result.message)
  }
}

const getfocus = () => {
  if (comscan.value != undefined) {
    comscan.value.getfocus()
  }
}

const losefocus = () => {
  if (comscan.value != undefined) {
    comscan.value.losefocus()
  }
}
const showErrorMessage = (message) => {
  comMessageRef.value.showErrorMessage(message, (res) => {
    if (res) {
    }
  })
}

const showSelect = () => {
  if (isEditPosition.value) {
    show.value = !show.value
  }
}
const confirmSelect = (e) => {
  positionInfo.value = `${e[0].label}-${e[1].label}-${e[2].label}`
  workshopCode.value = e[0].value
  productionLineCode.value = e[1].value
  workStationCode.value = e[2].value
  workShopName.value = e[0].label
  productionLineName.value = e[1].label
  workStationName.value = e[2].label

  const shop = positionList.value.find((shop) => shop.value == workshopCode.value)
  if (shop != undefined && shop.children != undefined) {
    const prodLine = shop.children.find((line) => line.value == productionLineCode.value)
    if (prodLine != undefined && prodLine.children != undefined) {
      const station = prodLine.children.find((r) => r.value == workStationCode.value)
      if (station.rawLocationCode == '' && station.rawLocationCode == null) {
        showErrorMessage(`${workStationName.value}的原材料库位为空,请重新选择`)
      } else {
        rawLocationCode.value = station.rawLocationCode
        fgLocationCode.value = station.fgLocationCode
      }
    } else {
      showErrorMessage('生产线-工位基础信息维护错误')
    }
  } else {
    showErrorMessage('车间-生产线基础信息维护错误')
  }
}
// 传递给父类
const emit = defineEmits(['getResult', 'close'])
defineExpose({
  openScanPopup,
  initData,
  getfocus,
  losefocus
})
</script>

<style lang="scss">
.scroll-view {
  overflow-y: scroll;
  height: auto;
  max-height: 300rpx;
}
</style>
