<template>
  <view class="">
    <u-popup v-model="isShow" mode="bottom">
      <view class="uni-flex uni-column pop_customer">
        <view class="" style="padding: 10rpx">
          <view class="uni-flex u-col-center uni-row space-between" style="padding: 10rpx 10rpx 20rpx 10rpx">
            <view class="" style="font-size: 35rpx">
              {{ title }}
            </view>
            <image style="width: 35rpx; height: 35rpx" src="/static/icons/icons_close.svg" @click="closeRequestPopup()"> </image>
          </view>
          <view class="split_line"></view>
          <view class="uni-flex uni-column" style="background-color: white">
            <view class="uni-flex uni-column">
              <view class="uni-flex uni-row padding title u-col-center">
                <text>从仓库代码：</text>
                <view class="uni-flex u-col-center uni-row" @click="showWarseHouseSelect">
                  <view class="" style="margin-left: 20rpx; width: 100%">
                    {{ fromWarehouseCode }}
                  </view>
                  <u-select v-model="showWareHouse" mode="single-column" :list="wareHouseList" @confirm="confirmWareHouse"></u-select>
                </view>
              </view>
              <view class="split_line"></view>
              <view class="uni-flex uni-row" style="align-items: center; background-color: #fff; margin-right: 20rpx; border-radius: 8rpx">
                <view class="uni-center" style="width: 25%; font-size: 32rpx"> 优先级 </view>
                <view style="width: 100%">
                  <uni-data-select style="padding: 20rpx" v-model="priority" :localdata="priorityList" :clear="false"></uni-data-select>
                </view>
              </view>
              <view class="split_line"></view>
              <view class="split_line"></view>
              <view class="uni-flex uni-row" style="align-items: center; background-color: #fff; margin-right: 20rpx; border-radius: 8rpx">
                <view class="uni-center" style="width: 25%; font-size: 32rpx"> 优先级 </view>
                <view style="width: 100%">
                  <uni-data-select style="padding: 20rpx" v-model="priority" :localdata="priorityList" :clear="false"></uni-data-select>
                </view>
              </view>
              <view class="uni-flex uni-row padding title u-col-center">
                <text>位置：</text>
                <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 class="split_line"></view>

              <view class="title padding" style="display: flex">
                <text style="flex-shrink: 0">物料：</text>
                <view class="" style="width: 100%">
                  {{ itemCode }}
                </view>
                <view class="">
                  <image src="/static/search.svg" mode="" style="width: 40rpx; height: 40rpx; margin-left: 20rpx" @click="itemCodeClick"> </image>
                </view>
              </view>

              <view class="split_line"></view>
              <view class="uni-flex uni-row padding title u-col-center">
                <text>数量： </text>
                <view class="uni-flex uni-row uni-center" style="display: flex; align-items: center; margin-left: 20rpx; justify-content: center">
                  <input style="text-align: center" class="qty_input" v-model="counQty" type="number" @confirm="confirm()" :focus="numberFocus" @input="checkNum" :maxlength="maxlength" />

                  <uomCom :uom="uom"></uomCom>
                  <view class="" v-if="stdPackInfo != undefined" style="display: flex; flex-direction: row; margin-left: 10rpx"> (<packQty :dataContent="stdPackInfo"></packQty>) </view>
                </view>
              </view>
              <view class="split_line"></view>
            </view>
          </view>
        </view>
        <view class="uni-flex uni-row hide_border">
          <button class="btn_edit_big_cancle" hover-class="btn_edit_big_after" @click="cancel()">取消</button>
          <button class="btn_edit_big_confirm" hover-class="btn_edit_big_after" @click="confirm()">确认</button>
        </view>
      </view>
    </u-popup>
    <win-scan-item ref="scanPopup" title="物料代码" @getScanResult="getItemScanResult" :itemCodeTypeList="itemCodeTypeList"> </win-scan-item>
    <com-message ref="comMessageRef" />
  </view>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, nextTick } from 'vue'
