import { Spin } from 'antd';
import { WeaSwitch } from 'comsMobx';
import { WeaLocaleProvider, WeaAlertPage, WeaSearchGroup, WeaFormItem } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import {
  indexOf
} from 'lodash';
import '../style/index.less'

const dbKeys = ["PERCENT", "PLAN", "PRECISION", "PRIMARY", "PRINT", "PROC", "PROCEDURE", "PUBLIC", "RAISERROR",
    "READ", "READTEXT", "RECONFIGURE", "REFERENCES", "REPLICATION", "RESTORE", "RESTRICT", "RETURN", "REVOKE",
    "RIGHT", "ROLLBACK", "ROWCOUNT", "ROWGUIDCOL", "RULE", "SAVE", "SCHEMA", "SELECT", "SESSION_USER", "SET",
    "SETUSER", "SHUTDOWN", "SOME", "STATISTICS", "SYSTEM_USER", "TABLE", "TEXTSIZE", "THEN", "TO", "TOP", "TRAN",
    "TRANSACTION", "TRIGGER", "TRUNCATE", "TSEQUAL", "UNION", "UNIQUE", "UPDATE", "UPDATETEXT", "USE", "USER",
    "VALUES", "VARYING", "VIEW", "WAITFOR", "WHEN", "WHERE", "WHILE", "WITH", "WRITETEXT", "EXCEPT", "EXEC", "EXECUTE",
    "EXISTS", "EXIT", "FETCH", "FILE", "FILLFACTOR", "FOR", "FOREIGN", "FREETEXT", "FREETEXTTABLE", "FROM", "FULL",
    "FUNCTION", "GOTO", "GRANT", "GROUP", "HAVING", "HOLDLOCK", "IDENTITY", "IDENTITY_INSERT", "IDENTITYCOL", "IF", "IN",
    "INDEX", "INNER", "INSERT", "INTERSECT", "INTO", "IS", "JOIN", "KEY", "KILL", "LEFT", "LIKE", "LINENO", "LOAD", "NATIONAL",
    "NOCHECK", "NONCLUSTERED", "NOT", "NULL", "NULLIF", "OF", "OFF", "OFFSETS", "ON", "OPEN", "OPENDATASOURCE", "OPENQUERY",
    "OPENROWSET", "OPENXML", "OPTION", "OR", "ORDER", "OUTER", "OVER", "ADD", "ALL", "ALTER", "AND", "ANY", "AS", "ASC", "AUTHORIZATION",
    "BACKUP", "BEGIN", "BETWEEN", "BREAK", "BROWSE", "BULK", "BY", "CASCADE", "CASE", "CHECK", "CHECKPOINT", "CLOSE", "CLUSTERED",
    "COALESCE", "COLLATE", "COLUMN", "COMMIT", "COMPUTE", "CONSTRAINT", "CONTAINS", "CONTAINSTABLE", "CONTINUE", "CONVERT", "CREATE",
    "CROSS", "CURRENT", "CURRENT_DATE", "CURRENT_TIME", "CURRENT_TIMESTAMP", "CURRENT_USER", "CURSOR", "DATABASE", "DBCC", "DEALLOCATE",
    "DECLARE", "DEFAULT", "DELETE", "DENY", "DESC", "DISK", "DISTINCT", "DISTRIBUTED", "DOUBLE", "DROP", "DUMMY", "DUMP", "ELSE", "END", "ERRLVL",
    "ESCAPE", "ACCESS", "ADD", "ALL", "ALTER", "AND", "ANY", "AS", "ASC", "AUDIT", "BETWEEN", "BY", "CHAR", "CHECK", "CLUSTER", "COLUMN", "COMMENT",
    "COMPRESS", "CONNECT", "CREATE", "CURRENT", "DATE", "DECIMAL", "DEFAULT", "DELETE", "DESC", "DISTINCT", "DROP", "ELSE", "EXCLUSIVE", "EXISTS",
    "FILE", "FLOAT", "FOR", "FROM", "GRANT", "GROUP", "HAVING", "IDENTIFIED", "IMMEDIATE", "IN", "INCREMENT", "INDEX", "INITIAL", "INSERT", "INTEGER",
    "INTERSECT", "INTO", "IS", "LEVEL", "LIKE", "LOCK", "LONG", "MAXEXTENTS", "MINUS", "MLSLABEL", "MODE", "MODIFY", "NOAUDIT", "NOCOMPRESS", "NOT",
    "NOWAIT", "NULL", "NUMBER", "OF", "OFFLINE", "ON", "ONLINE", "OPTION", "OR", "ORDER", "PCTFREE", "PRIOR", "PRIVILEGES", "PUBLIC", "RAW", "RENAME",
    "RESOURCE", "REVOKE", "ROW", "ROWID", "ROWNUM", "ROWS", "SELECT", "SESSION", "SET", "SHARE", "SIZE", "SMALLINT", "START", "SUCCESSFUL", "SYNONYM",
    "SYSDATE", "TABLE", "THEN", "TO", "TRIGGER", "UID", "UNION", "UNIQUE", "UPDATE", "USER", "VALIDATE", "VALUES", "VARCHAR", "VARCHAR2", "VIEW", "WHENEVER",
    "WHERE", "WITH"
]

