<template>
  <div>
    <div v-show="archiveAuthority">
      <div>
        <label>报表归档:</label>
        <button
          v-show="startArchive == false"
          class="btn btn-success"
          @click="startArchive = true"
        >
          归档
        </button>
        <button
          v-show="startArchive == false"
          class="btn btn-success"
          @click="searchArchiveRecord"
        >
          归档查询
        </button>
        <button
          v-show="startArchive == true"
          class="btn btn-success"
          @click="archive()"
        >
          保存
        </button>
        <button
          v-show="startArchive == true"
          class="btn btn-warning"
          @click="startArchive = false"
        >
          取消
        </button>
      </div>

      <div v-show="startArchive == true">
        <div class="form-group">
          <label>标题</label>
          <input
            v-model="title"
            autocomplete="off"
            type="text"
            class="form-control"
          />
        </div>
        <div class="form-group">
          <label>业务日期</label>
          <DateTime
            v-model="businessDate"
            class="form-control"
          />
        </div>
        <div class="form-group">
          <label>备注</label>
          <input
            v-model="description"
            autocomplete="off"
            type="description"
            class="form-control"
          />
        </div>
        <div class="form-group">
          <label>归档日期</label>
          <input
            v-model="archiveDate"
            autocomplete="off"
            type="text"
            class="form-control"
            :readonly="true"
          />
        </div>
        <div class="form-group">
          <label>归档人</label>
          <input
            v-model="userName"
            autocomplete="off"
            type="text"
            class="form-control"
            :readonly="true"
          />
        </div>
        <div
          v-for="item,index in processReportResult.reportResults"
          :key="'reportResult-' + index"
          class="form-group"
        >
          <label>报表路径{{ index + 1 }}</label>
          <input
            v-model="item.htmlPreviewUrl"
            autocomplete="off"
            type="text"
            class="form-control"
            :readonly="true"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Common from '../../common/Common.js';
import Notify from '../../common/Notify.js';
import DateTime from '../../datetime/src/DateTime.vue';

export default {

  components: {
    DateTime,
  },
  props: {
    'processReportResult':{
      type: Object,
      default: null,
    }},

  data: function () {
    return {
      archiveAuthority: '',
      startArchive: false,
      title: '',
      businessDate: '',
      description: '',
      userName: '',
      archiveDate: '',
      interval: '',
    };
  },
  watch: {
    'processReportResult': function () {
      this.initArchiveAuthority();
    },
  },
  mounted: function () {
    var _self = this;
    this.initArchiveAuthority();
    this.interval = window.setInterval(function () {
      _self.updateDate();
    }, 1000);

  },
  beforeUnmount: function () {
    clearInterval(this.interval);
  },

  methods: {
    initArchiveAuthority: function () {
      var _self = this;
      if (this.processReportResult) {
        this.title = this.processReportResult.processReportName;
      }

      var loginInfoJson = localStorage.getItem('json_LoginInfo');
      var loginInfo = JSON.parse(loginInfoJson);
      if (loginInfo != null) {
        _self.userName = loginInfo.userName;
      }
      if (_self.processReportResult == null || _self.processReportResult.processReportNo == null) {
        _self.archiveAuthority = false;
        return;
      }
      $.ajax({
        url: Common.getApiURL('ProcessReportResource/getArchiveAuthority'),
        type: 'get',
        dataType: 'json',
        data: {
          'processReportNo': _self.processReportResult.processReportNo,
        },
        beforeSend: function (request) {
          Common.addTokenToRequest(request);
        },
        success: function (data) {
          _self.archiveAuthority = data;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          Common.processException(XMLHttpRequest, textStatus, errorThrown);
        },
      });
    },
    archive: function () {
      var _self = this;
      var url = '';
      var reportNames = '';
      var reportResults = this.processReportResult.reportResults;
      for (var i = 0; i < reportResults.length; i++) {
        var reportResult = reportResults[i];
        var pdfDownLoadUrl = reportResult.pdfDownLoadUrl;
        if (pdfDownLoadUrl && pdfDownLoadUrl.length > 4) {
          url += pdfDownLoadUrl.substring(0, pdfDownLoadUrl.length - 4);
          reportNames += reportResult.reportName;
        }
        if (i + 1 < reportResults.length) {
          url += ',';
          reportNames += ',';
        }
      }
      $.ajax({
        url: Common.getApiURL('ArchiveResource/archive'),
        type: 'get',
        dataType: 'json',
        data: {
          'help': _self.title,
          'businessDate': _self.businessDate,
          'description': _self.description,
          'url': url,
          'reportNames': reportNames,
        },
        beforeSend: function (request) {
          Common.addTokenToRequest(request);
        },
        success: function (data) {
          Notify.success('归档成功', '归档成功，可以在【归档查询】页面查询所有归档信息', 1500);
          _self.startArchive = false;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          Common.processException(XMLHttpRequest, textStatus, errorThrown);
        },
      });
    },
    //格式化日期成 "yyyy-MM-dd HH-mm-SS"格式
    updateDate: function () {
      var date = new Date();
      var seperator1 = '-';
      var seperator2 = ':';
      var month = date.getMonth() + 1;
      var strDate = date.getDate();
      if (month >= 1 && month <= 9) {
        month = '0' + month;
      }
      if (strDate >= 0 && strDate <= 9) {
        strDate = '0' + strDate;
      }
      var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
                + ' ' + date.getHours() + seperator2 + date.getMinutes() + seperator2
                + (date.getSeconds() < 10 ? '0' : '') + date.getSeconds();
      this.archiveDate = currentdate;
    },
    searchArchiveRecord: function () {
      this.$router.push('/desktop/archive');
    },
  },
};
</script>