import { message } from 'antd';
import moment from 'moment';

export const checkParam = (values: any) => {
  var flag = true;

  for (var i in values) {
    // console.log(values[i]);
    if (values[i] == null) {
      flag = false;
    } else {
      if (values[i].length == 0) {
        flag = false;
      }
    }
  }
  return flag;
};

// onBlur={checkInput} maxLength={33}
export const checkInput = (e: any) => {
  // console.log(e)
  if (e.target.value.length == 1) {
    message.error('User Name Should be limit 2~30!', 3);
    e.target.value = null;
  }
};

export const picFromate = (values: any) => {
  var result = new Array();
  if (values != null) {
    values.map((item: any, index: any) => {
      result.push({ url: item });
    });
  }
  return result;
};

export const getCookie = (key: string) => {
  const name = key + '=';
  const ca = document.cookie.split(';');
  for (let i = 0; i < ca.length; i++) {
    const c = ca[i].trim();
    if (c.indexOf(name) === 0) {
      return c.substring(name.length, c.length);
    }
  }
  return '';
};

export const setCookie = (key: string, value: string) => {
  document.cookie = key + '=' + value + '; ';
};

export const RA = (index: any, values: any, module: string, dispatch: any) => {
  dispatch({
    type: module + '/RA',
    playload: { index: index, body: values },
  });
};
export const SA = (values: any, module: string, dispatch: any) => {
  dispatch({ type: module + '/SA', playload: values });
};

export const PAGE = (values: any, module: string, dispatch: any) => {
  dispatch({ type: module + '/PAGE', playload: values });
};

export const URL = (values: any, module: string, dispatch: any) => {
  dispatch({ type: module + '/URL', playload: values });
};

export const URLCLEAR = (module: string, dispatch: any) => {
  dispatch({ type: module + '/URLCLEAR' });
};
export const DataClear = (module: string, dispatch: any) => {
  dispatch({ type: module + '/DataClear' });
};
export const ResultClear = (module: any, dispatch: any) => {
  dispatch({ type: module + '/ResultClear' });
};
// const reg = [["url", "categoriesImageUrl"], ["name", "categoriesName"]]
// const values = [{ categoriesImageUrl: "http://url", categoriesName: "A9" },{ categoriesImageUrl: "http://url...", categoriesName: "A8" }]
export const Fromate = (values: any, reg: any) => {
  var result = new Array();
  if (values != null) {
    values.map((item: any, index: any) => {
      var obj = {};
      for (var i = 0; i < reg.length; i++) {
        if (reg[i][1] == null) {
          obj[reg[i][0]] = item;
        } else {
          obj[reg[i][0]] = item[reg[i][1]];
        }
      }
      result.push(obj);
    });
  }
  return result;
};
// 按键取值
// const reg2 = "name"
// const values2 = [{name:"kat",url:"longtime"},{name:"jack",url:"longtime2"}]
export const Fromate2 = (values: any, reg: string) => {
  var result = new Array();
  values.map((item: any, index: number) => {
    result.push(item[reg]);
  });
  return result;
};

// 获取对象信息
export const getObjectInfo = (test: any) => {
  console.log(JSON.stringify(test, null, '\t'));
  var array = new Array();
  for (let item in test) {
    array.push([item, typeof test[item]]);
  }
  return array;
};
//对象转URL
export const urlEncode = (obj: any) => {
  var url = '?';
  var i = 0;
  for (var item in obj) {
    if (i > 0) {
      url += '&';
    }
    url += item + '=' + obj[item];
    i += 1;
  }
  return url;
};
//对象抽空
export const filterObj = (obj: any) => {
  var tmp = {};
  for (var item in obj) {
    if (obj[item] != null && obj[item] != undefined) {
      tmp[item] = obj[item];
    }
  }
  return tmp;
};
//对象抽空根据关键字
export const filterObjbyTg = (obj: any, tg: any) => {
  var tmp = {};
  for (var item in obj) {
    if (tg.indexOf(item) > -1) {
      if (obj[item] != null) {
        tmp[item] = obj[item];
      }
    }
  }
  return tmp;
};

//提取当中的指定数据类型数据
export const getLevelNum = () => {
  var levelNum = new Array();
  for (var item in treeData) {
    // console.log(parseInt(treeData[item].key))
    if (!isNaN(parseInt(treeData[item].key))) {
      levelNum.push(treeData[item].key);
    } else {
      var tmp = treeData[item];
      tmp.children.map((item, index) => {
        levelNum.push(item.key);
      });
    }
  }
  return levelNum;
};
//转换
export const translate = (from: any, to: any, middle: any) => {
  middle.map((item, index) => {
    from[item[0]] = to[item[1]];
  });
  return from;
};

//用户名检测
export const checkData = (rule: any, value: any, callback: any) => {
  if (value) {
    if (/^[a-zA-Z0-9]+$/g.test(value)) {
      callback();
    } else {
      callback(new Error('*Numbers and letters'));
    }
  }
  callback();
};
//时间转换函数
export const timeForm = (values: any, format: any) => {
  var result = new Array();
  values.map((items: any, index: any) => {
    result.push(moment(items).format(format));
  });
  return result[0] + '-' + result[1];
};
