const colorMap: Record<string, any> = {
    orange: {
        color: '#d46b08',
        background: '#fff7e6',
        borderColor: '#ffd591',
        fillBackgroundColor: '#FAAD14',
    },
    blue: {
        color: '#0958d9',
        background: '#e6f4ff',
        borderColor: '#91caff',
        fillBackgroundColor: '#1890FF'
    },
    green: {
        color: '#389e0d',
        background: '#f6ffed',
        borderColor: '#b7eb8f',
        fillBackgroundColor: '#52C41A'
    },
    red: {
        color: '#cf1322',
        background: '#fff1f0',
        borderColor: '#ffa39e',
        fillBackgroundColor: '#FF4D4F'
    }
}

Component({
    properties: {
        color: {
            type: String,
            value: ''
        },
        fill: {
            type: Boolean,
            value: false
        },
        propStyle: {
            type: Object,
            value: {}
        }
    },
    data: {
        tagStyle: ''
    },
    lifetimes: {
        attached() {
            const that = this as any;
            let style = ''
            const { color, fill } = that.properties
            if (color && colorMap[color]) {
                const cfg = colorMap[color]
                if (fill) {
                    style = `color:#fff;background:${cfg.fillBackgroundColor};border-color:${cfg.fillBackgroundColor};`
                } else {
                    style = `color:${cfg.color};background:${cfg.background};border-color:${cfg.borderColor};`
                }
            }
            //如果是16进制颜色
            else if (that.isHexColor(color)) {
                if (fill) {
                    style = `color:#fff;background:${color};border-color:${color};`
                } else {
                    style = `color:${color};background:${that.hexToRgba(color, 0.1)};border-color:${color};`
                }
            }
            //默认灰色
            else {
                if (fill) {
                    style = `color:#fff;background:#595959;border-color:#595959;`
                } else {
                    style = `color:#595959;background:#fafafa;border-color:#d9d9d9;`
                }
            }
            that.setData({ tagStyle: style })
        }
    },
    methods: {
        //判断是否是合法16进制颜色
        isHexColor(str: string) {
            return /^#([A-Fa-f0-9]{3}){1,2}$/.test(str)
        },
        //16进制转rgba
        hexToRgba(hex: string, alpha = 1) {
            let r = 0, g = 0, b = 0
            if (hex.length === 4) {
                r = parseInt(hex[1] + hex[1], 16)
                g = parseInt(hex[2] + hex[2], 16)
                b = parseInt(hex[3] + hex[3], 16)
            } else if (hex.length === 7) {
                r = parseInt(hex.slice(1, 3), 16)
                g = parseInt(hex.slice(3, 5), 16)
                b = parseInt(hex.slice(5, 7), 16)
            }
            return `rgba(${r}, ${g}, ${b}, ${alpha})`
        }
    }
})
