<template>
  <div class="collapseStyle">

    <el-scrollbar style="border: 1px solid #ebeef5" :height="tableHeight">
      <el-collapse accordion v-model="activeNames" >
        <el-segmented v-model="value" :options="options" size="large" @change="materialChange"/>
        <el-collapse-item v-for="(item, index) in materialsData" :key="index" :title="item.opName" :name="index">

          <el-table :data="item.children" border>
            <el-table-column prop="itemName" label="物料名称" />
            <el-table-column prop="itemModel" label="物料规格" />
            <el-table-column prop="itemCode" label="物料编号" />
<!--            <el-table-column prop="itemSeq" label="采集编码" />-->
            <el-table-column prop="batchNo" label="批次号" />
            <el-table-column prop="seqNo" label="管号" />
            <el-table-column v-if="value==='host'" prop="oldBatchNo" label="原批次号" />
            <el-table-column v-if="value==='host'" prop="needNum" label="需求数量" />
            <el-table-column v-if="value==='host'" prop="realNum" label="已采集数量" />
          </el-table>
        </el-collapse-item>
      </el-collapse>
      <Empty v-if="materialsData.length < 1" />
    </el-scrollbar>
    <Pagination position="right" :page="page" :limit="limit" :total="total" @pagination="materialChange" />
  </div>
</template>

<script lang="ts" setup>
import { useProcessStore } from "@/store";
import {getAccessoryMaterialsInfo, getMaterialsInfo} from "@/api/process/traceability";
const store = useProcessStore();
const activeNames = ref([0]);
const page = ref(1);
const limit = ref(10);
const total = ref(10);
const materialsData = ref([]);
const tableHeight = ref(null);
const value = ref('host')

const options = [
  {
  label: '主料记录',
  value: 'host'

},
{
  label: '辅料记录',
  value: 'auxiliary',
},
]
const getPagination = async () => {
  const { data } = await getMaterialsInfo({
    pageNo: page.value,
    pageSize: limit.value,
    seqNo: store.useSeqNo,
    workOrderCode: store.odersData.workOrderCode,
  });
  total.value = data.totalCount;
  materialsData.value = data.records;
};
const getAccessoryPagination = async () => {
  const { data } = await getAccessoryMaterialsInfo({
    pageNo: page.value,
    pageSize: limit.value,
    seqNo: store.useSeqNo,
    workOrderCode: store.odersData.workOrderCode,
  });
  total.value = data.totalCount;
  materialsData.value = data.records;
};
const materialChange = () =>{
  if(value.value==="auxiliary"){
    getAccessoryPagination();
  }else{
    getPagination();
  }
}
//动态控制高度
const setTableHeight = () => {
  tableHeight.value =
    Number(document.getElementById("tabBox").offsetHeight) - 70;
};
onMounted(() => {
  getPagination();
  setTableHeight();
});
</script>

<style lang="scss" scoped></style>
