<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', 'next'],
  data: function () {
    return {
      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.assetInventoryStep2.inventorySheetName,
        warehouseId: _self.currentStep.assetInventoryStep2.warehouseId,
      };
      _self.loading = true;
      AssetInventoryResource.departmentGeneratesCountSheet(param).then(
        data => {
          if (data.errorCode != 0) {
            Notify.error('失败', data.errorMessage, false);
          } else {
            Notify.success('成功', data.errorMessage, false);
            _self.successText = data.errorMessage;
            _self.organizationNames = [
              {
                name: data.data.organizationName,
                documentNo: data.data.documentNo,
                documentName: data.data.name,
                id: data.data.id,
              }
            ]
          }

          _self.isShow = true;
          _self.loading = false;
        }, xmlHttpRequest => {
          _self.loading = false;
          Common.processException(xmlHttpRequest);
        });
    },

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