<template>
  <div>
    <a-result v-if="isShow" status="success" title="操作成功!" :sub-title="successText">
      <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 style="margin:14px 0 20px 0 !important;" />
    <a-button key="console" type="primary" @click="previous">上一步</a-button>
    <a-button key="console" style="float: right;" type="primary" @click="next">返回</a-button>
    <Loading v-if="loading" />
  </div>
</template>

<script>

import Common from '../common/Common.js';
import { Notify, Uuid } from 'pc-component-v3';
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',
      },
      ],
      successText: '',
    };
  },

  watch: {
    currentStep: {
      handler(newVal, oldVal) {
        console.log(newVal);
        if (newVal != null) {
          this.currentStepTempory = JSON.parse(JSON.stringify(newVal));
        }
      },
      immediate: true,
    },

    currentStepTempory: {
      handler(newVal, oldVal) {
        clearTimeout(this.timer); //清除延迟执行
        let _self = this;
        this.timer = setTimeout(() => { //设置延迟执行
          _self.$emit('update', _self.currentStepTempory);
        }, 1000);
      },
      deep: true,
    },
  },
  mounted: function () {

  },

  methods: {
    /**
       * 打开资产盘点单的CURD窗口
       */
    openCurdWindow: function (data) {
      let url = Common.getRedirectUrl('#/desktop/window1/window-read/view/081001/0/' + data +
        '?currPage=1&currIndex=1&totalCount=1&uuid=' + Uuid.createUUID());
      window.open(url);
    },

    // 确认生成抽盘盘点单
    end: function () {
      var _self = this;
      var param = {
        checkVouchName: _self.currentStep.assetInventoryStep4.checkVouchName,
        inventoryIds: _self.currentStep.assetInventoryStep4.inventoryIds,
        warehouseId: _self.currentStep.assetInventoryStep4.warehouseId,
      };
      console.log(param, '抽盘参数---');
      _self.loading = true;
      AssetInventoryResource.generateCountSheetBySampling(param).then(
        data => {
          _self.loading = false;
          if (data.errorCode == 0) {
            _self.organizationNames = [
              {
                name: data.data.organizationName,
                documentNo: data.data.documentNo,
                documentName: data.data.name,
                id: data.data.id,
              }
            ];
            Notify.success('成功', '抽盘生成盘点单成功', 2000);
            _self.successText = data.errorMessage;
            _self.isShow = true;
          } else {
            Notify.error('错误', data.errorMessage, 2000);
          }
        }, xmlHttpRequest => {
          _self.loading = false;
          Common.processException(xmlHttpRequest);
        });
    },

    previous: function () {
      var data = {
        currentStep: 1,
        showPage: 3,
        assetInventoryStep4: this.currentStep.assetInventoryStep4,
      };
      this.$emit('previous', data);
    },
    next: function () {
      var _self = this;
      _self.$router.push('/wms/asset-inventory');
    },
  },
};
</script>
<style></style>