import { getBasicItemByCode, getProductionlineItem, getWorkShopLineStation, getWarehouseCodeList } from '@/api/request2.js'
import { getLocationTypeName, getListLocationTypeDesc, checkDirectoryItemExist } from '@/common/directory.js'
import uomCom from '@/mycomponents/qty/uom.vue'
import packQty from '@/mycomponents/qty/packQty.vue'
import balanceStatus from '@/mycomponents/status/balanceStatus.vue'
import winScanItem from '@/mycomponents/scan/winScanItem.vue'

const { proxy } = getCurrentInstance()
const props = defineProps({
  title: {
    type: String,
    default: '需求信息'
  },
  itemCodeTypeList: {
    type: Array,
    default: []
  }
})

const workshopCode = ref('') // 车间
const workShopName = ref('')
const productionLineCode = ref('') // 生产线
const productionLineName = ref('')
const workStationCode = ref('') // 工位
const workStationName = ref('')
const itemCode = ref('请扫描物料信息')
const fromWarehouseCode = ref('请选择仓库代码')
const itemName = ref('')
const qty = ref(0)
const rawLocationCode = ref('')
const fgLocationCode = ref('')
const itemCodeFocus = ref(false)
const requestInfo = ref(null)
const itemCodeList = ref([])
const counQty = ref(undefined)
const editPosition = ref(true)
const numberFocus = ref(false)
const uom = ref('')
const positionInfo = ref('请选择位置')
const show = ref(false)
const isModifiedPosition = ref(true)
const positionList = ref([])
const stdQty = ref(0) // 标包
const maxlength = ref(10)
const stdPackInfo = ref(undefined)
const wareHouseList = ref([])
const showWareHouse = ref(false)
const priorityList = ref([
  {
    value: 0,
    text: '低'
  },
  {
    value: 1,
    text: '中'
  },
  {
    value: 2,
    text: '高'
  }
])
const priority = ref(1) // 优先级
const isShow = ref(false)
const comMessageRef = ref()
const scanPopup = ref()
const checkNum = (e) => {
  const { value } = e.detail
  const dot = value.indexOf('.') // 包含小数点
  const reg = /^[0-9]+$/ // 正整数
  if (dot > -1) {
    maxlength.value = dot + 7 // 长度是小数点后两位
    if (value.length > dot + 7) {
    }
  }
  if (reg.test(value)) {
    // 如果是正整数不包含小数点
    maxlength.value = 10
  }
  change(value)
}
const openRequestPopup = (editPositionParams) => {
  if (positionList.value.length == 0) {
    getWorkShopLineStation()
      .then((res) => {
        if (res.data != null && res.data.length > 0) {
          positionList.value = res.data
        } else {
          showErrorMessage('未查找到位置信息')
        }
      })
      .catch((error) => {
        showErrorMessage(error)
      })
  }

  editPosition.value = editPositionParams
  if (isModifiedPosition.value) {
    isModifiedPosition.value = false
  } else {
    itemCode.value = '请扫描物料信息'
    show.value = false
    showWareHouse.value = false
    uom.value = ''
    qty.value = 0
    stdPackInfo.value = undefined
    counQty.value = undefined
    numberFocus.value = false
    itemCodeGetFocus()
  }

  setTimeout((res) => {
    isShow.value = true
  }, 500)
}
const initData = () => {
  positionList.value = []
  wareHouseList.value = []
  showWareHouse.value = false
  editPosition.value = true
  itemCode.value = '请扫描物料信息'
  rawLocationCode.value = ''
  positionInfo.value = '请选择位置'
  priority.value = ''
  fromWarehouseCode.value = '请选择仓库代码'
  workshopCode.value = '' // 车间
  workShopName.value = ''
  productionLineCode.value = '' // 生产线
  productionLineName.value = ''
  workStationCode.value = '' // 工位
  workStationName.value = ''
  show.value = false
  uom.value = ''
  qty.value = 0
  stdPackInfo.value = undefined
  counQty.value = undefined
  numberFocus.value = false
}

