import Qs from 'qs';
import {post, get, upload} from './ajax';
import queue from './queue';
import axios from 'axios';
import cookie from './cookie';
import httpErrorHandler from './httpErrorHandler';

/**
 * 获取参数的类型
 *
 * @param {any} data
 * @returns {string} 参数类型
 */
export const type = (data) => {
  return Object.prototype.toString.call(data).replace(/(\[object |\])/g, '')
};

/**
 * 获取登录状态
 * 
 * @returns 
 */
export const getLoginStatus = () => {
  return window.sessionStorage.isLogin;
}

/**
 * 解析url参数
 *
 * @returns {object} localtion的query对象
 */
export const query = () => {
  return Qs.parse(window.location.href.split('?')[1])
}


/**
 * 编码url参数 
 *
 * @param {object} data 参数对象
 * @returns string
 */
export const encodeURI = (data) => {
  return Qs.stringify(data, {arrayFormat: 'repeat', allowDots: true});
}

/**
 * formatterDate
 *
 * @param {*} time 需要处理的时间
 * @returns {string} val 解析后的结果
 */
export function formatterDate(time) {
  if(!time) return '';
  let date = new Date(Number(time));
  let Y = date.getFullYear() + '-';
  let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  let D = panLeft(date.getDate()) + ' ';
  let h = panLeft(date.getHours()) + ':';
  let m = panLeft(date.getMinutes()) + ':';
  let s = panLeft(date.getSeconds());
  return Y+M+D+h+m+s;
};
/**
 * formatterDate
 *
 * @param {*} time 需要处理的时间串
 * @returns {string} val 解析后的结果 yyyy-MM-dd
 */
export function formatterDateDay(datetime) {
    if (!datetime) return "";
    if (datetime instanceof Date) {
        let Y = datetime.getFullYear() + "-";
        let M =
            (datetime.getMonth() + 1 < 10
                ? "0" + (datetime.getMonth() + 1)
                : datetime.getMonth() + 1) + "-";
        let D = panLeft(datetime.getDate());
        return Y + M + D;
    } else {
        let transformDate = new Date(Number(datetime));
        let Y = transformDate.getFullYear() + "-";
        let M =
            (transformDate.getMonth() + 1 < 10
                ? "0" + (transformDate.getMonth() + 1)
                : transformDate.getMonth() + 1) + "-";
        let D = panLeft(transformDate.getDate());
        return Y + M + D;
    }
    return datetime;
};
/**
 * 当前日期加天数后得到的相应日期
 * @param {*} day 
 */
export function getAddDay(day){
  var today = new Date();
  var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
  today.setTime(targetday_milliseconds); 
  var tYear = today.getFullYear();
  var tMonth = today.getMonth();
  var tDate = today.getDate();
  tMonth = doHandleMonth(tMonth + 1);
  tDate = doHandleMonth(tDate);
  return tYear+""+tMonth+""+tDate;
}

function doHandleMonth(month){
  var m = month;
  if(month.toString().length == 1){
    m = "0" + month;
  }
  return m;
}

function panLeft(num){
  return num < 10 ? '0'+num : num;
}


/**
 * call方法
 *
 * @param {Function} callMethod 调用的http包装方法
 * @param {String} url 调用接口
 * @param {Object} formData 需要发送参数
 * @param {Object} [config] 配置
 * @returns {Promise} axios请求
 */
const call = (callMethod, url, formData, config = {}) => {
  return new Promise((resolve, reject)=>{
    callMethod(url, formData, config).then( res =>{
      const {code, msg, data} = res;
      if(code !== 1){
        reject({
          message: msg || ''
        });
        return;
      }
      resolve(res);
    })
    .catch(error=>{
      if(error.status === 403) {
        window.location.href = '/#/login'
      }
      reject(error);
    })
  })
};

/**
 * 普通的ajax post请求
 *
 * @param {Object} formData  需要发送参数
 * @param {String} url 调用接口
 * @param {Object} [config] 配置
 * @returns 科里化后的call方法
 */
export const normalCallPost = (url, formData, config = {}) => {
  return call(post, url, formData, config);
};

/**
 * 普通的ajax get请求
 *
 * @param {Object} formData  需要发送参数
 * @param {String} url 调用接口
 * @param {Object} [config] 配置
 * @returns 科里化后的call方法
 */
export const normalCallGet = (url, formData, config= {}) => {
  return call(get, url, formData, config);
};

const mimeMap = {
  xlsx: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  zip: 'application/zip',
  doc:'application/msword'
}

/**
 * 普通的ajax download请求
 *
 * @param {Object} formData  需要发送参数
 * @param {String} url 调用接口
 * @param {Object} [config] 配置
 * @returns {Promise<blob>}
 */
