<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-button v-if="!isShow" style="float: left" type="primary" @click="end">确定合并盘点</a-button>
    <a-divider />
    <Loading v-if="loading" />
    <a-button type="primary" @click="previous">上一步</a-button>
    <a-button key="console" style="float: right;" type="primary" @click="next">下一步</a-button>
  </div>
</template>

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

module.exports = {

  props: {
    consolidatedCountSheet: {
      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',
      },
      ],
    };
  },

  components: {
    Loading,
    Common,
    Notify,
  },

  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;
      // if (_self.consolidatedCountSheet.mergeIntoInventoryIds.length == 0) {
      //   if (_self.consolidatedCountSheet.combinedInventoryIds.length == 0) {
      //     Common.showDialog('提示', '请先选择加入到被合并盘点单', 'error');
      //     return;
      //   }
      // }else{
      if (_self.consolidatedCountSheet.combinedInventoryIds.length == 0) {
        Common.showDialog('提示', '请先选择加入到被合并盘点单', 'error');
        return;
      }
      _self.loading=true;
      var param = {
        combinedInventoryIds: _self.consolidatedCountSheet.combinedInventoryIds,
        mergeIntoInventoryIds: _self.consolidatedCountSheet.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: 1,
        combinedInventoryIds: [],
        mergeIntoInventoryIds: _self.consolidatedCountSheet.mergeIntoInventoryIds,
      };
      this.$emit('previous', data);
    },
    next: function() {
      var _self = this;
      _self.$router.push('/eam/assetInventory');
    },
  },
  mounted: function() {

  },
};
</script>
<style>
</style>