// 渲染form表单: 一般对form的渲染都统一使用该方法
export const getSearchs = (form, condition, col, isCenter) => {
  const { isFormInit } = form;
  const formParams = form.getFormParams();
  let group = [];
  isFormInit && condition && condition.map(c =>{
    let items = [];
    c.items.map(fields => {
      items.push({
        com:(
          <WeaFormItem
            label={`${fields.label}`} // label 标签的文本
            labelCol={{span: `${fields.labelcol}`}} // label标签占一行比例
            wrapperCol={{span: `${fields.fieldcol}`}} // 右侧控件占一行比例
            error={form.getError(fields)} // 错误提示: 处理表单中有必填项,保存的校验
            tipPosition="bottom" // 错误提示的显示位置: top/bottom
          >
            <WeaSwitch
              fieldConfig={fields}
              form={form}
              formParams={formParams}
            />
          </WeaFormItem>),
        colSpan:1,
      })
    });
    group.push(
      <WeaSearchGroup
        col={col || 1} // 高级搜索列布局列数
        needTigger={true} // 是否开启收缩
        title={c.title || ''} // 高级搜索标题
        showGroup={c.defaultshow} // 是否开启面板
        items={items} // 条目数组数据
        center={isCenter || false} // 内容是否居中：一般弹框需要
      />)
  });
  return group;
}

// 页面加载中效果处理
export const renderLoading = (loading) => (
  <div className="wea-demo-loading">
    <Spin spinning={loading} />
  </div>
)
  
// 无权限处理
export const renderNoright = () => (
  <WeaAlertPage>
    <div>
      {getLabel(2012,'对不起，您暂时没有权限！')}
    </div>
  </WeaAlertPage>
)

// 暂无数据处理
export const renderNoData = () => (
  <WeaAlertPage>
    <div>
      暂无数据
    </div>
  </WeaAlertPage>
)

//对象非空判断
export const isEmpty = (obj) =>{
  for(var n in obj){
return false
}
  return true;
}

/**
 * 添加多级路径
 *
 * @param {*} url
 */
 export const addContentPath = (url) => {
  const ecologyContentPath = window.ecologyContentPath || '';
  if (url && ecologyContentPath) {
    //避免重复添加ecologyContentPath
    //避免传入的参数不是链接
    if (url.startsWith('/') && !url.startsWith(ecologyContentPath)) {
      url = ecologyContentPath + url;
    }
  }
  return url;
};


export const calFormHeight = (groupLength, children) => {
  let childrenLength = 0;
  children.map(c => {
      if (c.colSpan != null) {
          childrenLength += c.colSpan == 2 ? 1 : 2;
      } else
          childrenLength += 1;
  })
  let height = (childrenLength / 2 + childrenLength % 2) * 52 + 10;
  if (groupLength > 1)
      height += groupLength * 45;
  if (height > 300)
      return 300;
  return height;
}

export const validDBKeys = (value) => {
  if (indexOf(dbKeys, value.toUpperCase()) >= 0) {
      message.error("不能使用数据库保留字作为字段名!");
      return {
          isValid: false,
          value: ''
      };
  }
  return {
      isValid: true,
      value
  };
}

//二级路径加前缀
export const getSecondPath=(url)=>{
  const ecologyContentPath = window.ecologyContentPath||"";//二级路径
  if(ecologyContentPath!="" && url && url!=""){
    if(url.startsWith("/")){
      if(!url.startsWith(ecologyContentPath)){
        url = ecologyContentPath+url;
      }
    }else if(url.startsWith("http")){
      let origin = window.location.origin;
      if(!origin){
        origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
      }
      if(url.startsWith(origin) && !url.startsWith(origin+ecologyContentPath)){
        url = url.replace(origin,origin+ecologyContentPath);
      }
    }else {
      url = ecologyContentPath+"/"+url;
    }
  }
  return url;
}