<template>
  <div>
    <div class="i-layout-page-header">
      <PageHeader :title="$route.meta.title" :content="$route.meta.description" hidden-breadcrumb />
    </div>
    <Card :bordered="false" dis-hover class="ivu-mt">
      <view-table
        ref="table"
        view-code="BarcodeTable"
        :tree-enable="true"
        :tree-load="row => onTreeLoad(row)"
        :has-children="
          row => {
            return row.barcode.hasChildren;
          }
        "
        :before-load-data="beforeLoadData"
        @on-load-data="onLoadData"
      >
        <template #filterCommand>
          <slot name="filterCommand"></slot>
        </template>
        <template #command="{ row, index }">
          <!-- <Button size="small" title="库存明细" type="info" custom-icon="fa fa-bars" @click="$refs.barcodeStorageTable.open({ barcodeID: row.barcode.id })"></Button> -->
          <slot name="command" :row="row" :index="index"></slot>
        </template>
      </view-table>
    </Card>
    <!-- <modal-table ref="barcodeStorageTable" view-code="BarcodeStorage" /> -->
  </div>
</template>
<script>
  import { mixinPage, modelApi } from 'mooho-base-admin-plus';

  export default {
    name: 'storage-barcodeStorage',
    mixins: [mixinPage],
    components: {},
    data() {
      return {
        filter: {}
      };
    },
    created() {},
    methods: {
      async onTreeLoad(row) {
        let res = await modelApi.query('BarcodeStorage', {
          'barcode.parentID': row.barcodeID,
          materialID: row.materialID,
          supplierID: row.supplierID,
          batchNumber: row.batchNumber,
          storageStatus: row.storageStatus,
          page: 1,
          per: 1000,
          orderBy: '+barcode.no'
        });

        for (let data of res.data) {
          data._depth = row._depth == null ? 1 : row._depth + 1;
        }

        return res.data;
      },
      beforeLoadData() {
        if (!(this.$refs.table.filterData['barcode.no'] || '').trim()) {
          // 普通查询，只查询顶层包装
          this.$refs.table.filterData['barcode.parentID'] = 'null';
        } else {
          // 根据条码号查询，查询所有包装
          delete this.$refs.table.filterData['barcode.parentID'];
        }
      },
      async onLoadData() {
        if (!!(this.$refs.table.filterData['barcode.no'] || '').trim()) {
          // 根据条码号查询
          let data = this.$refs.table.data;
          if (data.length > 0) {
            let dataWithParent = [];
            for (let i = 0; i < data.length; i++) {
              dataWithParent.push(await this.addParentData(data[i]));
            }

            data.length = 0;
            dataWithParent.forEach(item => {
              data.push(item);
            });
          }
        }
      },
      async addParentData(data) {
        if (data.barcode.parentID) {
          let res = await modelApi.query('BarcodeStorage', {
            'barcode.id': data.barcode.parentID,
            page: 1,
            per: 1
          });

          let result = res.data[0];
          result.children = [data];
          result._showChildren = true;
          return result;
        } else {
          return data;
        }
      }
    }
  };
</script>
