<template>
  <view>

    <!-- <requiredLocation title="需求库位" :locationCode="dataContent.toLocationCode"
      :isShowEdit="dataContent.allowModifyLocation==1"></requiredLocation> -->
    <view v-for="(item,index) in dataContent.Items">
      <u-collapse ref="collapse">
        <u-collapse-item :open="true">
          <template v-slot:title>
            <view class="split_line"></view>
            <!-- 物品 -->
            <u-swipe-action ref="swipeAction" :options="removeOptions"
                            @click="(...event)=>itemCoceClick(event,item,index)">
              <item-qty :dataContent="item" :handleQty="item.handleQty"></item-qty>
            </u-swipe-action>
            <!-- 			<com-issue-request-info :workShopCode="dataContent.workShopCode" :dataContent="dataContent">
            </com-issue-request-info> -->
          </template>
          <view v-for="(loacation,locatonIndex) in item.Locations">
            <view v-if="locatonIndex ==0">
              <view class="uni-flex uni-row space-between">
                <!-- 推荐库位 -->
                <location :locationCode="loacation.fromLocationCode">
                </location>
              </view>
              <view v-for="(batch,batchIndex) in loacation.Batchs">
                <batch :batch="batch.detail.batch" v-if="batchIndex ==0"></batch>
              </view>
            </view>
          </view>
        </u-collapse-item>
      </u-collapse>
    </view>
  </view>
  <balance-qty-edit ref="balanceQtyEdit" @confirm="confirm" :isShowStatus="true"></balance-qty-edit>
  <detail-info-popup ref="detailInfoPopup"></detail-info-popup>
  <comMessage ref="message"></comMessage>
</template>

<script lang="ts" setup>
import {ref, onMounted, nextTick} from 'vue';
import itemQty from '@/mycomponents/item/itemQty.vue'
import recommend from '@/mycomponents/recommend/recommend.vue'
import recommendBalance from '@/mycomponents/balance/recommendBalance.vue'
import handleBalance from '@/mycomponents/balance/handleBalance.vue'
import recommendQtyEdit from '@/mycomponents/qty/recommendQtyEdit.vue'
import jobDetailPopup from '@/mycomponents/job/jobDetailPopup.vue'
import requiredLocation from '@/mycomponents/location/requiredLocation.vue'
import balanceQtyEdit from '@/mycomponents/qty/balanceQtyEdit.vue'
import location from '@/mycomponents/balance/location.vue'
import detailInfoPopup from '@/pages/productionReceipt/coms/detailInfoPopup.vue'
import comIssueRequestInfo from '@/pages/issue/coms/comIssueRequestInfo.vue'
import batch from '@/mycomponents/balance/batch.vue'
import {getDetailOption, getEditRemoveOption, getRemoveOption} from '@/common/array.js';

const emit = defineEmits(['updateData', "removeItemCode"])
const props = defineProps({
  dataContent: {
    type: Object,
    default: {}
  },
  settingParam: {
    type: Object,
    default: {}
  }
})

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

const dataContent = ref({});
const balanceQtyEdit = ref(null);
const receiptHint = ref(null);
const message = ref(null);

onMounted(() => {
  detailOptions.value = getDetailOption();
  scanOptions.value = getEditRemoveOption();
  removeOptions.value = getRemoveOption();
});

const resizeCollapse = () => {
  nextTick(() => {
    const collapseRefs = ref(null);
    collapseRefs.value.forEach(r => {
      r.childrens.forEach(i => {
        i.init();
      });
      r.resize();
    });
  });
};

const swipeClick = (e, batch, record, recordIndex, batchIndex, Batchs, locatonIndex, Locations) => {
  if (e.content.text === "编辑") {
    edit(batch, record);
  } else if (e.content.text === "移除") {
    remove(batch, record, recordIndex, batchIndex, Batchs, locatonIndex, Locations);
  }
};

const itemCoceClick = (e, item, index) => {
  if (e.content.text === "移除") {
    dataContent.value.Items.splice(index, 1);
    emit('removeItemCode');
  }
};

const edit = (batch, item) => {
  editItem.value = item;
  batchItem.value = batch;
  item.balance.balanceQty = item.balance.qty;
  balanceQtyEdit.value.openEditPopup(item.balance, item.qty);
};

const detail = (item) => {
  showItem.value = item;
  receiptHint.value.openScanPopup();
};

const remove = (batch, record, recordIndex, batchIndex, Batchs, locatonIndex, Locations) => {
  message.value.showQuestionMessage("确定移除扫描信息?", res => {
    if (res) {
      batch.Records.splice(recordIndex, 1);
      if (batch.Records.length === 0 && Batchs[batchIndex].isNewAdd) {
        Batchs.splice(batchIndex, 1);
      }
      if (batch.Records.length === 0 && Locations[locatonIndex].isNewAdd) {
        Locations.splice(locatonIndex, 1);
      }
      resizeCollapse();
      emit('updateData', record);
    }
  });
};

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

</script>

<style>
</style>