<template>
	<view style="background-color: #fff;">
		<item-compare-qty :dataContent="dataContent" :handleQty="dataContent.handleQty" :isShowStdPack="false">
		</item-compare-qty>
		<view class="" v-for="(item,index) in dataContent.subList" :key="index" :class="item.scaned?'scan_view':''">
			<u-swipe-action ref="swipeAction"
                      :options="(item.scaned&&isEdit)?editAndRemoveOptions : item.scaned? removeOptions:options"
                      @click="(...event)=>swipeClick(event,item,'parent')"
                      v-if='index==0'>
        <div style="display: flex;">
          <div style="flex: 1;">
            <batch v-if="item.batch" :batch="item.batch"></batch>
            <location title="来源库位" v-if="item.fromLocationCode" :locationCode="item.fromLocationCode"></location>
          </div>
          <text style="font-size: 30rpx;color: #2979ff; " @click="copy(item)" v-if="isDevlement()">复制</text>
        </div>
			</u-swipe-action>
		</view>
		<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 location from '@/mycomponents/balance/location.vue'
	import toLocation from '@/mycomponents/balance/toLocation.vue'
	import batch from '@/mycomponents/balance/batch.vue'

	import {
		getDetailOption,
		getPurchaseReceiptOption,
		getRemoveOption,
		getEditRemoveOption
	} 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
  },
  locationAreaTypeList: {
    type: Array,
    default: null
  },
  queryBalance: {
    type: Boolean,
    default: true
  },
  isShowStatus: {
    type: Boolean,
    default: true
  },
  isEdit: {
    type: Boolean,
    default: true
  }
});
const emit = defineEmits(['openDetail', 'remove', 'updateData']);

const option = ref([]);
const showItem = ref({});
const locatonItem = ref({});
const editItem = ref({});
const detailOptions = ref([]);
const scanOptions = ref([]);
const options = ref([]);
const removeOptions = ref([]);
const editAndRemoveOptions = 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);
  }
  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;
  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');
};

const isDevlement = () => {
  return config.isDevelopment;
};

const copy = (detail) => {
  console.log(detail);
  const content = `HPQ;V1.0;I${detail.itemCode};P${detail.packingNumber};B${detail.batch};Q${detail.qty}`;
  // #ifdef H5
  uni.copyText(content).then(() => {
    uni.showToast({
      title: '复制采购标签成功',
      icon: 'none'
    });
  });
  // #endif
  // #ifndef H5
  uni.setClipboardData({
    data: content,
    success: () => {
      uni.showToast({
        title: '复制采购标签成功'
      });
    }
  });
  // #endif
};

</script>

<style>
</style>