<template>
  <view class="" style="background-color: #fff">
    <u-collapse ref="collapse1">
      <u-collapse-item :open="true">
        <template v-slot:title>
          <u-swipe-action :show="false" style="width: 90%" :options="removeOptions" bg-color="rgba(255,255,255,0)" @click="(...event) => removeItem(event, dataContent)">
            <item-qty :dataContent="dataContent" :isShowBalance="true" :isShowBalanceQty="isShowBalanceQty"></item-qty>
          </u-swipe-action>
        </template>
        <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)">
          <balance :dataContent="detail" :isShowStdPack="false" :isShowStatus="true" :isShowPack="true" :isShowFromLocation="isShowFromLocation" :isShowToLocation="isShowToLocation"> </balance>
        </u-swipe-action>
      </u-collapse-item>
    </u-collapse>
    <balance-qty-edit ref="balanceQtyEditRef" @confirm="confirm"></balance-qty-edit>
    <record-detail-popup ref="recordDetailPopupRef"></record-detail-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 jobDetailPopup from '@/mycomponents/job/jobDetailPopup.vue'
import balance from '@/mycomponents/balance/balance.vue'
import balanceQtyEdit from '@/mycomponents/qty/balanceQtyEdit.vue'
import recordDetailPopup from '@/mycomponents/detail/recordDetailPopup.vue'

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

const props = defineProps({
  dataContent: {
    type: Object,
    default: {}
  },
  settingParam: {
    type: Object,
    default: {}
  },

  isShowStatus: {
    type: Boolean,
    default: false
  },

  isShowFromLocation: {
    type: Boolean,
    default: true
  },

  isShowToLocation: {
    type: Boolean,
    default: true
  },

  isShowBalanceQty: {
    type: Boolean,
    default: false
  }
})
const collapse1 = ref()
// 监视属性
watch(
  () => props.dataContent,
  (val) => {
    if (val.subList.length > 0) {
      if (collapse1.value != undefined && collapse1.value != null) {
        nextTick((res) => {
          collapse1.value.init()
        })
      }
    }
  },
  { immediate: true, deep: true }
)
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 balanceQtyEditRef = ref('')
const recordDetailPopupRef = ref('')
const dataContent = ref(props.dataContent)
dataContent.value.subList.forEach((item) => {
  item.show = false
})
onMounted(() => {
  detailOptions.value = getDetailOption()
  scanOptions.value = getDetailEditRemoveOption()
  removeOptions.value = getClearOption()
})
const removeItem = (params, dataContent) => {
  const { text } = removeOptions.value[params[1]]
  if (text == '清空') {
    comMessageRef.value.showQuestionMessage('确定清空物料及箱码信息?', (res) => {
      if (res) {
        emit('removeItem')
      }
    })
  }
}
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 == '移除') {
    remove(item, params[0])
  }
}
const edit = (item) => {
  editItem.value = item
  balanceQtyEditRef.value.openEditPopup(editItem.value, editItem.value.handleQty)
}
const detail = (item) => {
  showItem.value = item
  recordDetailPopupRef.value.openPopup(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 emit = defineEmits(['updateData', 'removePack'])
</script>

<style>
::v-deep .u-arrow-down-icon {
  margin-right: 0px !important;
}
</style>
