<template>
  <view class="" style="background-color: #fff;">
    <u-collapse ref="collapse">
      <u-collapse-item :open="true">
        <template v-slot:title>
          <u-swipe-action @click="(...event)=>swipeClick(event,dataContent)"
                          options="dataContent.handleQty>0? removeOptions:options"
                          :class="dataContent.scaned?'scan_view':''">
            <itemCompareQty :dataContent="dataContent" :handleQty="dataContent.handleQty"
                            :isShowPackUnit="false"></itemCompareQty>
            <batch v-if="dataContent.subList[0].batch" :batch="dataContent.subList[0].batch"></batch>
            <location title="来源库位" v-if="dataContent.subList[0].fromLocationCode"
                      :locationCode="dataContent.subList[0].fromLocationCode">
            </location>
          </u-swipe-action>
        </template>
      </u-collapse-item>
    </u-collapse>

    <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>
    <receipt-detail-info-popup ref="jobDetailPopup" :dataContent="showItem"></receipt-detail-info-popup>
    <comMessage ref="message"></comMessage>
  </view>
</template>

<script setup lang="ts">
import itemCompareQty from '@/mycomponents/item/itemCompareQty.vue'
import recommend from '@/mycomponents/recommend/recommend.vue'
import recommendQtyEdit from '@/mycomponents/qty/recommendQtyEdit.vue'
import jobDetailPopup from '@/mycomponents/detail/jobDetailPopup.vue'
import receiptDetailInfoPopup from '@/pages/purchaseReceipt/coms/receiptDetailInfoPopup.vue'
import winScanLocation from "@/mycomponents/scan/winScanLocation.vue"
import detailList from '@/mycomponents/detail/detailList.vue'
import packageList from '@/mycomponents/package/packageList.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 {
  getDetailOption,
  getPurchaseReceiptOption,
  getRemoveOption
} from '@/common/array.js';


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

const props = defineProps({
  dataContent: {
    type: Object,
    default: null
  },
  settingParam: {
    type: Object,
    default: null
  },
  locationAreaTypeList: {
    type: Object,
    default: null
  }
});

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

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

onMounted(() => {
  if (detailOptions.value.length === 0) {
    detailOptions.value = getDetailOption();
  }

  if (scanOptions.value.length === 0) {
    scanOptions.value = getPurchaseReceiptOption(props.settingParam.allowModifyQty, props.settingParam.allowModifyLocation);
  }

  if (removeOptions.value.length === 0) {
    removeOptions.value = getRemoveOption();
  }
});

const collapseChange = () => {
  setTimeout(() => {
    resizeCollapse();
  }, 500);
};

const resizeCollapse = () => {
  nextTick(() => {
    if (collapse.value) {
      collapse.value.init();
    }
  });
};

const refreshCollapse = () => {
  nextTick(() => {
    if (collapse.value) {
      collapse.value.forEach(r => {
        r.childrens.forEach(i => {
          i.init();
        });
        r.resize();
      });
    }
  });
};

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 === "移除") {
    removeScan(item);
  }
};

const edit = (item) => {
  editItem.value = item;
  if (receiptEdit.value) {
    receiptEdit.value.openTaskEditPopup(item.qty, item.handleQty, item.labelQty);
  }
};

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

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

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

const removeScan = (item) => {
  if (message.value) {
    message.value.showQuestionMessage("确定移除物料的所有扫描信息?", res => {
      if (res) {
        item.handleQty = null;
        item.subList.forEach(lst => {
          lst.scaned = false;
          lst.handleQty = null;
          lst.packList.forEach(pack => {
            pack.scaned = false;
            pack.handleQty = null;
          });
        });
        emit('updateData');
      }
    });
  }
};

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

const updateData = () => {
  emit('updateData');
};

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

<style>
</style>