<template>
  <div>
    <a-result v-if="isShow" status="success" title="操作成功!" sub-title="恭喜您，合并盘点单成功.">
      <template #extra>
        <div style="text-align: left; margin-bottom: 20px;">
          <a-table :columns="columns" :data-source="organizationNames">
            <template #bodyCell="{ column,record }">
              <template v-if="column.key === 'name'" />
              <template v-else-if="column.key === 'documentName'">
                <span>
                  {{ record.documentName }}
                </span>
              </template>
              <template v-else-if="column.key === 'documentNo'">
                <span>
                  {{ record.documentNo }}
                </span>
              </template>
              <template v-else-if="column.key === 'operation'">
                <a-button type="primary" @click="openCurdWindow(record.id)">查看盘点单</a-button>
              </template>
            </template>
          </a-table>
        </div>
      </template>
    </a-result>
    <a-result v-else title="合并盘点单">
      <template #extra>
        <a-button key="console" type="primary" @click="end">确定合并盘点</a-button>
      </template>
    </a-result>
    <a-divider />
    <Loading v-if="loading" />
    <a-button key="console" type="primary" @click="previous">上一步</a-button>
    <a-button key="console" style="float: right;" type="primary" @click="next">下一步</a-button>
  </div>
</template>

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

export default {
  components: {},

  props: {
    currentStep: {
      type: Object,
      default () {
        return {};
      },
    },
  },

  // 定义抛出的事件名称
  emits: ['previous'],
  data: function() {
    this.treeList = [];
    return {
      treeData: [],
      expandedKeys: [],
      checkedKeys: [],
      autoExpandParent: true,
      inventorySheetName: null,
      currentStepTempory: {},
      isShow: false,
      loading: false,
      organizationNames: [],
      columns: [{
        title: '部门',
        dataIndex: 'name',
        key: 'name',
      }, {
        title: '盘点单名称',
        dataIndex: 'documentName',
        key: 'documentName',
      }, {
        title: '盘点单编号',
        dataIndex: 'documentNo',
        key: 'documentNo',
      },
      {
        title: '操作',
        dataIndex: 'operation',
        key: 'operation',
      },
      ],
    };
  },
  mounted: function() {
    console.log('333333');
    console.log(this.currentStep);
  },
  methods: {
    /**
			 * 打开资产盘点单的CURD窗口
			 */
    openCurdWindow: function(data) {
      let url = Common.getRedirectUrl('#/desktop/window1/window-read/view/041101/0/' + data +
					'?currPage=1&currIndex=1&totalCount=1&uuid=' + Uuid.createUUID());
      window.open(url);
    },
    end: function() {
      var _self = this;
      _self.loading = true;
      var mergeIntoInventoryIds = [];
      mergeIntoInventoryIds.push(_self.currentStep.mergeInventorySheetId);
      var param = {
        combinedInventoryIds: _self.currentStep.consolidatedInventorySheetIds,
        mergeIntoInventoryIds: mergeIntoInventoryIds,
      };
      AssetInventoryResource.consolidatedAssetInventory(param).then(
        successData => {
          if (successData.errorCode == 0) {
            _self.isShow = true;
            var organizationName = {
              name: successData.organizationName,
              documentNo: successData.documentNo,
              documentName: successData.name,
              id: successData.id,
            };
            _self.organizationNames.push(organizationName);
            Common.showDialog('提示', successData.errorMessage, 'success');
          } else {
            Common.showDialog('提示', successData.errorMessage, 'error');
          }
          console.log(successData);
          _self.loading = false;
        },
        errorData => {
          _self.loading = false;
        },
      );
    },

    previous: function() {
      var _self = this;
      var data = {
        currentStep: 1,
        showPage: 7,
        mergeInventorySheetId: _self.currentStep.mergeInventorySheetId,
      };
      this.$emit('previous', data);
    },
    next: function() {
      var _self = this;
      _self.$router.push('/eam/assetInventory');
    },
  },
};
</script>
<style>
</style>