

/**
* datalist网格表单布局风格1
* @param {*} form 表单配置[{rowspan:2,colspan:3,prop:"fieldName1",label:"字段1"}]
* @param {*} option.hcDataList 数据列表个性化配置
* @param {*} option.row 网格行通用配置
* @param {*} option.cols 网格列配置
* @returns
*/
import { $CONST } from "@birt/funclib/ConstUtil";
import { $HCBIRT } from "@birt/funclib/HCBIRT"

export function formStyle01(form, option = {}) { 
    // let TitleTrStyle = option.titleTrStyle || "background-color:rgb(195,231,248);color:rgb(14,96,137);border: 1px solid #E6E6E6;font-family:Microsoft YaHei UI;font-size:11pt;font-weight:bold;text-indent:10px;"
    let TitleTrStyle = option.titleTrStyle || "font-family:Microsoft YaHei UI;font-size:11pt;font-weight:bold;text-indent:10px;"
    let LabelTdStyle = option.labelTdStyle || "background-color: #f5f3f6;border: 1px solid #dfd9e0;color:#333;font-family: &quot;Microsoft YaHei UI&quot;; font-size: 10pt; text-align: right;padding-right: 12px;"
    let emptyStyle = option.emptyStyle || "background-color: #f5f3f6;border: 1px solid #dfd9e0;"
    // let PropTdStyle = option.propTdStyle || "background-color: #EBEEF5;color:rgb(28,115,153);font-family:Microsoft YaHei UI;font-size:10pt;"
    let PropTdStyle = option.propTdStyle || "color:rgb(28,115,153);border: 1px solid #dfd9e0;font-family:Microsoft YaHei UI;font-size:10pt;"
    // 初始化数据列表配置
    let hcDataList = Object.assign(
        {
            controlName: "HcDataList",
            width: form.width || $HCBIRT.formWidth,
            layout: "form",
            style: "border-spacing:0px;border-collapse: collapse;margin:auto"
        },
        option.hcDataList || {}
    );
    // 初始化cols列配置, 默认6列，700px宽
    let cols = option.cols || [
        {
            colWidth: 120,
        },
        {
            colWidth: "auto",
        },
        {
            colWidth: 120,
        },
        {
            colWidth: "auto",
        },
        {
            colWidth: 120,
        },
        {
            colWidth: "auto",
        },
    ]
    // 初始化变量
    let rows = {};
    let curtRow = newEmptyRow();
    let rIndex = 1;
    let colTotalCount = cols.length;
    let colSpanCount = 0;
    let rowspanTds = {};
    // 计算rows行配置
    function newEmptyRow() {
        // 创建空行
        return Object.assign({ rowHeight: 20, tds: {} }, option.row || {});
    }
    // 获取分隔标题行
    function getSplitTitleTr(rIndex, colspan, splitRow = {}) {
        let splitTr = Object.assign(
            {
                style: 'height: 50px;line-height: 32px;',
                class: 'dataList_title',
                tds: {
                },
            }, splitRow.tr || {});
        let rAxis = "A" + rIndex;
        splitTr.tds[rAxis] = Object.assign(
            {
                value: splitRow.title,
                class: 'formTitle',
                style: TitleTrStyle,
                colspan: colspan,
            }, splitRow.td || {})
        return splitTr;
    }
    // 标签列
    function getLabelTd(fItem, colspan) {
        return {
            value: fItem.label,
            labelProp: fItem.prop,
            style: LabelTdStyle + (fItem.style || ""),
            colspan: colspan || 1,
        }
    }
    // 空格
    function getEmptyTd(colspan) {
        return {
            colspan: colspan || 1,
            style: emptyStyle,
            // style: "background-color: #EBEEF5;"
        }
    }
    // 获取项绑定字段td
    function getPropTd(formItem, rowspan, colspan) {
        return {
            style: PropTdStyle + (formItem.style || ""),
            className: "",
            rowspan: rowspan,
            colspan: colspan,
            children: [
                Object.assign(formItem, {
                    label: ""
                }),
            ],
        }
    }
    // 不足列进行补全
    function fillTd(rIndex, curtColIndex, colCount, curtRow) {
        if (curtColIndex < colCount) {
            for (let m = curtColIndex; m < colCount; m++) {
                let mAxis = String.fromCharCode(65 + m) + rIndex;
                if (!rowspanTds[mAxis]) {
                    curtRow.tds[mAxis] = {
                        colspan: 1
                    }
                }
            }
        }
    }
    // 编辑列添加前置或后置项
    function propTdAppendBrother(tdChildren, fiChildren) {
        let instI = 0;
        fiChildren.forEach(fi => {
            if (fi.slot && fi.slot == $CONST.PREPEND) {
                tdChildren.splice(instI, 0, fi);
                instI++;
            } else {
                tdChildren.push(fi);
            }
        })
    }

    for (let i = 0, l = form.length; i < l; i++) {
        let fItem = form[i];
        let prop = fItem.prop;
        let rowspan = fItem.rowspan || 1;
        let colspan = fItem.colspan || 1;
        let tdAxis = String.fromCharCode(65 + colSpanCount);
        // 记录跨行数大于1的列
        if (rowspan > 1) {
            
            for (let j = rowspan; j > 1; j--) {
                for (let k = 0; k < colspan; k++) {
                    let tdSpanAxis = String.fromCharCode(65 + colSpanCount + k) + (rIndex + j - 1);
                    rowspanTds[tdSpanAxis] = true;
                }
            }
        }
        // 判断当前坐标是否被跨行包含
        let checkAxis = tdAxis + rIndex;
        while (rowspanTds[checkAxis]) {
            colSpanCount++;
            if (colSpanCount > colTotalCount) {
                rIndex++;
                colSpanCount = 0;
            }
            checkAxis = String.fromCharCode(65 + colSpanCount) + rIndex;
        }
        // 获取表单项类型
        let type = fItem.type;
        // 当前项已超本行总列数或当前是行分隔项，补全空余, 别取新行
        if ((colSpanCount + colspan > colTotalCount) || (type == $CONST.SPLITROW)) {
            // 不足列进行补全
            fillTd(rIndex, colSpanCount, colTotalCount, curtRow);
            // 当前列添加到rows,另取新行
            let rAxis = "r" + rIndex;
            rows[rAxis] = JSON.parse(JSON.stringify(curtRow));
            curtRow = newEmptyRow();
            rIndex++;
            colSpanCount = 0;
            tdAxis = String.fromCharCode(65);
            // 添加分隔行
            if (type == $CONST.SPLITROW) {
                let splitTr = getSplitTitleTr(rIndex, colTotalCount, fItem);
                let splitRAxis = "r" + rIndex;
                rows[splitRAxis] = splitTr;
                rIndex++;
                continue;
            }
        }
        // 当前行curtRow添加表单项td
        if (prop) {
            let propAxis = tdAxis + rIndex;
            let propColspan = colspan;
            // 当前有标签配置时，补全标签显示列
            if (fItem.label) {
                curtRow.tds[tdAxis + rIndex] = getLabelTd(fItem, fItem.labelColspan);
                propColspan -= 1;
                propAxis = String.fromCharCode(65 + colSpanCount + 1);
            }
            let propTd = getPropTd(fItem, rowspan, propColspan);
            // 当表单存在children 前置、后置项时添加到添加prop列
            if (fItem.brother) {
                propTdAppendBrother(propTd.children, fItem.brother);
            }
            curtRow.tds[propAxis] = propTd;
        } else {
            if (fItem.label) {
                curtRow.tds[tdAxis + rIndex] = getLabelTd(fItem, colspan);
            } else {
                curtRow.tds[tdAxis + rIndex] = getEmptyTd(colspan);
            }
        }
        colSpanCount += colspan;
    }
    // 添加最后一行
    if (colSpanCount > 0) {
        // 补全最后一行
        fillTd(rIndex, colSpanCount, colTotalCount, curtRow);
        let rAxis = "r" + rIndex;
        rows[rAxis] = JSON.parse(JSON.stringify(curtRow));
    }
    // console.log('rows', rows)
    // console.log('cols', cols)
    hcDataList.rows = rows;
    hcDataList.cols = cols;
    // 返回表单结构
    return hcDataList;
}
