// add tables with row and cell styles

import {
    CreateDocx,
    OptionsCellTable,
    OptionsParagraph,
    OptionsRowTable,
    OptionsTable,
    WordFragment,
} from "../../../build";
import * as fs from "fs";

const docx = new CreateDocx();

// add a table with cell and row styles adding text and WordFragment contents
const optionsCellTable1_1: OptionsCellTable = {
    backgroundColor: "0000FF",
    text: "11",
};
const optionsCellTable1_1_2: OptionsCellTable = {
    backgroundColor: "0000FF",
    text: "12",
};
const optionsCellTable1_3_3: OptionsCellTable = {
    backgroundColor: "00FF00",
    border: {
        top: {
            width: 20,
        },
        right: {
            style: "none",
        },
        bottom: {
            width: 20,
        },
        left: {
            width: 20,
        },
    },
    text: "",
    width: 200,
};
const cellContent_3_4 = new WordFragment();
const optionsParagraph: OptionsParagraph = {
    align: "right",
};
cellContent_3_4.addText("34", optionsParagraph);
const optionsCellTable1_3_4: OptionsCellTable = {
    backgroundColor: "00FF00",
    border: {
        top: {
            width: 20,
        },
        right: {
            color: "FF0000",
            width: 20,
        },
        bottom: {
            width: 20,
        },
        left: {
            style: "none",
        },
    },
    margin: {
        top: 200,
        right: 150,
        bottom: 200,
        left: 150,
    },
    text: cellContent_3_4,
    textDirection: "tbRl",
    wrapText: false,
};
const itemsTable1 = [
    [optionsCellTable1_1, optionsCellTable1_1_2, "13", "14"],
    ["21", "22", "23", "24"],
    ["31", "32", optionsCellTable1_3_3, optionsCellTable1_3_4],
];
const optionsRow1Table: OptionsRowTable = {
    cantSplit: true,
    header: true,
    height: {
        rule: "exact",
        value: 2000,
    },
};
const optionsRow2Table: OptionsRowTable = {};
const optionsRow3Table: OptionsRowTable = {
    cantSplit: true,
    height: {
        rule: "atLeast",
        value: 3000,
    },
};
const optionsTable: OptionsTable = {
    align: "center",
    width: {
        type: "pct",
        value: 80,
    },
};
docx.addTable(
    itemsTable1,
    [optionsRow1Table, optionsRow2Table, optionsRow3Table],
    optionsTable
);

// add a table with colspan and rowspan options
const optionsCellTable2_1_1: OptionsCellTable = {
    vMerge: "restart",
    text: "11",
};
const optionsCellTable2_1_4: OptionsCellTable = {
    vMerge: "restart",
    text: "14",
};
const optionsCellTable2_2_2: OptionsCellTable = {
    gridSpan: 2,
    text: "22",
};
const cellContent_3_4_2 = new WordFragment();
const optionsParagraph2: OptionsParagraph = {
    align: "right",
};
cellContent_3_4_2.addText("Total", optionsParagraph2);
const optionsCellTable2_4_4: OptionsCellTable = {
    backgroundColor: "A9A9A9",
    border: {
        top: {
            color: "FF0000",
            width: 20,
        },
        right: {
            color: "FF0000",
            width: 20,
        },
        bottom: {
            color: "FF0000",
            width: 20,
        },
        left: {
            color: "FF0000",
            width: 20,
        },
    },
    gridSpan: 5,
    margin: {
        top: 200,
        right: 150,
        bottom: 200,
        left: 150,
    },
    text: cellContent_3_4_2,
};
const optionsCellMergeContinue: OptionsCellTable = {
    text: "",
    vMerge: "continue",
};
const itemsTable2 = [
    [optionsCellTable2_1_1, "12", "13", optionsCellTable2_1_4, "15"],
    [
        optionsCellMergeContinue,
        optionsCellTable2_2_2,
        optionsCellMergeContinue,
        "25",
    ],
    [optionsCellTable2_4_4],
];
docx.addTable(itemsTable2);

docx.save().then(contents => {
    fs.writeFileSync("addTable_3.docx", Buffer.from(contents));
});
