<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 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',
      }],
    };
  },

  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/041101/0/' + data +
        '?currPage=1&currIndex=1&totalCount=1&uuid=' + Uuid.createUUID());
      window.open(url);
    },

    end: function () {
      var _self = this;
      var param = _self.currentStep.assetInventoryStep3;
      _self.loading = true;
      AssetInventoryResource.departmentGeneratesCountSheet(param).then(
        data => {
          if (data.errorCode != 0) {
            Notify.error('失败', data.errorMessage, false);
          } else {
            data.datas.forEach(function (item) {
              var organizationName = {
                name: item.organizationName,
                documentNo: item.documentNo,
                documentName: item.name,
                id: item.id,
              };
              _self.organizationNames.push(organizationName);
            });
            Notify.success('成功', '全单位生成盘点单成功', 2000);
          }

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

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