<template>
	<view style="background-color: #fff;">
    <u-collapse ref="collapse1Ref" @change="">
      <u-collapse-item :open="true">
        <template #title>
          <item-compare-qty :dataContent="dataContent"
                            :handleQty="dataContent.handleQty" :isShowStdPack="false">
          </item-compare-qty>
        </template>
        <view class="" v-for="(item,index) in dataContent.subList" :key="index">
          <u-swipe-action ref="swipeAction"
                          :options="item.scaned?scanOptions:detailOptions"
                          @click="(...event)=>swipeClick(event,item,'parent')"
                          v-if='index==0'>
            <recommend :detail="item" :isShowFromLocation="isShowFromLocation"
                       :isShowStatus="isShowStatus" :isShowToLocation="isShowToLocation">
            </recommend>
          </u-swipe-action>
        </view>
      </u-collapse-item>
    </u-collapse>


		<balance-qty-edit ref="qtyEdit" :settingParam="settingParam" :queryBalance="queryBalance"
			@confirm="confirm"></balance-qty-edit>
		<win-scan-location ref="scanLocationCode" title="目标库位" @getLocation='getLocation'
			:locationAreaTypeList="locationAreaTypeList"></win-scan-location>
		<comMessage ref="message"></comMessage>
	</view>
</template>

<script setup>
	import config from '@/static/config.js'
	import ItemCompareQty from '@/mycomponents/item/itemCompareQty.vue'
	import Recommend from '@/mycomponents/recommend/recommend.vue'
	import BalanceQtyEdit from '@/mycomponents/qty/balanceQtyEdit.vue'
	import WinScanLocation from "@/mycomponents/scan/winScanLocation.vue"

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


import { ref, watch, onMounted, nextTick } from 'vue';

const props = defineProps({
  dataContent: {
    type: Object,
    default: null
  },
  settingParam: {
    type: Object,
    default: null
  },
  isShowPack: {
    type: Boolean,
    default: true
  },
  isShowBatch: {
    type: Boolean,
    default: true
  },
  isShowLocation: {
    type: Boolean,
    default: true
  },
  isShowFromLocation: {
    type: Boolean,
    default: true
  },
  isShowToLocation: {
    type: Boolean,
    default: false
  },
  locationAreaTypeList: {
    type: Array,
    default: null
  },
  queryBalance: {
    type: Boolean,
    default: true
  },
  isShowStatus: {
    type: Boolean,
    default: true
  },
});
const emit = defineEmits(['openDetail', 'remove', 'updateData']);

const showItem = ref({});
const locatonItem = ref({});
const editItem = ref({});
const detailOptions = ref([]);
const scanOptions = ref([]);
const options = ref([]);

const collapse1Ref = ref(null);
const qtyEditRef = ref(null);
const messageRef = ref(null);
const scanLocationCodeRef = ref(null);

watch(() => props.dataContent, (newDataContent, oldDataContent) => {
  if (newDataContent?.subList?.length > 0) {
    nextTick(() => {
      setTimeout(() => {
        if (collapse1Ref.value) {
          collapse1Ref.value.resize();
        }
      }, 500);
    });
  }
}, { immediate: true, deep: true });

onMounted(() => {
  if (detailOptions.value.length === 0) {
    detailOptions.value = getDetailOption();
  }
  if (scanOptions.value.length === 0) {
    scanOptions.value = getPurchaseReceiptOption(props.settingParam.allowModifyQty, false);
  }
});

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

const edit = (item) => {
  editItem.value = item;
  qtyEditRef.value.openEditPopup(item.balance, item.handleQty);
};

const detail = (item) => {
  emit('openDetail', item);
};

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

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

const showLocation = (item) => {
  locatonItem.value = item;
  scanLocationCodeRef.value.openScanPopup();
};

const getLocation = (location, code) => {
  locatonItem.value.toLocationCode = code;
  emit('updateData');
};
</script>

<style>
</style>