import _ from 'lodash'

/**
 * 子表赋值 用于清空某(些)子表某(些)字段值
 * 公式说明 此赋值公式会无条件触发关联字段的所有公式 对于字段非空的校验 应当写在配置前端公式的条件中
 * @author fengjiansheng
 * @data 2021-08-26
 */
export default {
    execute: function () {
        console.log("s1 ClearSubFields")
        if (arguments && arguments[0] && arguments[0].length > 0 && arguments[1]) {
            // TODO 参数校验
            let context = arguments[1]
            for (let [index, targetSubField] of arguments[0].entries()) {
                if (index < 0) continue
                let tempField = targetSubField.FieldCode
                let subTableObject = context.getTableType(targetSubField.FieldTableCode)
                let subIndex = context.getSubFocus()
                let focusData = context.getAllSubData()[subTableObject.title][subIndex].getData()
                if ((!_.isEqual(focusData[tempField]._field_old_value_, focusData[tempField].value) || !_.isEmpty(focusData[tempField].value))) {
                    focusData[tempField].value = null
                    focusData[tempField]._field_old_value_ = null
                }
                context.refreshSubDataRow(subTableObject.title)
            }
        }
    },
    getResultType: function () {
        return FMR.ConstDataTypes.Void;
    }
}
