<template>
  <div>
    <Navbar :title="$t('lang.ConsolidatedAssetInventory.consolidatedAssetInventory')" :is-go-back="false" />
    <a-space>
      <label>{{ $t("lang.ConsolidatedAssetInventory.inventoryName") }}</label>
      <a-input v-model:value="name" @press-enter="resetQuery" />
      <label>{{ $t("lang.ConsolidatedAssetInventory.inventoryNo") }}</label>
      <a-input v-model:value="documentNo" @press-enter="resetQuery" />
      <a-button type="primary" @click="resetQuery">
        {{ $t("lang.ConsolidatedAssetInventory.query") }}
      </a-button>
      <a-button style="background-color: #4aa14a; color: white;" @click="resetQuery">
        {{ $t("lang.ConsolidatedAssetInventory.combinedInventory") }}
        <span style="color: white;">{{ '(' + consolidatedInventorySheetIds.length + ')' }}</span>
      </a-button>
    </a-space>
    <CommonTable
      ref="table2" :columns="columns" :data-source="inventoryDtos" :total="pagination.total"
      :is-select="true" :extra-height="120" @get-pager="getPageParams" @get-selected="getSelectParams"
    />
    <div class="footer">
      <a-button type="primary" @click="previous">上一步</a-button>
      <a-button style="float: right;" type="primary" @click="next">下一步</a-button>
    </div>
    <Loading v-if="loading" />
  </div>
</template>

<script>
import Common from '../common/Common.js';
import { Notify } from 'pc-component-v3';
import CommonTable from '../common/CommonTable.vue';
import AssetInventoryResource from '../api/asset/AssetInventoryResource.js';

export default {
  components: {
    CommonTable,
  },
  props: {
    currentStep: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  emits: ['previous', 'next'],
  data: function () {
    return {
      inventoryDtos: [],
      consolidatedInventorySheetIds: [], //被合并的盘点单
      pagination: {
        total: 0,
        per_page: Common.pageSize, // required
        current_page: 1, // required
        last_page: 0, // required
      },
      name: null,
      documentNo: null,
      checked: false,
      loading: false,
      columns: [
        {
          title: this.$t('lang.ConsolidatedAssetInventory.departmentName'),
          dataIndex: 'organizationName',
        },
        {
          title: this.$t('lang.ConsolidatedAssetInventory.organizationName'),
          dataIndex: 'clientName',
        },
        {
          title: this.$t('lang.ConsolidatedAssetInventory.inventoryName'),
          dataIndex: 'name',
        },
        {
          title: this.$t('lang.ConsolidatedAssetInventory.inventoryNo'),
          dataIndex: 'documentNo',
        },
        {
          title: this.$t('lang.ConsolidatedAssetInventory.startDate'),
          dataIndex: 'inventoryStartDate',
        },
        {
          title: this.$t('lang.ConsolidatedAssetInventory.expirationDate'),
          dataIndex: 'inventoryEndDate',
        },
        {
          title: this.$t('lang.ConsolidatedAssetInventory.accountDate'),
          dataIndex: 'accountDate',
        },
      ].map(column => ({
        ...column,
        ellipsis: true,
        align: 'center',
        resizable: true,
      })),
    };
  },
  computed: {
  },

  watch: {
    // 如果路由有变化，会再次执行该方法
    $route: 'init',
  },

  mounted: function () {
    this.init();
    console.log(this.currentStep);
  },

  methods: {
    getPageParams(current, pageSize) {
      this.pagination.current_page = current;
      this.pagination.per_page = pageSize;
      this.getDatas();
    },
    getSelectParams(selected) {
      this.consolidatedInventorySheetIds = selected.selectedRowKeys;
    },
    previous: function () {
      var data = {
        currentStep: 1,
        showPage: 1,
      };
      this.$emit('previous', data);
    },
    next: function () {
      var _self = this;
      if (_self.consolidatedInventorySheetIds == undefined || _self.consolidatedInventorySheetIds == null || _self.consolidatedInventorySheetIds.length < 1) {
        Notify.error('错误', '未选择被合并盘点单', 1000);
        return;
      }
      var data = {
        currentStep: 1,
        showPage: 8,
        mergeInventorySheetId: _self.currentStep.mergeInventorySheetId,
        consolidatedInventorySheetIds: _self.consolidatedInventorySheetIds,
      };
      _self.$emit('next', data);
    },

    /**
       * 重置分页，然后查询
       */
    resetQuery: function () {
      this.$refs.table2.backFirstPage();
    },

    /**
       * 查询数据
       */
    getDatas: function () {
      var _self = this;
      _self.loading = true;
      var param = {
        range: {
          start: (_self.pagination.current_page - 1) * _self.pagination.per_page,
          length: _self.pagination.per_page,
        },
        name: _self.name,
        documentNo: _self.documentNo,
      };
      AssetInventoryResource.findByAssetInventoryRequest(param).then(
        successData => {
          _self.pagination.total = successData.totalSize;
          if (successData.dataList) {
            successData.dataList.forEach(item => {
              item.checked = false;
            });
          }
          _self.inventoryDtos = successData.dataList;
          _self.loading = false;
          if (
            successData.dataList == undefined ||
            successData.dataList.length == 0
          ) {
            return;
          }
          _self.pagination.last_page = Math.ceil(
            successData.totalSize / successData.range.length,
          );
        },
        errorData => {
          _self.loading = false;
          Common.processException(errorData);
        },
      );
    },



    init: function () {
      var _self = this;
      _self.getDatas();
    },
  },
};
</script>


<style scoped>
.footer {
  display: flex;
  justify-content: space-between;
}
</style>