let JV = require('../jpc_value_define');

let JpcFieldHelper = {
    getValue: function(dataField, valueIdx) {
        let rst = "";
        if (dataField && (dataField.length > valueIdx) && (valueIdx >= 0)) {
            rst = dataField[valueIdx];
        }
        return rst;
    },
    decorateValue: function (cell, controls) {
        if (controls) {
            let val = cell[JV.PROP_VALUE];
            let showZero = controls[cell[JV.PROP_CONTROL]][JV.PROP_SHOW_ZERO];
            if (showZero && showZero == 'F' ) {
                if (0.0 == 1.0 * (0 + val)) {
                    cell[JV.PROP_VALUE] = "";
                }
            }
        }
    },
    findAndPutDataFieldIdx: function (rptTpl, tab_fields, rstFields, rstFieldsIdx) {
        if (tab_fields) {
            let detail_fields = rptTpl[JV.NODE_FIELD_MAP][JV.NODE_DETAIL_FIELDS];
            for (let i = 0; i < tab_fields.length; i++) {
                let isFounded = false;
                for (let j = 0; j < detail_fields.length; j++) {
                    if (tab_fields[i]["FieldID"] == detail_fields[j]["ID"]) {
                        isFounded = true;
                        if (rstFields) rstFields.push(tab_fields[i]);
                        if (rstFieldsIdx) rstFieldsIdx.push(j);
                        break;
                    }
                }
                if (!isFounded) {
                    if (rstFields) rstFields.push(tab_fields[i]);
                    if (rstFieldsIdx) rstFieldsIdx.push(JV.BLANK_FIELD_INDEX);
                }
            }
        }
    }
};

module.exports = JpcFieldHelper;
