<template>
  <view class="" style="background-color: #fff">
    <u-collapse ref="collapse">
      <u-collapse-item :open="true">
        <template v-slot:title>
          <itemCompareQty :dataContent="dataContent" :handleQty="dataContent.handleQty" :isShowPackUnit="true" style="flex: 1"></itemCompareQty>
        </template>
        <package-list :dataContent="dataContent.subList" :isEdit="settingParam.allowModifyQty == 'TRUE'" :settingParam="settingParam" @collapseChange="collapseChange" @updateData="updateData"></package-list>
      </u-collapse-item>
    </u-collapse>
    <recommend-qty-edit ref="receiptEdit" :dataContent="editItem" :settingParam="settingParam" @confirm="confirm"> </recommend-qty-edit>
    <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 recommendQtyEdit from '@/mycomponents/qty/recommendQtyEdit.vue'
import packageList from '@/mycomponents/package/packageList.vue'

import { getDetailOption, getPurchaseReceiptOption } from '@/common/array.js'

const props = defineProps({
  dataContent: {
    type: Object,
    default: null
  },
  settingParam: {
    type: Object,
    default: null
  },
  locationAreaTypeList: {
    type: Object,
    default: null
  }
})
const showItem = ref({})
const editItem = ref({
  record: {}
})
const locatonItem = ref({})
const detailOptions = ref([])
const scanOptions = ref([])
const comMessageRef = ref()
const collapse = ref()
const receiptEdit = ref()
// 监视属性
watch(
  () => props.dataContent,
  (val) => {
    if (val.subList.length > 0) {
      if (collapse.value != undefined && collapse.value != null) {
        nextTick((res) => {
          collapse.value.init()
        })
      }
    }
  },
  {
    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)
  }
})

const collapseChange = () => {
  setTimeout(() => {
    resizeCollapse()
  }, 500)
}
const resizeCollapse = (object) => {
  nextTick(() => {
    collapse.value.init()
  })
}
const refreshCollapse = () => {
  nextTick((r) => {
    collapse.value.forEach((r) => {
      r.childrens.forEach((i) => {
        i.init()
      })
      r.init()
    })
  })
}
const swipeClick = (e, item) => {
  if (e.content.text == '编辑') {
    edit(item)
  } else if (e.content.text == '移除') {
    remove(item)
  }
}
const edit = (item) => {
  editItem = item
  receiptEdit.value.openTaskEditPopup(item.qty, item.handleQty, item.labelQty)
}
const remove = (item) => {
  comMessageRef.value.showQuestionMessage('确定移除扫描信息?', (res) => {
    if (res) {
      item.scaned = false
      item.handleQty = null
      emit('remove', item)
    }
  })
}
const confirm = (qty) => {
  editItem.value.handleQty = qty
  emit('updateData')
}
const updateData = () => {
  emit('updateData')
}
// 传递给父类
const emit = defineEmits(['remove', 'updateData'])
</script>

<style></style>
