<template>
  <el-drawer v-model="open" :title="`申购列表${applyPurchaseNumTotal != 0 ? '(' + applyPurchaseNumTotal + ')' : ''}`"
    :before-close="handleClose" direction="rtl" class="demo-drawer" modal-class="el-subscribe-drawer" size="80%">
    <Index />
    <template #footer v-if="applyPurchaseNumTotal > 0">
      <el-button plain @click="clearApplyPurchaseDetails" v-if="applyPurchaseNumTotal > 0">清 空</el-button>
      <el-button :disabled="applyPurchaseNumTotal == 0" type="primary" @click="submitForm">提 交</el-button>
    </template>
  </el-drawer>
</template>

<script setup>
import { clearApplyPurchaseDetails } from "@/utils/applyPurchaseDetails";
import Index from "./index";
const open = ref(false);
const store = useStore();
const emit = defineEmits(["submit"]);

const applyPurchaseNumTotal = computed(() => {
  let total = 0;
  Object.values(store.state.data.applyPurchaseDetails).map((item) => {
    total += item.applyPurchaseNum;
  });
  store.state.data.applyPurchaseDetailsCustomList.map((item) => {
    total += item.applyPurchaseNum;
  });
  return total;
});

// 显示弹框
function show() {
  open.value = true;
}

// 关闭按钮
function handleClose() {
  open.value = false;
}

function submitForm() {
  emit("submit", store.state.data.applyPurchaseDetails)
}

defineExpose({
  show,
  handleClose,
});
</script>