<template>
  <view class="" style="background-color: #fff">
    <u-collapse ref="collapse">
      <u-collapse-item :open="true">
        <template v-slot:title>
          <item-compare-qty :dataContent="dataContent" :handleQty="dataContent.handleQty" :isShowStdPack="false" style="width: 100%"> </item-compare-qty>
        </template>
        <u-swipe-action :show="item.show" :index="index" v-for="(item, index) in dataContent.subList" :key="index" :options="item.scaned ? scanOptions : detailOptions" bg-color="rgba(255,255,255,0)" @click="(...event) => swipeClick(event, item)">
          <recommend :detail="item" :isShowToLocation="false"> </recommend>
        </u-swipe-action>
      </u-collapse-item>
    </u-collapse>
    <balance-qty-edit ref="qtyEdit" :settingParam="settingParam" @confirm="confirm"></balance-qty-edit>
    <win-scan-location ref="scanLocationCode" title="目标库位" @getLocation="getLocation" :locationAreaTypeList="locationAreaTypeList"></win-scan-location>
    <com-message ref="comMessageRef" />
  </view>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, onMounted, nextTick, watch } from 'vue'
import itemCompareQty from '@/mycomponents/item/itemCompareQty.vue'
import recommend from '@/mycomponents/recommend/recommend.vue'
import balanceQtyEdit from '@/mycomponents/qty/balanceQtyEdit.vue'
import winScanLocation from '@/mycomponents/scan/winScanLocation.vue'
import { getDetailOption, getPurchaseReceiptOption } from '@/common/array.js'

const props = defineProps({
  dataContent: {
    type: Object,
    default: null
  },
  settingParam: {
    type: Object,
    default: null
  },
  isShowPack: {
    type: Boolean,
    default: true
  },
  isShowBatch: {
    type: Boolean,
    default: true
  },
  isShowLocation: {
    type: Boolean,
    default: true
  },
  locationAreaTypeList: {
    type: Array,
    default: null
  }
})
const option = ref([])
const showItem = ref({})
const editItem = ref({})
const locatonItem = ref({})
const detailOptions = ref([])
const scanOptions = ref([])
const comMessageRef = ref()
const collapse = ref()
const qtyEdit = ref()
const dataContent = ref(props.dataContent)
dataContent.value.subList.forEach((item) => {
  item.show = false
})
// 监视属性
watch(
  () => props.dataContent,
  (val) => {
    if (val.subList.length > 0) {
      if (collapse.value != undefined && collapse.value != null) {
        nextTick((res) => {
          collapse.value.init()
        })
      }
    }
  },
  {
    immediate: true,
    deep: true
  }
)
onMounted(() => {
  if (detailOptions.value.length == 0) {
    detailOptions.value = getDetailOption()
  }
  if (scanOptions.value.length == 0) {
    scanOptions.value = getPurchaseReceiptOption(props.settingParam.allowModifyQty, props.settingParam.allowModifyLocation)
  }
  nextTick((res) => {
    collapse.value.init()
  })
})
const swipeClick = (params, item) => {
  let text = ''
  if (item.scaned) {
    text = scanOptions.value[params[1]].text
  } else {
    text = detailOptions.value[params[1]].text
  }
  if (text == '详情') {
    detail(item)
  } else if (text == '编辑') {
    edit(item)
  } else if (text == '库位') {
    showLocation(item)
  } else if (text == '移除') {
    remove(item)
  }
}
const edit = (item) => {
  editItem.value = item
  qtyEdit.value.openEditPopup(item.balance, item.handleQty)
}
const detail = (item) => {
  emit('openDetail', item)
}
const remove = (item) => {
  comMessageRef.value.showQuestionMessage('确定移除扫描信息?', (res) => {
    if (res) {
      item.scaned = false
      item.balance = {}
      item.handleQty = null
      emit('remove', item)
    }
  })
}
const confirm = (qty) => {
  editItem.value.handleQty = qty
  emit('updateData')
}
const showLocation = () => {
  locatonItem.value = item
  scanLocationCode.value.openScanPopup()
}
// 扫描源库位
const getLocation = () => {
  locatonItem.value.LocationCode = code
  emit('updateData')
}
// 传递给父类
const emit = defineEmits(['openDetail', 'remove', 'updateData'])
</script>

<style></style>
