<template>
  <div>
    <ProcessReportDynamic
      v-if="reportType == 'DYNAMIC'"
      :process-report-dto="processReportDto"
      @value-changed="valueChanged"
    />
    <ProcessReportStatic
      v-if="reportType == 'STATIC'"
      :process-report-dto="processReportDto"
      @value-changed="valueChanged"
    />
  </div>
</template>

<script>

var Common = require('../../common/Common.js').default;
var ProcessReportDynamic = require('./ProcessReportDynamic.vue').default;
var ProcessReportStatic = require('./ProcessReportStatic.vue').default;

export default {
  name: 'ProcessReport',

  components: {
    ProcessReportDynamic,
    ProcessReportStatic,
  },
    
  data: function () {
    return {
      'reportType': '',
      'processReportDto': {},
    };
  },

  watch: {
    '$route': function (to, from) {
      this.processReportNo = this.$route.params.no;
      this.loadData();
    },
  },

  mounted: function () {
    this.processReportNo = this.$route.params.no;
    this.loadData();
  },

  methods: {

    /**
         * 根据流程报表ID加载流程报表数据
         * @return {void} 
         */
    loadData: function () {
      var _self = this;
      $.ajax({
        url: Common.getApiURL('ProcessReportResource/uniqueByNo'),
        type: 'get',
        dataType: 'json',
        beforeSend: function (request) {
          Common.addTokenToRequest(request);
        },
        data: {
          no: _self.processReportNo,
        },
        success: function (data) {
          console.log('参数结果' + data.reportType);
          _self.reportType = data.reportType;

          if (data.processReportParameters) {
            data.processReportParameters.forEach(function (item) {
              if (item.fieldValue == undefined) {
                item.fieldValue = {
                  id: '',
                  displayValue: [],
                  fieldType: 'String',
                };
              }
            });
          }

          _self.processReportDto = data;
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          Common.processException(XMLHttpRequest, textStatus, errorThrown);
        },
      });
    },
    // 值改变事件
    valueChanged: function (processReportParameter) {
      var _self = this;
      console.log('processReportParameter:' + JSON.stringify(processReportParameter));
      if (_self.processReportDto && _self.processReportDto.processReportParameters) {
        for (let i = 0; i < _self.processReportDto.processReportParameters.length; i++) {
          if (_self.processReportDto.processReportParameters[i].fieldName == processReportParameter.fieldName) {
            _self.processReportDto.processReportParameters[i].fieldValue = processReportParameter.fieldValue;
          }
        }
      }
      if (processReportParameter.calloutProcessNo && processReportParameter.calloutProcessNo != '') {
        _self.callout(processReportParameter.calloutProcessNo);
      }



      if (_self.processReportDto != null && _self.processReportDto.processReportParameters != null) {
        for (let i = 0; i < _self.processReportDto.processReportParameters.length; i++) {
          var item = _self.processReportDto.processReportParameters[i];
          if (item.fieldName != processReportParameter.fieldName
                        && item.whereClause != null
                        && item.whereClause.indexOf(':' + processReportParameter.fieldName) >= 0) {
            item.fieldValue.id = '';
            item.fieldValue.displayValue.splice(0, item.fieldValue.displayValue.length);
            _self.processReportDto.processReportParameters[i]=item;
          }
        }
      }

    },
    callout: function (calloutProcessNo) {
      var _self = this;
      $.ajax({
        url: Common.getApiURL('ProcessReportResource/runProcessReportCallout/') + calloutProcessNo,
        type: 'POST',
        beforeSend: function (request) {
          Common.addTokenToRequest(request);
        },
        contentType: 'application/json',
        data: JSON.stringify(_self.processReportDto),
        success: function (data) {
          if (data && data.processReportParameterDtos) {
            var processReportParameterDtos = data.processReportParameterDtos;
            for (var i = 0; i < data.processReportParameterDtos.length; i++) {
              var fieldName = data.processReportParameterDtos[i].fieldName;
              for (var j = 0; j < _self.processReportDto.processReportParameters.length; j++) {
                if (_self.processReportDto.processReportParameters[j].fieldName == fieldName) {
                  var parameter = _self.processReportDto.processReportParameters[j];
                  var cloneObject = {
                    'index': 1,
                    'isMandatory': false,
                    'id': parameter.id,
                    'fieldName': parameter.fieldName,
                    'name': parameter.name,
                    'help': parameter.help,
                    'displayType': parameter.displayType,
                    'listDisplayFieldNames': parameter.listDisplayFieldNames,
                    'infoWindowId': parameter.infoWindowId,
                    'sortNo': parameter.sortNo,
                    'isShow': parameter.isShow,
                    'constraintEnum': parameter.constraintEnum,
                    'whereClause': parameter.whereClause,
                    'enumClass': parameter.enumClass,
                    'fieldValue': data.processReportParameterDtos[i].fieldValue,
                    'calloutProcessNo': parameter.calloutProcessNo,
                    'mandatory': parameter.mandatory,
                    'listDisplayFieldName': parameter.listDisplayFieldName,
                    'displayName': parameter.displayName,

                  };
                  _self.processReportDto.processReportParameters[j]=cloneObject;
                }
              }
            }

          }

        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          Common.processException(XMLHttpRequest, textStatus, errorThrown);
        },
      });

    },

  },
};
</script>
<style scoped>
.control-label {
    font-size: 15px;
}

.processResult-textarea {
    width: 100%;
    height: 200px;
}
</style>