import { message } from 'antd';
import moment from "moment";
import "moment/locale/zh-cn"
//统一处理请求成功、失败

export function handleResponse(res, callback, error) {
    if (res.data.code === 200) {
        callback()
    } else {
        message.warning('操作失败' || res.data.message);
        error();
    }
}

export function pickCheckedTreeIds(data, menuIds = [], resourceIds = []) {
    let arr = data.children;
    if (data.relation === '1') {
        data.softwareMenuId && menuIds.push(data.softwareMenuId);
        data.softwareResourceId && resourceIds.push(data.softwareResourceId);
    }
    if (arr) {
        arr.map((it) => {
            pickCheckedTreeIds(it, menuIds, resourceIds);
        });
    }
    return {
        id: data.softwareId,
        name: data.softwareName,
        softwareMenuIds: menuIds,
        softwareResourceIds: resourceIds
    }
}
// 处理组织结构数据
export function organizationData(arr) {
    arr.forEach(item => {
        item.value = item.hospitalId
        item.title = item.hospitalName
        if (!item.children && item.depts) {
            item.depts.forEach(it => {
                it.value = item.parentId + '-' + it.deptId
            })
            item.children = JSON.parse(JSON.stringify(item.depts).replace(/deptName/g, 'title').replace(/deptId/g, 'value'))
            return
        }
        if (item.children) {
            organizationData(item.children)
        }
    })
    return arr
}

// 去重
export function unique(arr) {
    return arr.filter(function (item, index, arr) {
        return arr.indexOf(item, 0) === index;
    });
}
//时间获取
export function getTimeDetail() {
    const d = new Date();
    const days = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    let sec = d.getSeconds()
    let min = d.getMinutes()
    let hour = d.getHours()
    let str = `${d.getFullYear()}年${d.getMonth() + 1}月${d.getDate()}日 / ${days[d.getDay()]} / ${hour > 9 ? hour : '0' + hour}:${min > 9 ? min : '0' + min}:${sec > 9 ? sec : '0' + sec}`
    return str;
}
export function getValueFromEvent(e) {
    return e.target.value.replace(/(^\s*)|(\s*$)/g, '');
}
//存取cookie
export function setCookie(cname, cvalue) {
    if (!cname) {     //删除cookie
        document.cookie = 'userId=;expires=Thu, 01 Jan 1970 00:00:00 GMT;';
        return;
    }
    const text = cname + "=" + cvalue;
    document.cookie = text;
}
export function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); }
    }
    return "";
}
//从对象数组中遍历出id
export function filterIds(arr, str) {    //arr需遍历的数组；str需拼接的字段数组
    let tempStr = '', temp = '';
    return arr.map((it) => {
        if (str) {
            str.map((t, i) => {
                temp = it[t] || '';
                tempStr = i === 0 ? temp : (tempStr + (tempStr ? "-" + temp : temp));
            });
            return tempStr;
        }
        return it.id + ''
    })
}

export function disabledDate(current){
    return current && current >= moment().endOf('day'); // 选择时间要大于等于当前天。若今天不能被选择，去掉等号即可。
}
