import Common from '../../common/Common.js';

export default {
  /**
   * 富文本类型
   * @param {Object} field
   */
  isRichTextAreaEditor: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'RichTextAreaEditor') {
      return true;
    }
    return false;
  },
  // 是否是文本类型
  isTextType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'TextBoxEditor') {
      return true;
    }
    return false;
  },

  // 是否是数字类型
  isNumberType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'DoubleBoxEditor' || field.displayType == 'IntegerBoxEditor'
      || field.displayType == 'LongBoxEditor' || field.displayType == 'FloatBoxEditor'
      || field.displayType == 'BigDecimalEditor') {
      return true;
    }
    return false;
  },

  // 是否是复选框类型
  isCheckBoxType: function (field) {
    if (!field || field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'CheckBoxEditor') {
      return true;
    }
    return false;
  },


  // 是否是红蓝字框类型
  isRedGreenEditorType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'RedGreenEditor') {
      return true;
    }
    return false;
  },

  // 是否是日期类型
  isDateType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'DateBoxEditor') {
      return true;
    }
    return false;
  },

  // 是否是时间类型
  isTimeType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'TimeBoxEditor') {
      return true;
    }
    return false;
  },

  // 是否是日期时间类型
  isDateTimeType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'DateTimeBoxEditor') {
      return true;
    }
    return false;
  },

  // 是否是富文本类型
  isTextAreaType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'TextAreaEditor') {
      return true;
    }
    return false;
  },

  // 是否是搜索类型
  isSearchType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'SearchBoxEditor') {
      return true;
    }
    return false;
  },

  // 是否是图片类型
  isImageType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'ImageEditor') {
      return true;
    }
    return false;
  },

  // 是否是图片组类型
  isImageListType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'ImageListEditor') {
      return true;
    }
    return false;
  },

  // 是否是枚举下拉类型
  isEnumListType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'ListBoxEnumEditor') {
      return true;
    }
    return false;
  },

  // 是否是枚举多选类型
  isEnumMultiType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'EnumMultiSelect') {
      return true;
    }
    return false;
  },

  // 是否是下拉类型
  isSelectType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'ListBoxEditor') {
      return true;
    }
    return false;
  },

  // 是否是password类型
  isPasswordType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'PasswordTextBoxEditor') {
      return true;
    }
    return false;
  },

  // 是否是RadioButtonGroup类型
  isRadioButtonGroupType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'RadioButtonGroupEditor') {
      return true;
    }
    return false;
  },

  // 是否是ManyToManySetBoxEditor类型
  isManyToManySetType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'ManyToManySetBoxEditor') {
      return true;
    }
    return false;
  },

  // 判断 FieldValue是否相等
  isFieldValueEqual: function (fieldValue1, fieldValue2) {
    if (fieldValue1 == undefined && fieldValue2 == undefined) {
      return true;
    }
    if (fieldValue1 == undefined && fieldValue2 != undefined) {
      return false;
    }
    if (fieldValue1 != undefined && fieldValue2 == undefined) {
      return false;
    }
    if (fieldValue1 === fieldValue2) {
      return true;
    }
    if (fieldValue1.fieldType != fieldValue2.fieldType) {
      return false;
    }
    if (fieldValue1.fieldType == 'Key') {
      return (fieldValue1.id == fieldValue2.id)
        && (fieldValue1.displayValue[0] == fieldValue2.displayValue[0]);
    } else if (fieldValue1.fieldType == 'String') {
      if(fieldValue1.displayValue !== fieldValue2.displayValue){
        return false;
      }
      if (fieldValue1.displayValue == undefined) {
        fieldValue1.displayValue = [];
      }
      if (fieldValue2.displayValue == undefined) {
        fieldValue2.displayValue = [];
      }
      return fieldValue1.displayValue[0] == fieldValue2.displayValue[0];
    } else if (fieldValue1.fieldType == 'ManyToManyKey') {
      if (fieldValue1.ids == undefined) {
        fieldValue1.ids = [];
      }
      if (fieldValue2.ids == undefined) {
        fieldValue2.ids = [];
      }
      if (fieldValue1.ids.length != fieldValue2.ids.length) {
        return false;
      }
      for (var i = 0; i < fieldValue1.ids.length; i++) {
        if (fieldValue2.ids.indexOf(fieldValue1.ids[i]) < 0) {
          return false;
        }
      }
      return true;
    }
    return false;
  },

  /**
   * 是否是视频框
   * @param  {Object}  field 字段
   * @return {Boolean}       
   */
  isVideoType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'Video') {
      return true;
    }
    return false;
  },

  /**
   * 是否是文件框
   * @param  {Object}  field 字段
   * @return {Boolean}       
   */
  isFileType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'File') {
      return true;
    }
    return false;
  },

  /**
   * 是否是年份选择器
   */
  isYearType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'YearEditor') {
      return true;
    }
    return false;
  },

  /**
   * 是否是年月选择器
   */
  isYearMonthType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'YearMonthEditor') {
      return true;
    }
    return false;
  },

  /**
   * 是否是按钮类型
   */
  isButtonType: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'ButtonEditor') {
      return true;
    }
    return false;
  },

  /**
   * 是否是自定义渲染
   */
  isCustomerRender: function (field) {
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'CustomerRender') {
      return true;
    }
    return false;
  },



  /**
   * 格式化数据
   * @param  {[type]} value      [description]
   * @param  {[type]} pattern    [description]
   * @param  {[type]} successFun [description]
   * @return {[type]}            [description]
   */
  formatNumber: function (value, pattern, successFun) {
    if (value == undefined || pattern == undefined) {
      return;
    }
    $.ajax({
      url: Common.getApiURL('FieldResource/formatNumber'),
      type: 'GET',
      contentType: 'application/json',
      data: {
        'value': value,
        'pattern': pattern,
      },
      success: function (data) {
        successFun(data);
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        Common.processException(XMLHttpRequest, textStatus, errorThrown);
      },
    });
  },

  /**
   * 格式化数字
   * @param  {[type]} value      [description]
   * @param  {[type]} pattern    [description]
   * @return {[type]}            [description]
   */
  formatNumbers: function (value, pattern, displayType) {
    if (value == undefined || value == null) {
      return;
    }
    //格式化去掉小数点
    if ((displayType == 'DoubleBoxEditor' || displayType == 'IntegerBoxEditor'
      || displayType == 'LongBoxEditor' || displayType == 'FloatBoxEditor'
      || displayType == 'BigDecimalEditor') && pattern == '0') {
      return parseInt(value);
    }
    //格式化成以","隔开
    if ((displayType == 'DoubleBoxEditor' || displayType == 'IntegerBoxEditor'
      || displayType == 'LongBoxEditor' || displayType == 'FloatBoxEditor'
      || displayType == 'BigDecimalEditor') && value.length > 5) {
      var val = this.number_format(value, '.', ',');
      return val;
    } else {
      return value;
    }
  },
  /*
  * 参数说明：
  * number：要格式化的数字
  * decimals：保留几位小数
  * dec_point：小数点符号
  * thousands_sep：千分位符号
  * */
  number_format: function (number, decimals, dec_point, thousands_sep) {
    number = (number + '').replace(/[^0-9+-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
      prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
      sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
      dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
      s = '',
      toFixedFix = function (n, prec) {
        var k = Math.pow(10, prec);
        return '' + Math.floor(n * k) / k;
      };
    s = (prec ? toFixedFix(n, prec) : '' + Math.floor(n)).split('.');
    var re = /(-?\d+)(\d{3})/;
    console.log(s);
    while (re.test(s[0])) {
      s[0] = s[0].replace(re, '$1' + sep + '$2');
    }
    if ((s[1] || '').length < prec) {
      s[1] = s[1] || '';
      s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
  },
  /**
   * 是否操作列
   */
  isGridButtonEditor:function(field){
    if (field.displayType == undefined) {
      return false;
    }
    if (field.displayType == 'GridButtonEditor') {
      return true;
    }
    return false;
  },

};