import { useEffect, useState } from "react"
import createReport, { listCommands } from 'docx-templates'
import { IconPlus, IconX } from "@tabler/icons-react"
import { CommandSummary } from "docx-templates/lib/types"
import { Control, Controller, FieldValues, SubmitHandler, useFieldArray, useForm, UseFormRegister } from "react-hook-form"
import { Button, Field, Input, Text, tokens, useId } from "@fluentui/react-components"
import { ArrowUploadRegular } from "@fluentui/react-icons"
const xslTemplate = `
progid="Excel.Sheet"
Регион
Нас. пункт
Котельная
Адрес
Кол-во домов
Этаж.
Общая пл.
Кол-во прожив.
Отопление
ГВС
ХВС
Канализация
Сальдо исход.
Объем
Сумма
Объем
Сумма
Объем
Сумма
Объем
Сумма
222
Регион
Нас. пункт
Котельная
Наименование
Кол-во объектов
Объем здания
Отопление
Подогрев
ГВС
ХВС
Канализация
Итого
Объем
Сумма
Объем
Сумма
Объем
Сумма
Объем
Сумма
Объем
Сумма
`
const handleGenerateExcel = () => {
// Define the example XML data
const xmlData = `
1Region 1City 11010005002001000300150040020005002500300
`
// Parse the XSL template and XML data
const parser = new DOMParser()
const xslDoc = parser.parseFromString(xslTemplate, "application/xml")
const xmlDoc = parser.parseFromString(xmlData, "application/xml")
// Apply the transformation
const xsltProcessor = new XSLTProcessor()
xsltProcessor.importStylesheet(xslDoc)
const resultDocument = xsltProcessor.transformToDocument(xmlDoc)
// Serialize the result to a string
const serializer = new XMLSerializer()
const resultXml = serializer.serializeToString(resultDocument)
// Add missing Excel-specific headers if needed
const correctedXml = `\n` + resultXml
// Convert to Blob and trigger download
const blob = new Blob([correctedXml], { type: "application/vnd.ms-excel" })
const url = URL.createObjectURL(blob)
const link = document.createElement("a")
link.href = url
link.download = "template.xls"
link.click()
URL.revokeObjectURL(url)
}
interface TemplateCommand extends CommandSummary {
children?: CommandSummary[]
}
export function parseCommandList(commands: CommandSummary[]): TemplateCommand[] {
function parseBlock(startIndex: number, currentElement?: string): [TemplateCommand[], number] {
const block: TemplateCommand[] = []
let i = startIndex
while (i < commands.length) {
const command = commands[i]
if (command.type === "FOR") {
const [elementName, , arrayName] = command.code.split(" ")
const forCommand: TemplateCommand = {
raw: command.raw,
type: command.type,
code: arrayName,
children: [],
}
const [children, nextIndex] = parseBlock(i + 1, elementName)
forCommand.children = children
i = nextIndex
block.push(forCommand)
} else if (command.type === "END-FOR") {
return [block, i + 1]
} else {
let code = command.code
if (currentElement && (code.startsWith(`${currentElement}.`) || code.startsWith(`$${currentElement}.`))) {
code = code.replace(`$${currentElement}.`, "").replace(`${currentElement}.`, "")
}
block.push({
...command,
code,
})
i++
}
}
return [block, i]
}
const [parsed] = parseBlock(0)
return parsed
}
const FormLoop = ({
control,
register,
command,
}: {
control: Control,
register: UseFormRegister,
command: TemplateCommand,
}) => {
const { fields, append, remove } = useFieldArray({
name: command.code,
control
})
return (