export const download = (url, formData, config= {}) => {
  return new Promise(async (resolve, reject)=>{
    const option = Object.assign({}, config, {
      responseType:'blob'
    })
    try {
      const data = await post(url, formData, option)
      const link = document.createElement('a')
      let blob
      let extName
      if(option.type=="zip"){
         blob = new Blob([data], { type: mimeMap.zip })
         extName="zip"
      }else if(option.type=="excel"){
        blob = new Blob([data], { type: mimeMap.xlsx })
        extName="xlsx"
      }else if(option.type=="doc"){
        blob = new Blob([data], { type: mimeMap.doc })
        extName="doc"
      }else{
        extName="xlsx"
      }

      link.href = URL.createObjectURL(blob)
      link.setAttribute('download', `${url.substr(1).replace(/\//g, '_')}_${new Date().getTime()}.${extName}`) // 设置下载文件名称
      document.body.appendChild(link)
      link.click()
      document.body.appendChild(link)

      resolve();
    } catch (error) {
      reject(error);
    }
  })
};


export const downloadWithCustName = (url, formData, config= {}) => {
  return new Promise(async (resolve, reject)=>{
    const option = Object.assign({}, config, {
      responseType:'blob',
      baseURL: '/m',
      headers: {
        post: {
          'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        }
      }
    })
    try {
    	const params = Object.assign({}, formData, {
    	    __mortals_token__: cookie.getItem('__mortals_token__'),
    	})
    	var requestData = Qs.stringify(params, {arrayFormat: 'repeat', allowDots: true});
		axios.post(url, requestData, option)
	    .then(response => {
	    	if (response.data.type == 'application/json'){
	    		var reader = new FileReader();
	    		reader.onload = function(event) {
	    			var content = reader.result;
	    			reject(content);
	    		};
	    		reader.readAsText(response.data);
	    		return;
	    	}
	    	const filename = decodeURI(response.headers['content-disposition'].split(';')[1].split('=')[1]) || `${url.substr(1).replace(/\//g, '_')}_${new Date().getTime()}.xls`
	        let downloadUrl = window.URL.createObjectURL(new Blob([response.data]))
	        let link = document.createElement('a')
	        link.style.display ='none';
	        link.href = downloadUrl;
	        link.setAttribute('download', filename);
	        document.body.appendChild(link);
	        link.click();
	        document.body.removeChild(link);
	        resolve();
	    }, err => {
	        reject(err);
	    }).catch((error) => {
	        reject(error)
	    })
    } catch (error) {
      reject(error);
    }
  })
};

/**
 * 普通的ajax upload请求
 *
 * @param {Object} formData  需要发送参数
 * @param {String} url 调用接口
 * @param {Object} [config] 配置
 * @returns 科里化后的call方法
 */
export const normalCallUpload = (url, formData, config= {}) => {
  return call(upload, url, formData, config);
};

/**
 * 使用队列的ajax请求，后自动cancel前一个请求
 *
 * @param {Object} formData  需要发送参数
 * @param {String} url 调用接口
 * @param {Object} [config] 配置
 * @returns  科里化后的call方法
 */
export const queueCall = (url, formData, config = {}) => {
  return call(queue, url, formData, config);
};

/**
 * 根据条件查询用户列表
 */
export async function getUserListByQuery(query) {
    try {
       /* //下拉只获取正常状态数据：status{0: "停用", 1: "正常", 2: "冻结", 3: "销户", 4: "离职"}
        query["query.status"] = 1;*/
        const data = await normalCallPost('/user/list', query);
        const list = data.data.data.map(({id,loginName, realName,mobile})=>{
            return {id,loginName, realName,mobile}
        });
        return list;
    } catch (error) {
        return [];
    }
};


/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 * @param {*} rootId 根Id 默认 0
 */
 export function handleTree(data, id, parentId, children, rootId) {
   console.log("00000000")
	id = id || 'id'
	parentId = parentId || 'parentId'
	children = children || 'children'
	rootId = rootId || Math.min.apply(Math, data.map(item => { return item[parentId] })) || 0
	//对源数据深度克隆
	const cloneData = JSON.parse(JSON.stringify(data))
	//循环所有项
  console.log("11111",cloneData)
	const treeData = cloneData.filter(father => {
		let branchArr = cloneData.filter(child => {
			//返回每一项的子级数组
			return father[id] === child[parentId]
		});
		branchArr.length > 0 ? father.children = branchArr : '';
		//返回第一层
		return father[parentId] === rootId;
	});
  console.log("treeData",treeData)
	return treeData != '' ? treeData : data;
};


