let util = {};
util.getTimeStamp = function (str) {
    if (str == 'now') {
        let now = new Date().getTime()
        // let now = Date.now();
        return now
    } else if (str == 'last') {
        let now = new Date()
        let year = now.getFullYear(); //当前年份
        let month = now.getMonth() + 1; //当前月份
        let day = now.getDate(); //天
        let hour = now.getHours(); //小时
        let min = now.getMinutes(); //分
        let sec = now.getSeconds(); //秒
        let dateStr = (year - 1) + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec
        let last = new Date(dateStr).getTime()
        return last
    } else if (str == 'next') {
        let now = new Date()
        let year = now.getFullYear(); //当前年份
        let month = now.getMonth() + 1; //当前月份
        let day = now.getDate(); //天
        let hour = now.getHours(); //小时
        let min = now.getMinutes(); //分
        let sec = now.getSeconds(); //秒
        let dateStr = (year + 1) + '-' + month + '-' + day + ' ' + hour + ':' + min + ':' + sec
        let next = new Date(dateStr).getTime()
        return next
    } else if (str == 'nowBegin') {
        let now = new Date()
        let year = now.getFullYear(); //当前年份
        // let month = now.getMonth() + 1; //当前月份
        // let day = now.getDate(); //天
        // let hour = now.getHours(); //小时
        // let min = now.getMinutes(); //分
        // let sec = now.getSeconds(); //秒
        let dateStr = year + '-01-01 00:00:00'
        let nowBegin = new Date(dateStr).getTime()
        return nowBegin
    } else if (str == 'nowEnd') {
        let now = new Date()
        let year = now.getFullYear(); //当前年份
        // let month = now.getMonth() + 1; //当前月份
        // let day = now.getDate(); //天
        // let hour = now.getHours(); //小时
        // let min = now.getMinutes(); //分
        // let sec = now.getSeconds(); //秒
        let dateStr = year + '-12-31 23:59:59'
        let nowEnd = new Date(dateStr).getTime()
        return nowEnd
    } else if (str == 'monthBegin') {
        let now = new Date()
        let year = now.getFullYear(); //当前年份
        let month = now.getMonth() + 1; //当前月份
        let dateStr = year + '-' + month + '-01 00:00:00';
        let monthBegin = new Date(dateStr).getTime();
        return monthBegin;
    }
};
util.getTimeStr = function (str, format) {
    if (!format) {
        format = 'yyyy-M-d';
    }
    if (str == 'now') {
        let now = new Date()
        // let now = Date.now();
        let year = now.getFullYear(); //当前年份
        let month = now.getMonth(); //当前月份-1
        let day = now.getDate(); //天
        let hour = now.getHours(); //小时
        let min = now.getMinutes(); //分
        let sec = now.getSeconds(); //秒
        //  let dateStr = year+'-'+month+'-'+day
        let dateStr = now.format(format);
        return dateStr
    } else if (str == 'last') {
        let now = new Date()
        let year = now.getFullYear(); //当前年份
        let month = now.getMonth(); //当前月份-1
        let day = now.getDate(); //天
        let hour = now.getHours(); //小时
        let min = now.getMinutes(); //分
        let sec = now.getSeconds(); //秒
        //  let dateStr = (year-1)+'-'+month+'-'+day
        let dateStr = new Date(year - 1, month, day).format(format);
        return dateStr
    } else if (str == 'next') {
        let now = new Date()
        let year = now.getFullYear(); //当前年份
        let month = now.getMonth(); //当前月份-1
        let day = now.getDate(); //天
        let hour = now.getHours(); //小时
        let min = now.getMinutes(); //分
        let sec = now.getSeconds(); //秒
        //  let dateStr = (year+1)+'-'+month+'-'+day
        let dateStr = new Date(year + 1, month, day).format(format);
        return dateStr
    } else if (str == 'nowBegin') {
        let now = new Date();
        let year = now.getFullYear(); //当前年份
        // let month = now.getMonth() + 1; //当前月份
        // let day = now.getDate(); //天
        // let hour = now.getHours(); //小时
        // let min = now.getMinutes(); //分
        // let sec = now.getSeconds(); //秒
        // let dateStr = year+'-01-01'
        let dateStr = new Date(year, 0, 1).format(format);
        // console.log(dateStr);
        return dateStr;
    } else if (str == 'nowEnd') {
        let now = new Date()
        let year = now.getFullYear(); //当前年份
        // let month = now.getMonth() + 1; //当前月份
        // let day = now.getDate(); //天
        // let hour = now.getHours(); //小时
        // let min = now.getMinutes(); //分
        // let sec = now.getSeconds(); //秒
        //   let dateStr = year+'-12-31'
        let dateStr = new Date(year, 11, 31).format(format);
        return dateStr
    } else if (str == 'monthBegin') {
        let now = new Date()
        let year = now.getFullYear(); //当前年份
        let month = now.getMonth(); //当前月份-1
        let monthBegin = new Date(year, month, 1).format(format);
        return monthBegin;
    }
};
util.condition_input_initDefaultValue = function (item) {
    let param = {};
    param.id = item.id;
    param.value = item.config.defaultVal;
    param.text = item.config.defaultVal;
    param.type = item.dataType == 'STRING' ? "strInput" : "numInput"
    param.title = item.title;
    param.fuzzySearch = item.config.fuzzySearch;
    param.name = item.config.name;
    param.dataType = item.dataType;
    return param;
};
util.condition_date_initDefaultValue = function (item) {
    let param = {};
    param.id = item.id;
    param.value = util.getTimeStamp(item.config.defaultVal)
    param.text = util.getTimeStr(item.config.defaultVal, item.format && item.format.config ? item.format.config : 'yyyy-M-d');
    param.title = item.title;
    param.name = item.config.name;
    param.type = "date";
    param.dataType = "DATE";
    return param;
};
util.condition_range_initDefaultValue = function (item) {
    if (item.dataType == 'DATE') {
        let param = {
            value: {},
            name: {}
        };
        param.text = util.getTimeStr(item.config.defaultVal.beginDate, item.format && item.format.config ? item.format.config : 'yyyy-M-d') + " - " + this.getTimeStr(item.config.defaultVal.endDate, item.format && item.format.config ? item.format.config : 'yyyy-MM-dd');
        param.id = item.id;
        param.name.min = item.config.names.min;
        param.name.max = item.config.names.max;
        param.title = item.title;
        param.value.min = util.getTimeStamp(item.config.defaultVal.beginDate);
        param.value.max = util.getTimeStamp(item.config.defaultVal.endDate);
        param.type = 'dateRange';
        param.dataType = 'DATE';
        return param;
    } else if (item.dataType == 'NUMBER' || item.dataType == 'STRING') {
        let param = {
            value: {},
            text: {},
            name: {}
        };
        param.name.min = item.config.names.min;
        param.name.max = item.config.names.max;
        param.id = item.id;
        param.title = item.title;
        param.type = item.dataType == 'NUMBER' ? 'numRange' : 'strRange';
        param.dataType = item.dataType;
        param.value.min = item.config.defaultVal.min;
        param.value.max = item.config.defaultVal.max;
        param.text.min = item.config.defaultVal.min;
        param.text.max = item.config.defaultVal.max;
        return param;
    }
};
util.condition_select_initDefaultValue = function (val) {
    let condition = {};
    condition.id = val.id;
    condition.title = val.title;
    condition.text = val.text;
    condition.name = val.name;
    condition.type = val.type;
    condition.dataType = val.dataType;
    condition.isMulti = val.isMulti;
    condition._index = val._index;
    if (val.value != undefined) {
        condition.value = val.value;
    } else {
        return null;
    }
    return condition;
};
util.conditionControls = {
    INPUT: {
        component: 'ConditionInput',
        initDefaultValue: (item) => {
            return this.condition_input_initDefaultValue(item);
        }
    },
    DATE: {
        component: 'ConditionDate',
        initDefaultValue: (item) => {
            return this.condition_date_initDefaultValue(item);
        }
    },
    SELECT: {
        component: 'ConditionSelect',
        initDefaultValue: (item) => {
            return this.condition_select_initDefaultValue(item);
        }
    },
    RANGE: {
        component: 'ConditionRange',
        initDefaultValue: (item) => {
            return this.condition_range_initDefaultValue(item);
        }
    },
    DROPDOWN: {
        component: 'ConditionDropdown',
        initDefaultValue: (item) => {
            return null;
        }
    },
    PERSON: {
        component: 'ConditionPersonDropdown',
        initDefaultValue: (item) => {
            return null;
        }
    },
    ORGPERSON: {
        component: 'ConditionOrgPersonDropdown',
        initDefaultValue: (item) => {
            return null;
        }
    },
    DEPARTMENT: {
        component: 'ConditionDepartmentDropdown',
        initDefaultValue: (item) => {
            return null;
        }
    },
    DANWERTREE: {
        component: 'ConditionOrgDropdown',
        initDefaultValue: (item) => {
            return null;
        }
    },
    DEPOSITARY: {
        component: 'ConditionDepositaryDropdown',
        initDefaultValue: (item) => {
            return null;
        }
    },
    BASEDATA: {
        component: 'ConditionBaseData',
        initDefaultValue: (item) => {
            return null;
        }
    },
    MASTERDATA: {
        component: 'ConditionMasterData',
        initDefaultValue: (item) => {
            return null;
        }
    },
    INPUT_WITH_RANGE: {
        component: 'ConditionInputWithRange',
        initDefaultValue: (item) => {
            return null;
        }
    }
};
util.initCondition = function(val, cType) {
    let condition = {};
    condition.id = val.id;
    condition.name = val.name;
    condition.title = val.title;
    condition.type = val.type;
    condition.dataType = val.dataType;
    condition.text = val.text; //在高级筛选确认时标签展示内容

    if (cType === 'RANGE' && val.isChangeDatasource) {
        condition.isChangeDatasource = val.isChangeDatasource
        condition.suffix = val.suffix
    }
    if (cType === 'BASEDATA' && val.isCountInfo) {
        condition.isCountInfo = val.isCountInfo
    }
    let attrType = {
        INPUT: ['fuzzySearch'],
        DATE: ['isChoseEnum'],
        SELECT: ['isMulti', '_index'],
        RANGE: ['_index', 'isChoseEnum'],
        DROPDOWN: [],
        PERSON: [],
        ORGPERSON: [],
        DEPARTMENT: [],
        DANWERTREE: [],
        DEPOSITARY: [],
        BASEDATA: [],
        MASTERDATA: [],
        INPUT_WITH_RANGE_STR: ['fuzzySearch'],
        INPUT_WITH_RANGE_HAVE: ['_index', 'isChoseEnum']
    }
    attrType[cType] && (attrType[cType].forEach(item => {
        condition[item] = val[item];
    }));
    if (val.value || ['RANGE', 'INPUT_WITH_RANGE_HAVE'].includes(cType)) {
        condition.value = (val.fuzzySearch && cType == 'INPUT') ? ('%' + val.value + '%') : val.value;
    }
    return condition;
}
export default util;