<template>
	<view class="" style="background-color: #fff;">
		<u-collapse ref="collapse1" >
			<u-collapse-item :open="true">
				<template v-slot: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"
                            style='padding:0px 0px  5px 0px;align-items: center;'
                            @click="(...event)=>swipeClick(event,item)">
            <view  v-if="item.isRecommend" class="uni-flex" style="flex-direction: row; align-items: center;background-color: antiquewhite;">
              <view class="" style="font-size: 32rpx; color: black; font-weight: bold; text-align: center;">
                推
                荐
              </view>
              <recommend  :detail="item" :isShowStatus="isShowStatus" :isShowToLocation="false">
              </recommend>
            </view>
            <view v-else class="uni-flex" style="flex-direction: row; align-items: center;background-color: antiquewhite; margin-top: 5rpx;">
              <view class="" style="font-size: 32rpx; color: red; font-weight: bold; text-align: center;">
                实
                际
              </view>
              <recommend  :detail="item" :isShowStatus="isShowStatus" :isShowToLocation="false">
              </recommend>
            </view>
					</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 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
  },
  locationAreaTypeList: {
    type: Array,
    default: null
  },
  queryBalance: {
    type: Boolean,
    default: true
  },
  isShowStatus: {
    type: Boolean,
    default: true
  }
});
const emit = defineEmits(['openDetail', 'remove', 'updateData']);

const collapse1 = ref(null);
const qtyEdit = ref(null);
const message = ref(null);
const scanLocationCode = ref(null);

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

watch(() => props.dataContent, (newDataContent, oldDataContent) => {
  if (newDataContent?.subList.length > 0) {
    nextTick(() => {
      setTimeout(() => {
        if (collapse1.value) {
          collapse1.value.init();
        }
      }, 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);
  }
  // showLocation();
});

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

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

const detail = (item) => {
  emit('openDetail', item);
  // showItem.value = item;
  // jobDetailPopup.value.openPopup(item);
};

const remove = (item) => {
  message.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;
  scanLocationCode.value.openScanPopup();
};

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

</script>

<style>
</style>