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

<script setup lang="ts">
import { ref, onMounted, nextTick, watch } from 'vue'
import itemQty from '@/mycomponents/item/itemQty.vue'
import recommend from '@/mycomponents/recommend/recommend.vue'
import balanceQtyEdit from '@/mycomponents/qty/balanceQtyEdit.vue'
import detailInfoPopup from '@/pages/inventoryMove/coms/detailInfoPopup.vue'
import winScanLocation from '@/mycomponents/scan/winScanLocation.vue'

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

const props = defineProps({
  dataContent: {
    type: Object,
    default: {}
  },
  settingParam: {
    type: Object,
    default: {}
  }
})
const option = ref([])
const title = ref('推荐详情')
const showItem = ref({})
const editItem = ref({})
const detailOptions = ref([])
const scanOptions = ref([])
const removeOptions = ref([])
const dataList = ref([])
const comMessageRef = ref('')
const detailInfoPopupRef = ref('')
const qtyEdit = ref('')
const scanLocationCode = ref('')
const winHint = ref()
const dataContent = ref(props.dataContent)
const collapse1 = ref()

onMounted(() => {
  if (detailOptions.value.length == 0) {
    detailOptions.value = getDetailOption()
  }
  if (scanOptions.value.length == 0) {
    scanOptions.value = getPurchaseReceiptOption(props.settingParam.allowModifyQty, props.settingParam.allowModifyLocation)
  }
  nextTick(() => {
    collapse1.value.init()
  })
})
const openDetailCardPopup = () => {
  winHint.value.openScanPopup()
}
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, index) => {
  comMessageRef.value.showQuestionMessage('确定移除扫描信息', (res) => {
    if (res) {
      dataContent.value.subList.splice(index, 1)
      emit('removePack')
    }
  })
}

const confirm = (qty) => {
  editItem.value.handleQty = qty
  emit('updateData')
}

const showLocation = (item) => {
  locatonItem.value = item
  scanLocationCode.value.openScanPopup()
}
// 扫描源库位
const getLocation = (location, code) => {
  locatonItem.value.toLocationCode = code
  emit('updateData')
}

// 传递给父类
const emit = defineEmits(['updateData', 'removePack', 'openDetail'])
</script>

<style></style>
