<template>
  <view class="" style="background-color: #fff;">
    <u-collapse ref="collapse1">
      <u-collapse-item :open="true">
        <template v-slot:title>
          <u-swipe-action ref="swipeAction" :options="removeOptions" @click="removeData($event,dataContent)">
            <item-qty :dataContent="dataContent"
                      :isShowBalanceQty="false"
                      :isShowBalance="true"></item-qty>
          </u-swipe-action>
        </template>

        <view class="" v-for="(item,index) in dataContent.subList">
          <u-swipe-action ref="swipeAction" :options="item.scaned?scanOptions:detailOptions"
                          @click="swipeClick($event,item,index)">
            <comMovebalance :dataContent="item" :isShowStdPack="false" :isShowPack="true"
                            :fromInventoryStatus="item.inventoryStatus" :toInventoryStatus="item.toInventoryStatus"
                            :isShowLocation="true" :allowEditStatus='allowEditStatus'></comMovebalance>
          </u-swipe-action>
          <view class='split_line'></view>
        </view>
      </u-collapse-item>
    </u-collapse>
    <balanceQtyEdit ref="balanceQtyEdit" @confirm="confirm" :isShowStatus="isShowStatus"
                    :allowEditStatus="allowEditStatus"
                    :allowEditQty="allowEditQty"
    >
    </balanceQtyEdit>
    <job-detail-popup ref="winHint" :dataContent="showItem"></job-detail-popup>
    <comMessage ref="comMessageRef"></comMessage>
  </view>
</template>

<script setup lang="ts">
import itemQty from '@/mycomponents/item/itemQty.vue'
import recommend from '@/mycomponents/recommend/recommend.vue'
import jobDetailPopup from '@/mycomponents/job/jobDetailPopup.vue'
import balance from '@/mycomponents/balance/balance.vue'
import balanceMove from '@/mycomponents/balance/balanceMove.vue'
import comMovebalance from '@/pages/inventoryMove/coms/comMovebalance.vue'
import balanceQtyEdit from '@/mycomponents/qty/balanceQtyEdit.vue'

import {
  getDetailOption,
  getDetailRemoveOption,
  getRemoveOption
} from '@/common/array.js';
import {ref, watch, onMounted, nextTick} from 'vue';

// 定义 props
const props = defineProps({
  dataContent: {
    type: Object,
    default: () => ({})
  },
  settingParam: {
    type: Object,
    default: () => ({})
  },
  fromInventoryStatus: {
    type: String,
    default: ""
  },
  toInventoryStatus: {
    type: String,
    default: ""
  },
  isShowStatus: {
    type: Boolean,
    default: true
  },
  allowEditStatus: {
    type: Boolean,
    default: false
  },
  allowEditQty: {
    type: Boolean,
    default: false
  }
});

// 定义 emits
const emit = defineEmits(['removeData', 'removePack', 'updateData']);

// 响应式数据
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 collapse1 = ref(null);
const comMessageRef = ref(null);
const balanceQtyEdit = ref(null);
const winHint = ref(null);

// 监听 dataContent 变化
watch(() => props.dataContent, (newVal, oldVal) => {
  if (newVal?.subList?.length > 0) {
    nextTick(() => {
      setTimeout(() => {
        if (collapse1.value) {
          collapse1.value.init();
        }
      }, 500);
    });
  }
}, {immediate: true, deep: true});

// 生命周期钩子
onMounted(() => {
  detailOptions.value = getDetailOption();
  scanOptions.value = getDetailRemoveOption();
  removeOptions.value = getRemoveOption();
});

// 方法
const removeData = (e, dataContent) => {
  if (e.content.text === "移除") {
    comMessageRef.value.showQuestionMessage("确定移除扫描信息?", res => {
      if (res) {
        emit('removeData', dataContent);
      }
    });
  }
};

const swipeClick = (e, item, index) => {
  if (e.content.text === "详情") {
    detail(item);
  } else if (e.content.text === "编辑") {
    edit(item);
  } else if (e.content.text === "移除") {
    remove(item, index);
  }
};

const edit = (item) => {
  editItem.value = item;
  balanceQtyEdit.value.openEditPopup(item, item.qty);
};

const detail = (item) => {
  showItem.value = item;
  dataList.value = [
    {title: "箱码", content: item.packingNumber},
    {title: "批次", content: item.batch},
    {title: "库位", content: item.locationCode},
    {title: "数量", content: item.qty},
    {title: "单位", content: item.uom}
  ];
  winHint.value.openScanPopup(showItem.value);
};

const remove = (item, index) => {
  comMessageRef.value.showQuestionMessage("确定移除扫描信息?", res => {
    if (res) {
      item.scaned = false;
      item.balance = {};
      item.handleQty = null;
      emit('removePack', item);
    }
  });
};

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

</script>

<style>
</style>
