import utils from './utils'
import XEUtils from 'xe-utils'

function delcommafy(num) { //去除千分位中的‘，’
    　　
    if (num && num != 'undefined' && num != 'null') {　　　　
        var numS = num;　　　　
        numS = numS.toString();　　　　
        numS = numS.replace(/,/gi, '');　　　　
        return numS;　　
    } else {　　　　 return num;　　 }
}
var obj = {
    getColNullFn(pid) {
        var cols = []
        for (var i = 0; i < 20; i++) {
            cols.push({
                id: utils.uuid(16),
                title: '',
                align: 'center',
                field: 'col' + i,
                // sortable: false,
                width: 60,
                fontSize: 12,
                fontColor: '#333',
                backgroundColor: '',
                pid: pid,
                clickEvent: [],
                resizable: false
            })
        }
        return cols
    },
    // 数据源参数
    dataSourceColSource(data) {
        return {
            dataType: 'local',
            code: 'name',
            text: 'name',
            data: data || [],
            isReq: true,
            value: {
                name: '',
                code: ''
            }
        }
    },
    // 统一设置权限
    setToken(data, item, _this, global_) {
        if (!item.isToken) {
            return data
        }
        if (!item.tokenParams) {
            if (!item.dsType || item.dsType == '0') {
                item.tokenParams =
                    'token,deptcode,deptid,userId,userCode,roleCode,hospCode,corpCode,orgRang'
            } else {
                item.tokenParams =
                    'token,deptcode,deptid,userId,userCode,roleCode,hospCode,corpCode'
            }
        }
        for (var tp of item.tokenParams.split(',')) {
            if (tp == 'token') {
                data.token = _this.$cookies.get('token')
            } else if (tp == 'deptid') {
                data[tp] = global_.userInfo.deptId
            } else if (tp == 'userCode') {
                data[tp] = global_.userInfo.usercode
            } else if (tp == 'userId') {
                data[tp] = global_.userInfo.id
            } else if (tp == 'corpCode') {
                data[tp] = global_.userInfo.corpcode
            } else if (tp == 'roleCode') {
                data[tp] = global_.userInfo.sysRoleCode
            } else {
                data[tp] = global_.userInfo[tp]
            }
        }
        return data
    },
    // 对象转成&
    objArgs(obj, oldUrl, contentType) {
        var url = '/ts-report/pushData?url='
        if (
            oldUrl &&
            oldUrl.indexOf('http://') != 0 &&
            oldUrl.indexOf('https://') != 0 &&
            oldUrl.indexOf('sso://') != 0 // 非完整的URL，需拼接
        ) {
            oldUrl = location.origin + oldUrl
                // oldUrl = 'http://192.168.10.166:9108/' + oldUrl
        }
        url = url + oldUrl + '?params=app=' + ((!contentType || contentType.indexOf('json') > -1) ? 1 : 2) +
            ';'
        for (var name in obj) {
            if (name != 'orgRang') {
                url += name + '=' + (encodeURIComponent(obj[name]) || '') + ';'; // 转码并进行赋值
            }
        }
        url = url.substring(0, url.length - 1); // 去掉最后一个&符号
        return url; // 返回
    },
    // 设置个别隐私信息
    setUserInfo(url, obj, _this) {
        url =
            url.indexOf('{menu}') > -1 ? url.replace(/{menu}/g, obj.menuId) : url
        url =
            url.indexOf('{token}') > -1 ?
            url.replace(/{token}/g, _this.$cookies.get('token')) :
            url
        url =
            url.indexOf('{dep}') > -1 ?
            url.replace(/{dep}/g, _this.$store.userInfo.deptcode) :
            url
        url =
            url.indexOf('{user}') > -1 ?
            url.replace(/{user}/g, _this.$store.userInfo.usercode) :
            url
        url =
            url.indexOf('{deptId}') > -1 ?
            url.replace(/{deptId}/g, _this.$store.userInfo.deptId) :
            url
        url =
            url.indexOf('{hospCode}') > -1 ?
            url.replace(/{hospCode}/g, _this.$store.userInfo.hospCode) :
            url
        url =
            url.indexOf('{userid}') > -1 ?
            url.replace(/{userid}/g, _this.$store.userInfo.id) :
            url
        url =
            url.indexOf('{ca}') > -1 ?
            url.replace(/{ca}/g, (sessionStorage.getItem('THPS_CA') == '01' ? true : false)) :
            url
            // if (sessionStorage.getItem('THPS_CA') == '01') {
            //     url += (url.indexOf('?') == -1 ? '?' : '&') + 'ca=true'
            // } else {
            //     url += (url.indexOf('?') == -1 ? '?' : '&') + 'ca=false'
            // }
        if (url.indexOf(':sso') == 0 || url.indexOf('sso:') == 0) {
            return url
        }
        if (url.indexOf('?') == -1) {
            url += '?'
        }
        if (obj.menuId) {
            url += '&appid=' + obj.menuId
        }
        if (obj.token) {
            url += '&' + obj.token + '=' + _this.$cookies.get('token')
        }
        if (obj.deptCode) {
            url += '&' + obj.deptCode + '=' + _this.$store.userInfo.deptcode
        }
        if (obj.userCode) {
            url += '&' + obj.userCode + '=' + _this.$store.userInfo.usercode
        }
        url += '&deptid=' + _this.$store.userInfo.deptid
        url += '&userId=' + _this.$store.userInfo.userId
        url += '&roleCode=' + _this.$store.userInfo.sysRoleCode
        url += '&hospCode=' + _this.$store.userInfo.hospCode
        url += '&corpCode=' + _this.$store.userInfo.corpcode
        url += '&orgRang=' + _this.$store.userInfo.orgRang

        if (url.indexOf('?&') > -1) {
            url = url.replace('?&', '?')
        }
        return url
    },
    // 跳转URL，如果是http开头或客户端的跳转到新的页签
    goHrefUrl(global, url) {
        if (global.software == 'bs') {
            url =
                (url.indexOf('?') == -1 ? url + '?' : url + '&') +
                'software=bs'
        }
        if (
            url.indexOf('http://') != -1 ||
            url.indexOf('https://') != -1 ||
            global.software == 'bs'
        ) {
            window.open(url)
        } else {
            if (url.indexOf('#') == -1) {
                url = '#' + url
            }
            location.href = url
        }
    },
    // 将图表数据改变成表格数据
    changeChart2TableData(chartData) {
        var tableData = {}
        var tableDataArr = []
        for (var cd of chartData) {
            if (!tableData[cd.category]) {
                tableData[cd.category] = []
            }
            tableData[cd.category].push(cd)
        }
        for (var td of Object.keys(tableData)) {
            var column0 = tableData[td][0] ? tableData[td][0].column : 0
            var column1 = tableData[td][1] ? tableData[td][1].column : 3000
            var thisItem = (Number(column1) > Number(column0) ? tableData[td][1] : tableData[td][0]) || {}
            var sameItem = (Number(column1) > Number(column0) ? tableData[td][0] : tableData[td][1]) || {}
            var sameVal = sameItem.value
            var thisVal = thisItem.value
                //同比增长率
            var sameDiffVal = Number(thisVal) - Number(sameVal)

            var sameRate = sameVal ? sameDiffVal / Number(sameVal) * 100 : '-'
            if (sameVal == 0) {
                sameRate = 100
            }
            tableDataArr.push({
                category: td,
                sameVal: sameVal,
                thisVal: thisVal,
                sameRate: sameRate,
                sameDiffVal: sameDiffVal,
                chainDiffVal: thisItem.chainDiffVal,
                chainRate: thisItem.chainRate,
                chainVal: thisItem.chainVal
            })
        }
        return tableDataArr
    },
    // 整理千分位
    splitNum(cellValue, dataOf) {
        if (!cellValue) {
            return '-'
        }
        // if (!isNaN(Number(cellValue))) {
        // cellValue = 
        // }
        // if (dataOf && dataOf.indexOf('TIMES') > -1) { //不是金额,去掉小数点
        //     cellValue = cellValue.split('.')[0]
        // }
        return utils.thousandsNum(cellValue)
    },
    // 整理单元格结果处理
    formatFilterCol(filter, cellValue) {
        if (filter == 1) {
            //千分位
            return XEUtils.commafy(parseFloat(cellValue), { digits: 2 })
        } else if (filter == 2) {
            //年月日
            return XEUtils.toDateString(cellValue, 'yyyy-MM-dd')
        } else if (filter == 3) {
            return XEUtils.toDateString(cellValue, 'yyyy-MM-dd HH:mm:ss')
        } else if (filter == 4) {
            return Number(cellValue).toFixed(2)
        } else if (filter == 5) {
            return delcommafy(cellValue)
        }
    },

    //显示增减标识
    formatUpDown(cellValue) {
        if (cellValue == 0 || !cellValue || cellValue == '-' || cellValue == '0%') {
            return cellValue || '-'
        }
        if (cellValue && (cellValue + '').indexOf('-') == 0 && cellValue != '-') {
            // 负数
            return '<div class="green">' + cellValue + '▼ </div>'
        } else {
            return '<div class="red2">' + cellValue + '▲ </div>'
        }
    },
    // 获取边框图
    getBorderTypes(type) {
        var list = []
        for (var i = 1; i < 14; i++) {
            //添加边框
            list.push({
                code: 'dv-border-box-' + i,
                iconfont: 'icon-dv-border-box-' + i,
                color: 'blue' //this.colors[i]
            })
        }
        if (type == 'attrPerson') {
            //添加非大屏边框
            list.push({
                code: 'img_border1',
                iconfont: 'icon-dv-decoration-6',
                color: 'blue' //this.colors[i]
            })
        }

        return list
    },
}
export default obj