<template>
	<view class="" style="background-color: #fff;">
					<itemCompareQty :dataContent="dataContent" :handleQty="dataContent.handleQty"></itemCompareQty>
		<view class="" v-for="(item,index) in dataContent.subList" :key="index" >
			<u-swipe-action ref="swipeAction" v-if="index==0"
                        :options="(item.scaned&&isEdit)?editAndRemoveOptions : item.scaned? removeOptions:options"
                        @click="(...event)=>swipeClick(event,item,'parent')"
                        :class="item.scaned?'scan_view':''">
        <recommendBatch :detail="item" :isShowFromLocation="false"
                        :isShowToLocation="settingParam.allowModifyLocation=='TRUE'"></recommendBatch>
			</u-swipe-action>
			<view v-if="item.fgList!=undefined && item.fgList.length>0" class="card_view " style="margin-left: 20px;"
				v-for="(fg, index) in item.fgList" :key="index">
				<com-fg-card :detail='fg' class="scan_view"></com-fg-card>
			</view>
		</view>

		<recommend-qty-edit ref="receiptEdit" :dataContent="editItem" :settingParam="settingParam" @confirm="confirm">
		</recommend-qty-edit>
		<win-scan-location ref="scanLocationCode" title="目标库位" @getLocation='getLocation'
			:locationAreaTypeList="locationAreaTypeList"></win-scan-location>
		<productDetailInfoPopup ref="jobDetailPopup" :dataContent="showItem"></productDetailInfoPopup>
		<comMessage ref="message"></comMessage>
	</view>
</template>

<script setup lang="ts">
	import itemCompareQty from '@/mycomponents/item/itemCompareQty.vue'
	import recommendBatch from '@/mycomponents/recommend/recommendBatch.vue'
	import recommendQtyEdit from '@/mycomponents/qty/recommendQtyEdit.vue'
	import jobDetailPopup from '@/mycomponents/detail/jobDetailPopup.vue'
	import productDetailInfoPopup from '@/pages/productReceipt/coms/productDetailInfoPopup.vue'
	import winScanLocation from "@/mycomponents/scan/winScanLocation.vue"
	import comFgCard from "@/pages/productReceipt/coms/comFgCard.vue"

	import pack from '@/mycomponents/balance/pack.vue'
	import location from '@/mycomponents/balance/location.vue'
	import toLocation from '@/mycomponents/balance/toLocation.vue'
	import batch from '@/mycomponents/balance/batch.vue'
	import config from '@/static/config.js'
	import {
		getRemoveOption,
		getEditRemoveOption
	} from '@/common/array.js';


import { ref, watch, onMounted, computed,nextTick } from 'vue';
import { useCountStore } from '@/store'
  const store = useCountStore()
const props = defineProps({
  dataContent: {
    type: Object,
    default: null
  },
  settingParam: {
    type: Object,
    default: null
  },
  locationAreaTypeList: {
    type: Array,
    default: null
  },
  isEdit: {
    type: Boolean,
    default: true
  }
});

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

const collapse1 = ref(null);
const receiptEdit = ref(null);
const scanLocationCode = ref(null);
const jobDetailPopup = ref(null);
const message = ref(null);

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

onMounted(() => {
  removeOptions.value = getRemoveOption();
  editAndRemoveOptions.value = getEditRemoveOption();
});

// 定义方法
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;
  receiptEdit.value.openTaskEditPopup(item.qty, item.handleQty, item.labelQty);
};

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

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

const detail = (item) => {
  showItem.value = item;
  jobDetailPopup.value.openPopup(item);
};

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

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

const isDevlement = computed(() => config.isDevelopment);

const copy = (detail) => {
  const content = `HPQ;V1.0;I${detail.itemCode};P${detail.packingNumber};B${detail.batch};Q${detail.qty}`;

  // #ifdef H5
  navigator.clipboard.writeText(content).then(() => {
    uni.showToast({
      title: '复制采购标签成功',
      icon: 'none'
    });
  });
  // #endif

  // #ifndef H5
  uni.setClipboardData({
    data: content,
    success: () => {
      uni.showToast({
        title: '复制采购标签成功'
      });
    }
  });
  // #endif
};

const emit = defineEmits(['updateData', 'remove']);
</script>

<style>
</style>