<template>
  <view>
    <view class="u-p-l-20 u-p-b-20 u-p-r-20">
      <view v-for="(item, index) in dataContent.Items" :key="index" style="background: white" class="u-p-b-20 u-p-l-20 u-p-r-20">
        <u-collapse ref="collapse">
          <u-collapse-item :open="true">
            <template v-slot:title>
              <view class="" style="width: 90%">
                <u-swipe-action :show="item.show" :index="index" :options="removeOptions" bg-color="rgba(255,255,255,0)" @click="(...event) => itemCoceClick(event, item)">
                  <item-qty :dataContent="item" :handleQty="item.handleQty"></item-qty>
                </u-swipe-action>
              </view>
            </template>

            <view v-for="(loacation, index) in item.Locations" :key="index">
              <view>
                <view class="uni-flex uni-row space-between">
                  <!-- 推荐库位 -->
                  <location :locationCode="loacation.fromLocationCode"> </location>
                </view>
                <view v-for="(batch, index1) in loacation.Batchs" :key="index1">
                  <recommend-balance style="margin-left: 0rpx" :detail="batch" :isShowLocation="false" :isShowPack="batch.packingNumber != null && batch.packingNumber != ''"> </recommend-balance>
                  <view class="" v-if="batch.Records.length > 0">
                    <view class="center" style="width: 20px; background-color: #0cc2b6; color: #fff; padding: 0px 2px; font-size: 24rpx"> 扫描 </view>
                    <view class="uni-flex uni-column scan_view" style="flex: 1">
                      <u-swipe-action :show="record.show" :index="key" v-for="(record, key) in batch.Records" :key="key" :options="scanOptions" bg-color="rgba(255,255,255,0)" @click="(...event) => swipeClick(event, batch, record)" style="width: 100%">
                        <handle-balance :detail="record" :isShowLocation="false" :isShowBatch="batch.packingNumber != null"> </handle-balance>
                      </u-swipe-action>
                    </view>
                  </view>
                </view>
              </view>
            </view>
          </u-collapse-item>
        </u-collapse>
      </view>
    </view>
    <balance-qty-edit ref="balanceQtyEditRef" @confirm="confirm" :isShowStatus="true"></balance-qty-edit>
    <detail-info-popup ref="detailInfoPopup"></detail-info-popup>
    <com-message ref="comMessageRef" />
  </view>
</template>

<script setup lang="ts">
import { ref, getCurrentInstance, onMounted, nextTick } from 'vue'
import { onLoad, onNavigationBarButtonTap, onPullDownRefresh } from '@dcloudio/uni-app'
import itemQty from '@/mycomponents/item/itemQty.vue'
import recommend from '@/mycomponents/recommend/recommend.vue'
import recommendBalance from '@/mycomponents/balance/recommendBalance.vue'
import handleBalance from '@/mycomponents/balance/handleBalance.vue'
import recommendQtyEdit from '@/mycomponents/qty/recommendQtyEdit.vue'
import jobDetailPopup from '@/mycomponents/job/jobDetailPopup.vue'
import requiredLocation from '@/mycomponents/location/requiredLocation.vue'
import balanceQtyEdit from '@/mycomponents/qty/balanceQtyEdit.vue'
import location from '@/mycomponents/balance/location.vue'
import detailInfoPopup from '@/pages/productionReceipt/coms/detailInfoPopup.vue'
import comIssueRequestInfo from '@/pages/issue/coms/comIssueRequestInfo.vue'
import { getDetailOption, getEditRemoveOption, getRemoveOption } from '@/common/array.js'

const props = defineProps({
  dataContent: {
    type: Object,
    default: {}
  },
  settingParam: {
    type: Object,
    default: {}
  }
})
const option = ref([])
const showItem = ref({})
const editItem = ref({})
const batchItem = ref({})
const detailOptions = ref([])
const scanOptions = ref([])
const removeOptions = ref([])
const collapse = ref()
const comMessageRef = ref()
const dataContent = ref(props.dataContent)
const balanceQtyEditRef = ref()
dataContent.value.Items.forEach((item) => {
  item.Locations.forEach((cur) => {
    cur.Batchs.forEach((ele) => {
      ele.Records.forEach((ele1) => {
        ele1.show = false
      })
    })
  })
})
onMounted(() => {
  detailOptions.value = getDetailOption()
  scanOptions.value = getEditRemoveOption()
  removeOptions.value = getRemoveOption()
})
const resizeCollapse = () => {
  nextTick((r) => {
    collapse.value.forEach((r) => {
      r.childrens.forEach((i) => {
        i.init()
      })
      r.init()
    })
  })
}
const swipeClick = (params, batch, record) => {
  const { text } = scanOptions.value[params[1]]
  if (text == '编辑') {
    edit(batch, record)
  } else if (text == '移除') {
    remove(batch, record, params[0])
  }
}
const itemCoceClick = (params, item) => {
  const { text } = removeOptions.value[params[1]]
  if (text == '移除') {
    dataContent.value.Items.splice(params[0], 1)
    emit('removeItemCode')
  }
}
const edit = (batch, item) => {
  editItem.value = item
  batchItem.value = batch
  item.balance.balanceQty = item.balance.qty
  balanceQtyEditRef.value.openEditPopup(item.balance, item.qty)
}

const remove = (batch, record, index) => {
  comMessageRef.value.showQuestionMessage('确定移除扫描信息?', (res) => {
    if (res) {
      batch.Records.splice(index, 1)
      resizeCollapse()
      emit('updateData', record)
    }
  })
}
const confirm = (val) => {
  editItem.value.qty = val
  emit('updateData', editItem.value)
}
// 传递给父类
const emit = defineEmits(['updateData', 'removeItemCode'])
defineExpose({
  resizeCollapse
})
</script>

<style></style>
