<template>
	<view class="" style="background-color: #fff;">
		<!-- <item-qty :dataContent="dataContent" :handleQty="dataContent.handleQty" :showBalanceQty="false">
		</item-qty> -->
		<item-compare-qty :dataContent="dataContent" :handleQty="dataContent.handleQty" :isShowStdPack="false">
		</item-compare-qty>
		<view class="" v-for="(item,index) in dataContent.subList">
			<u-swipe-action ref="swipeAction" v-if="index == 0"
                      :options="(item.scaned&&isEdit)?editAndRemoveOptions : item.scaned? removeOptions:options"
                      :class="item.scaned?'scan_view':''"
                      @click="(...event)=>swipeClick(event,item)">
        <view style="display: flex;">
          <view style="flex: 1;">
            <batch v-if="item.batch" :batch="item.batch"></batch>
            <location v-if="item.fromLocationCode" title="来源库位" :locationCode="item.fromLocationCode">
            </location>
            <to-location v-if="item.toLocationCode" title="目标库位" :locationCode="item.toLocationCode">
            </to-location>
          </view>
          <text style="font-size: 30rpx;color: #2979ff; " @click="copy(item)" v-if="isDevlement()">复制</text>
        </view>
			</u-swipe-action>
		</view>
		<recommend-qty-edit ref="receiptEditRef" :dataContent="editItem" :settingParam="settingParam" @confirm="confirm">
		</recommend-qty-edit>
		<win-scan-location ref="scanLocationCodeRef" title="目标库位" @getLocation='getLocation'
			:locationAreaTypeList="locationAreaTypeList"></win-scan-location>
		<detail-info-popup ref="detailInfoPopupRef"></detail-info-popup>
		<comMessage ref="messageRef"></comMessage>
	</view>
</template>

<script setup lang="ts">
	import itemQty from '@/mycomponents/item/itemQty.vue'
	import recommend from '@/mycomponents/recommend/recommend.vue'
	import recommendQtyEdit from '@/mycomponents/qty/recommendQtyEdit.vue'
	import jobDetailPopup from '@/mycomponents/job/jobDetailPopup.vue'
	import detailInfoPopup from '@/pages/unPlanned/coms/detailInfoPopup.vue'
	import winScanLocation from "@/mycomponents/scan/winScanLocation.vue"

	import location from '@/mycomponents/balance/location.vue'
	import toLocation from '@/mycomponents/balance/toLocation.vue'
	import batch from '@/mycomponents/balance/batch.vue'
	import itemCompareQty from '@/mycomponents/item/itemCompareQty.vue'
	import config from '@/static/config.js'

	import {
		getRemoveOption,
		getEditRemoveOption
	} from '@/common/array.js';


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

const props = defineProps({
  dataContent: {
    type: Object,
    default: () => ({})
  },
  settingParam: {
    type: Object,
    default: () => ({})
  },
  isShowLocation: {
    type: Boolean,
    default: false
  },
  locationAreaTypeList: {
    type: Object,
    default: null
  },
  isEdit: {
    type: Boolean,
    default: true
  }
});

const emit = defineEmits(['updateData', 'remove']);

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

const collapse1Ref = ref(null);
const receiptEditRef = ref(null);
const scanLocationCodeRef = ref(null);
const detailInfoPopupRef = ref(null);
const messageRef = ref(null);

watch(() => props.dataContent, (newDataContent) => {
  if (newDataContent.subList.length > 0) {
    setTimeout(() => {
      if (collapse1Ref.value) {
        collapse1Ref.value.resize();
      }
    }, 500);
  }
}, { 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;
  receiptEditRef.value.openTaskEditPopup(item.qty, item.handleQty, item.labelQty);
};

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

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

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

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

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

const isDevlement = () => {
  return 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
};
</script>

<style>
</style>