Ext.override(Ext.form.action.Action, {
  submitEmptyText: false, // don't submit empty text by default
});

Ext.override(Ext.form.field.Date, {
  selectOnFocus: true,
  format: 'd/m/Y',
  formatText: 'Expected date format DD/MM/YYYY.',
  // just add "time" from default
  altFormats:
    'm/d/Y|n/j/Y|n/j/y|m/j/y|n/d/y|m/j/Y|n/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d|Y-m-d|n-j|n/j|time',
});

Ext.override(Ext.grid.column.Date, {
  format: 'd/m/Y',
});

Ext.override(Ext.Date, {
  defaultFormat: 'd/m/Y',
});

Ext.override(Ext.data.field.Field, {
  allowNull: true,
  initComponent: function () {
    const me = this;
    me.callParent(arguments);
    if (me.allowBlank === false) {
      me.labelSeparator =
        '<span style="color: red; padding-left: 2px;">*</span>:';
    }
    if (me.readOnly === true) {
      me.fieldStyle = 'background-color: #ddd;';
    }
  },
});

Ext.override(Ext.form.field.Number, {
  hideTrigger: true,
  mouseWheelEnabled: false,
  selectOnFocus: true,
  initComponent: function () {
    const me = this;
    me.callParent(arguments);
    if (me.allowBlank === false) {
      me.labelSeparator =
        '<span style="color: red; padding-left: 2px;">*</span>:';
    }
    if (me.readOnly === true) {
      me.fieldStyle = 'background-color: #ddd;';
    }
  },
});
Ext.override(Ext.form.field.Text, {
  selectOnFocus: true,
  initComponent: function () {
    const me = this;
    me.callParent(arguments);
    if (me.allowBlank === false) {
      me.labelSeparator =
        '<span style="color: red; padding-left: 2px;">*</span>:';
    }
    if (me.readOnly === true) {
      me.fieldStyle = 'background-color: #ddd;';
    }
  },
  setReadOnly: function (readOnly) {
    const me = this,
      triggers = me.getTriggers(),
      hideTriggers = me.getHideTrigger();
    let trigger, id;

    readOnly = !!readOnly;
    if (readOnly) {
      me.addCls('field-read-only');
    } else {
      me.removeCls('field-read-only');
    }

    me.callParent([readOnly]);
    if (me.rendered) {
      me.setReadOnlyAttr(readOnly || !me.editable);
    }

    if (triggers) {
      for (id in triggers) {
        trigger = triggers[id];
        /*
         * Controlled trigger visibility state is only managed fully when 'hideOnReadOnly' is falsy.
         * Truth table:
         *   - If the trigger is configured/defaulted as 'hideOnReadOnly : true', it's readOnly-visibility
         *     is determined solely by readOnly state of the Field.
         *   - If 'hideOnReadOnly : false/undefined', the Fields.{link #hideTrigger hideTrigger} is honored.
         */
        if (
          trigger.hideOnReadOnly === true ||
          (trigger.hideOnReadOnly !== false && !hideTriggers)
        ) {
          trigger.setVisible(!readOnly);
        }
      }
    }
  },
});
Ext.override(Ext.form.field.TextArea, {
  selectOnFocus: true,
  initComponent: function () {
    const me = this;
    me.callParent(arguments);
    if (me.allowBlank === false) {
      me.labelSeparator =
        '<span style="color: red; padding-left: 2px;">*</span>:';
    }
    if (me.readOnly === true) {
      me.fieldStyle = 'background-color: #ddd;';
    }
  },
});

Ext.override(Ext.grid.Panel, {
  style: {
    'border-radius': '9px',
    'box-shadow': '0 4px 8px 0 rgba(0,0,0,0.2)',
    // 'transition': '10s'
  },
});

Ext.override(Ext.form.Panel, {
  style: {
    'border-radius': '9px',
    'box-shadow': '0 4px 8px 0 rgba(0,0,0,0.2)',
  },
});

Ext.override(Ext.window.Window, {
  style: {
    'border-radius': '9px',
    'box-shadow': '0 4px 8px 0 rgba(0,0,0,0.2)',
  },
});

Ext.override(Ext.button.Button, {
  style: {
    'border-radius': '3px',
  },
});

Ext.override(Ext.form.field.ComboBox, {
  listeners: {
    focusenter: function (combo, event, eOpts) {
      combo.getStore().filter(function (record) {
        return record.get('deleted') ? false : true;
      });
    },
  },
});

Ext.override(Ext.form.Panel, {
  jsonSubmit: true,
});

Ext.override(Ext.form.Basic, {
  setReadOnly: function (readOnly) {
    this.getFields().each(function (field) {
      if (field.originalReadOnly === undefined)
        field.originalReadOnly = field.readOnly;
      field.setReadOnly(readOnly ? readOnly : field.originalReadOnly);
    });
  },
});

Ext.override(Ext.data.Store, {
  load: function (options) {
    const me = this;
    const proxy = me.getProxy();

    if (options && options.params && proxy.type === 'ajax') {
      const { params, callback } = options;
      const url = proxy.getUrl();

      options.callback = function (...args) {
        proxy.setUrl(url);
        if (callback) callback(...args);
      };

      let newUrl = url;
      options.params = {};

      Object.keys(params).forEach((k) => {
        const value = params[k];
        if (newUrl.indexOf('{' + k + '}') === -1) {
          options.params[k] = value;
        } else {
          let valueStr = '';
          if (value instanceof Date) {
            valueStr = encodeURIComponent(
              Ext.Date.format(value, App.dateSubmitFormat)
            );
          } else {
            valueStr = encodeURIComponent(value);
          }
          newUrl = newUrl.replaceAll('{' + k + '}', valueStr);
        }
      });
      proxy.setUrl(newUrl);
    }
    return me.callParent(arguments);
  },
});

Ext.override(Ext.form.action.Load, {
  handleResponse: function (response) {
    const me = this;

    if (me.form.reader) {
      return me.callParent(arguments);
    }

    if (response.status >= 500) {
      return { success: false };
    } else if (response.status >= 400) {
      try {
        const rs = Ext.decode(response.responseText);
        rs.success = false;
        return rs;
      } catch (e) {
        return { success: false };
      }
    } else if (response.status >= 300) {
      return { success: false };
    } else if (response.status >= 200) {
      try {
        const rs = Ext.decode(response.responseText);
        if (!rs.success) rs.success = true;
        return rs;
      } catch (e) {
        return { success: false };
      }
    } else if (response.status >= 100) {
      return { success: true };
    } else {
      try {
        return Ext.decode(response.responseText);
      } catch (e) {
        return { success: false };
      }
    }
  },
});

Ext.override(Ext.form.action.Submit, {
  handleResponse: function (response) {
    const me = this;

    if (me.form.errorReader) {
      return me.callParent(arguments);
    }

    if (response.status >= 500) {
      return { success: false };
    } else if (response.status >= 400) {
      try {
        const rs = Ext.decode(response.responseText);
        rs.success = false;
        return rs;
      } catch (e) {
        return { success: false };
      }
    } else if (response.status >= 300) {
      return { success: false };
    } else if (response.status >= 200) {
      if (response.status === 204) {
        return { success: true };
      }

      try {
        const rs = Ext.decode(response.responseText);
        if (!rs.success) rs.success = true;
        return rs;
      } catch (e) {
        return { success: false };
      }
    } else if (response.status >= 100) {
      return { success: true };
    } else {
      try {
        return Ext.decode(response.responseText);
      } catch (e) {
        return { success: false };
      }
    }
  },
});
