
var excel = _xls.create()

excel.sheet().setColumnWidth(1, 256 * 20)
excel.sheet().setColumnWidth(2, 256 * 20)

const fontTitle = excel.workbook.createFont()
fontTitle.setBold(true)
fontTitle.setFontHeightInPoints(14)
fontTitle.setColor(_xls.color("white"))

const styleHeader = excel.workbook.createCellStyle()
styleHeader.setFillPattern(_xls.fillPattern("solid-foreground"))
styleHeader.setFillBackgroundColor(_xls.color("black"))
styleHeader.setAlignment(_xls.horizontalAlignment("center"))
styleHeader.setFont(fontTitle)

const styleData = excel.workbook.createCellStyle()
styleData.setBorderTop(_xls.borderStyle("double"))
styleData.setBorderBottom(_xls.borderStyle("double"))
styleData.setBorderLeft(_xls.borderStyle("double"))
styleData.setBorderRight(_xls.borderStyle("double"))

const header = _val.list()
    .add(
        _val.map()
            .set("value", "Data")
            .set("style", styleHeader)
    )
    .add(
        _val.map()
            .set("value", "Quantidade")
            .set("style", styleHeader)
    )

//_req.getString("inicio")
//_req.getString("fim")

const dbPublicacoes = _db.query(`
    SELECT momento::DATE as data,
        count(id) as total
    FROM publicacao
    GROUP BY data
    ORDER BY data desc
`)

const data = _val.list()

for (const dbPublicacao of dbPublicacoes) {
    data.add(
        _val.list()
            .add(
                _val.map()
                    .set("value", dbPublicacao.getString("data"))
                    .set("style", styleData)
            )
            .add(
                _val.map()
                    .set("value", dbPublicacao.getString("total"))
                    .set("style", styleData)
            )
    )
}

excel.addDataTable(1, 1, header)

excel.addDataTable(2, 1, data)

excel.output("publicacoces-totais.xls")