const closeRequestPopup = () => {
  isShow.value = false
}

const locationConfirm = () => {
  // 查询库位信息
  checkLocatioCode()
}
const itemCodeClick = () => {
  scanPopup.value.openScanPopup()
}
const itemCodeGetFocus = () => {
  itemCodeFocus.value = true
}
const itemCodeLoseFocus = () => {
  itemCodeFocus.value = false
}

const confirm = () => {
  if (priority.value === '') {
    showErrorMessage('请输入优先级', 'priority')
    return
  }
  if (positionInfo.value == '请选择位置') {
    showErrorMessage('请选择位置')
    return
  }

  if (fromWarehouseCode.value == '请选择仓库代码') {
    showErrorMessage('请选择仓库代码')
    return
  }

  if (itemCode.value == '请扫描物料信息') {
    showErrorMessage('请输入物料', 'itemCode')
    return
  }
  if (counQty.value == undefined) {
    showErrorMessage('请输入数量')
    return
  }

  if (qty.value == 0) {
    showErrorMessage('数量必须大于0')
    return
  }

  if (rawLocationCode.value == '') {
    showErrorMessage(`${workStationName.value}的原材料库位为空`)
    return
  }
  callback('add')
}

const callback = (action) => {
  const item = {
    positionInfo: positionInfo.value,
    workshopCode: workshopCode.value, // 车间
    workShopName: workShopName.value,
    productionLineCode: productionLineCode.value, // 生产线
    productionLineName: productionLineName.value,
    workStationCode: workStationCode.value, // 工位
    workStationName: workStationName.value,
    rawLocationCode: rawLocationCode.value,
    fgLocationCode: fgLocationCode.value,
    fromWarehouseCode: fromWarehouseCode.value,
    toWarehouseCode: fromWarehouseCode.value,
    priority: priority.value,
    itemCode: itemCode.value,
    itemName: itemName.value,
    uom: uom.value,
    qty: qty.value
  }
  console.log(777, item)
  closeRequestPopup()
  emit('confirm', action, item)
}

const showErrorMessage = (message, type) => {
  setTimeout((r) => {
    comMessageRef.value.showErrorMessage(message, (res) => {
      if (type == 'itemCode') {
        itemCodeGetFocus()
      } else {
        numberFocus.value = true
      }
    })
    if (type == 'itemCode') {
      itemCode.value = '请扫描物料信息'
    }
  })
}
const change = (value) => {
  qty.value = value
}
const cancel = (e) => {
  closeRequestPopup()
}
const showSelect = () => {
  if (editPosition.value) {
    show.value = true
  }
}

const showWarseHouseSelect = () => {
  if (editPosition.value) {
    wareHouseClick()
  }
}
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 getItemScanResult = (code, scanResult) => {
  itemCode.value = code
  itemName.value = scanResult.name
  uom.value = scanResult.uom
  stdPackInfo.value = scanResult.package
  numberFocus.value = true
}
const wareHouseClick = () => {
  proxy.$modal.loading('查询中....')
  getWarehouseCodeList(1, 100)
    .then((res) => {
      uni.hideLoading()
      if (res.data != null && res.data.list.length > 0) {
        res.data.list.forEach((item) => {
          item.value = item.code
          item.label = item.name
        })
        wareHouseList.value = res.data.list
        showWareHouse.value = true
      } else {
        showErrorMessage('未查找到仓库代码')
      }
    })
    .catch((error) => {
      uni.hideLoading()
      showErrorMessage(error)
    })
}

const confirmWareHouse = (e) => {
  fromWarehouseCode.value = e[0].value
}

// 传递给父类
const emit = defineEmits(['confirm'])
defineExpose({
  openRequestPopup
})
</script>

<style lang="scss">
.title {
  font-size: 30rpx;
}
</style>
