import xml.etree.ElementTree as ET
from xml.dom import minidom

def create_eci_label_template(output_path):
    report = ET.Element("TfrxReport", {
        "Version": "2022.2.7",
        "DotMatrixReport": "False",
        "IniFile": r"\Software\Fast Reports",
        "PreviewOptions.Buttons": "167935",
        "PreviewOptions.Zoom": "1",
        "PrintOptions.Printer": "Default",
        "PrintOptions.PrintOnSheet": "0",
        "ReportOptions.CreateDate": "46297.0",
        "ReportOptions.Description.Text": "Etiqueta de Bulto ECI (Pedidos Sucursal / Departamento)",
        "ReportOptions.LastChange": "46297.0",
        "ScriptLanguage": "PascalScript",
        "ScriptText.Text": "begin\r\n\r\nend."
    })

    datasets = ET.SubElement(report, "Datasets")
    ET.SubElement(datasets, "item", {
        "DataSet": "frxDBMaster",
        "DataSetName": "frxDBmaster"
    })

    ET.SubElement(report, "TfrxDataPage", {
        "Name": "Data",
        "HGuides.Text": "",
        "VGuides.Text": "",
        "Height": "1000",
        "Left": "0",
        "Top": "0",
        "Width": "1000"
    })

    page = ET.SubElement(report, "TfrxReportPage", {
        "Name": "Page1",
        "PaperWidth": "100",
        "PaperHeight": "150",
        "PaperSize": "256",
        "LeftMargin": "3",
        "RightMargin": "3",
        "TopMargin": "3",
        "BottomMargin": "3",
        "ColumnWidth": "0",
        "ColumnPositions.Text": "",
        "Frame.Typ": "0",
        "MirrorMode": "0"
    })

    # Master Data Band: iterates over records in frxDBmaster
    master_data = ET.SubElement(page, "TfrxMasterData", {
        "Name": "MasterData1",
        "FillType": "ftBrush",
        "FillGap.Top": "0",
        "FillGap.Left": "0",
        "FillGap.Bottom": "0",
        "FillGap.Right": "0",
        "Frame.Typ": "0",
        "Height": "515",
        "Left": "0",
        "Top": "0",
        "Width": "345",
        "DataSet": "frxDBMaster",
        "DataSetName": "frxDBmaster",
        "RowCount": "0"
    })

    # Outer Border Box
    ET.SubElement(master_data, "TfrxShapeView", {
        "Name": "ShapeBorder",
        "AllowVectorExport": "True",
        "Left": "0",
        "Top": "0",
        "Width": "340",
        "Height": "520",
        "Frame.Typ": "15",
        "Frame.Width": "2",
        "Frame.Color": "0"
    })

    # =========================================================================
    # SECTION 1: HEADER (Proveedor / Operador) Top: 0 -> 65
    # =========================================================================
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtProveedorNombre",
        "AllowVectorExport": "True",
        "Left": "8",
        "Top": "6",
        "Width": "324",
        "Height": "16",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-12",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": '[frxDBmaster."proveedor_nombre"]'
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtProveedorOperador",
        "AllowVectorExport": "True",
        "Left": "8",
        "Top": "23",
        "Width": "324",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-10",
        "Font.Name": "Arial",
        "Font.Style": "0",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": '[frxDBmaster."proveedor_operador"]'
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtProveedorDir",
        "AllowVectorExport": "True",
        "Left": "8",
        "Top": "39",
        "Width": "324",
        "Height": "16",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-10",
        "Font.Name": "Arial",
        "Font.Style": "0",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": '[frxDBmaster."proveedor_dir"]'
    })

    # Divider line 1: Y = 60
    ET.SubElement(master_data, "TfrxLineView", {
        "Name": "LineDiv1",
        "AllowVectorExport": "True",
        "Left": "0",
        "Top": "60",
        "Width": "340",
        "Height": "0",
        "Color": "0",
        "Frame.Typ": "4",
        "Frame.Width": "1.5"
    })

    # =========================================================================
    # SECTION 2: LUGAR DE ENTREGA & Nº DE BULTOS (Y: 60 -> 125)
    # =========================================================================
    # Vertical divider left col: X = 72 (from Y=60 to 125)
    ET.SubElement(master_data, "TfrxLineView", {
        "Name": "LineVEntrega1",
        "AllowVectorExport": "True",
        "Left": "72",
        "Top": "60",
        "Width": "0",
        "Height": "65",
        "Color": "0",
        "Frame.Typ": "1",
        "Frame.Width": "1.2"
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "LblLugarEntrega",
        "AllowVectorExport": "True",
        "Left": "2",
        "Top": "74",
        "Width": "68",
        "Height": "36",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-10",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "HAlign": "haCenter",
        "VAlign": "vaCenter",
        "ParentFont": "False",
        "Text": "LUGAR DE\r\nENTREGA"
    })

    # Middle: Lugar entrega details (X = 76 to 246)
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtEntregaNom",
        "AllowVectorExport": "True",
        "Left": "76",
        "Top": "64",
        "Width": "168",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-10",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": '[frxDBmaster."lugar_entrega_linea1"]'
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtEntregaCentro",
        "AllowVectorExport": "True",
        "Left": "76",
        "Top": "79",
        "Width": "168",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-10",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": '[frxDBmaster."lugar_entrega_linea2"]'
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtEntregaDir",
        "AllowVectorExport": "True",
        "Left": "76",
        "Top": "94",
        "Width": "168",
        "Height": "26",
        "StretchMode": "smActualHeight",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-9",
        "Font.Name": "Arial",
        "Font.Style": "0",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": '[frxDBmaster."lugar_entrega_linea3"]'
    })

    # Vertical divider right col: X = 248 (from Y=60 to 125)
    ET.SubElement(master_data, "TfrxLineView", {
        "Name": "LineVBultos",
        "AllowVectorExport": "True",
        "Left": "248",
        "Top": "60",
        "Width": "0",
        "Height": "65",
        "Color": "0",
        "Frame.Typ": "1",
        "Frame.Width": "1.2"
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "LblNoBultos",
        "AllowVectorExport": "True",
        "Left": "250",
        "Top": "64",
        "Width": "88",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-9",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "HAlign": "haCenter",
        "ParentFont": "False",
        "Text": "Nº DE BULTOS"
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtBultoTexto",
        "AllowVectorExport": "True",
        "Left": "250",
        "Top": "80",
        "Width": "88",
        "Height": "38",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-19",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "HAlign": "haCenter",
        "VAlign": "vaCenter",
        "ParentFont": "False",
        "Text": '[frxDBmaster."bulto_texto"]'
    })

    # Divider line 2: Y = 125
    ET.SubElement(master_data, "TfrxLineView", {
        "Name": "LineDiv2",
        "AllowVectorExport": "True",
        "Left": "0",
        "Top": "125",
        "Width": "340",
        "Height": "0",
        "Color": "0",
        "Frame.Typ": "4",
        "Frame.Width": "1.5"
    })

    # =========================================================================
    # SECTION 3: DESTINO MERCANCÍA & SUCURSAL & IA (91) (Y: 125 -> 195)
    # =========================================================================
    # Vertical divider left col: X = 72 (from Y=125 to 195)
    ET.SubElement(master_data, "TfrxLineView", {
        "Name": "LineVDest1",
        "AllowVectorExport": "True",
        "Left": "72",
        "Top": "125",
        "Width": "0",
        "Height": "70",
        "Color": "0",
        "Frame.Typ": "1",
        "Frame.Width": "1.2"
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "LblDestinoMerc",
        "AllowVectorExport": "True",
        "Left": "2",
        "Top": "138",
        "Width": "68",
        "Height": "36",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-10",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "HAlign": "haCenter",
        "VAlign": "vaCenter",
        "ParentFont": "False",
        "Text": "DESTINO\r\nMERCANCÍA"
    })

    # Middle: Destino details (X = 76 to 180)
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtDestinoNom",
        "AllowVectorExport": "True",
        "Left": "76",
        "Top": "129",
        "Width": "104",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-10",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": '[frxDBmaster."destino_linea1"]'
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtDestinoSucursalNom",
        "AllowVectorExport": "True",
        "Left": "76",
        "Top": "144",
        "Width": "104",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-10",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": '[frxDBmaster."destino_linea2"]'
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtDestinoDir",
        "AllowVectorExport": "True",
        "Left": "76",
        "Top": "159",
        "Width": "104",
        "Height": "30",
        "StretchMode": "smActualHeight",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-9",
        "Font.Name": "Arial",
        "Font.Style": "0",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": '[frxDBmaster."destino_linea3"]'
    })

    # Vertical divider sucursal: X = 182 (from Y=125 to 195)
    ET.SubElement(master_data, "TfrxLineView", {
        "Name": "LineVSucursal",
        "AllowVectorExport": "True",
        "Left": "182",
        "Top": "125",
        "Width": "0",
        "Height": "70",
        "Color": "0",
        "Frame.Typ": "1",
        "Frame.Width": "1.2"
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtSucursalNum",
        "AllowVectorExport": "True",
        "Left": "184",
        "Top": "135",
        "Width": "58",
        "Height": "48",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-21",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "HAlign": "haCenter",
        "VAlign": "vaCenter",
        "ParentFont": "False",
        "Text": '[frxDBmaster."sucursal_codigo"]'
    })

    # Vertical divider barcode 91: X = 244 (from Y=125 to 195)
    ET.SubElement(master_data, "TfrxLineView", {
        "Name": "LineVBar91",
        "AllowVectorExport": "True",
        "Left": "244",
        "Top": "125",
        "Width": "0",
        "Height": "70",
        "Color": "0",
        "Frame.Typ": "1",
        "Frame.Width": "1.2"
    })
    # Barcode IA (91)
    ET.SubElement(master_data, "TfrxBarCodeView", {
        "Name": "BarCodeIA91",
        "AllowVectorExport": "True",
        "Left": "248",
        "Top": "130",
        "Width": "88",
        "Height": "46",
        "BarType": "bcGS1Code128",
        "DataField": "barcode_ia91",
        "DataSet": "frxDBMaster",
        "DataSetName": "frxDBmaster",
        "Expression": '<frxDBmaster."barcode_ia91">',
        "ShowText": "False",
        "Zoom": "1"
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtIA91Human",
        "AllowVectorExport": "True",
        "Left": "246",
        "Top": "178",
        "Width": "92",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-9",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "HAlign": "haCenter",
        "ParentFont": "False",
        "Text": '[frxDBmaster."barcode_ia91"]'
    })

    # Divider line 3: Y = 195
    ET.SubElement(master_data, "TfrxLineView", {
        "Name": "LineDiv3",
        "AllowVectorExport": "True",
        "Left": "0",
        "Top": "195",
        "Width": "340",
        "Height": "0",
        "Color": "0",
        "Frame.Typ": "4",
        "Frame.Width": "1.5"
    })

    # =========================================================================
    # SECTION 4: DATOS ASOCIADOS A LA ENTREGA & IA (98) (Y: 195 -> 275)
    # =========================================================================
    # Vertical divider left col: X = 72 (from Y=195 to 275)
    ET.SubElement(master_data, "TfrxLineView", {
        "Name": "LineVDatos1",
        "AllowVectorExport": "True",
        "Left": "72",
        "Top": "195",
        "Width": "0",
        "Height": "80",
        "Color": "0",
        "Frame.Typ": "1",
        "Frame.Width": "1.2"
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "LblDatosEntrega",
        "AllowVectorExport": "True",
        "Left": "2",
        "Top": "205",
        "Width": "68",
        "Height": "60",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-9",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "HAlign": "haCenter",
        "VAlign": "vaCenter",
        "ParentFont": "False",
        "Text": "DATOS\r\nASOCIADOS\r\nA LA\r\nENTREGA"
    })

    # Middle: DPTO-PV, PEDIDO, ALBARAN (X = 76 to 198)
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "LblDptoPv",
        "AllowVectorExport": "True",
        "Left": "76",
        "Top": "203",
        "Width": "80",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-9",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": "DPTO.-P.VENTA:"
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtDptoPv",
        "AllowVectorExport": "True",
        "Left": "158",
        "Top": "203",
        "Width": "42",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-9",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": '[frxDBmaster."dpto_pventa"]'
    })

    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "LblPedido",
        "AllowVectorExport": "True",
        "Left": "76",
        "Top": "228",
        "Width": "50",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-9",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": "PEDIDO:"
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtPedido",
        "AllowVectorExport": "True",
        "Left": "128",
        "Top": "228",
        "Width": "72",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-10",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": '[frxDBmaster."pedido_num"]'
    })

    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "LblAlbaran",
        "AllowVectorExport": "True",
        "Left": "76",
        "Top": "252",
        "Width": "50",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-9",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": "ALBARÁN:"
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtAlbaran",
        "AllowVectorExport": "True",
        "Left": "128",
        "Top": "252",
        "Width": "72",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-10",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": '[frxDBmaster."albaran_num"]'
    })

    # Vertical divider barcode 98: X = 202 (from Y=195 to 275)
    ET.SubElement(master_data, "TfrxLineView", {
        "Name": "LineVBar98",
        "AllowVectorExport": "True",
        "Left": "202",
        "Top": "195",
        "Width": "0",
        "Height": "80",
        "Color": "0",
        "Frame.Typ": "1",
        "Frame.Width": "1.2"
    })
    # Barcode IA (98)
    ET.SubElement(master_data, "TfrxBarCodeView", {
        "Name": "BarCodeIA98",
        "AllowVectorExport": "True",
        "Left": "206",
        "Top": "201",
        "Width": "130",
        "Height": "54",
        "BarType": "bcGS1Code128",
        "DataField": "barcode_ia98",
        "DataSet": "frxDBMaster",
        "DataSetName": "frxDBmaster",
        "Expression": '<frxDBmaster."barcode_ia98">',
        "ShowText": "False",
        "Zoom": "1"
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtIA98Human",
        "AllowVectorExport": "True",
        "Left": "204",
        "Top": "258",
        "Width": "134",
        "Height": "14",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-8",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "HAlign": "haCenter",
        "ParentFont": "False",
        "Text": '[frxDBmaster."barcode_ia98"]'
    })

    # Divider line 4: Y = 275
    ET.SubElement(master_data, "TfrxLineView", {
        "Name": "LineDiv4",
        "AllowVectorExport": "True",
        "Left": "0",
        "Top": "275",
        "Width": "340",
        "Height": "0",
        "Color": "0",
        "Frame.Typ": "4",
        "Frame.Width": "1.5"
    })

    # =========================================================================
    # SECTION 5: S.S.C.C. & IA (00) (Y: 275 -> 520)
    # Height of section: 245 units (~ 65 mm)
    # Barcode height: 165 units (~ 43.6 mm > 32 mm minimum required by ECI!)
    # =========================================================================
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "LblSSCC",
        "AllowVectorExport": "True",
        "Left": "8",
        "Top": "280",
        "Width": "70",
        "Height": "16",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-10",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "ParentFont": "False",
        "Text": "S.S.C.C."
    })

    ET.SubElement(master_data, "TfrxBarCodeView", {
        "Name": "BarCodeSSCC",
        "AllowVectorExport": "True",
        "Left": "20",
        "Top": "304",
        "Width": "300",
        "Height": "165",
        "BarType": "bcGS1Code128",
        "DataField": "barcode_ia00",
        "DataSet": "frxDBMaster",
        "DataSetName": "frxDBmaster",
        "Expression": '<frxDBmaster."barcode_ia00">',
        "ShowText": "False",
        "Zoom": "1"
    })
    ET.SubElement(master_data, "TfrxMemoView", {
        "Name": "TxtSSCCHuman",
        "AllowVectorExport": "True",
        "Left": "20",
        "Top": "475",
        "Width": "300",
        "Height": "20",
        "Font.Charset": "1",
        "Font.Color": "0",
        "Font.Height": "-13",
        "Font.Name": "Arial",
        "Font.Style": "1",
        "Frame.Typ": "0",
        "HAlign": "haCenter",
        "ParentFont": "False",
        "Text": '[frxDBmaster."barcode_ia00"]'
    })

    # Format XML nicely
    xml_str = ET.tostring(report, encoding="utf-8")
    parsed = minidom.parseString(xml_str)
    pretty = parsed.toprettyxml(indent="  ", encoding="utf-8")

    with open(output_path, "wb") as f:
        f.write(pretty)
    print(f"Created template at {output_path}")

create_eci_label_template(r"c:\fuentes\proyectos delphi\RAUL_ASENCIO\GESTION\reportes\EtiquetaBultoECI.fr3")
