/*
 * autoTemp.Field 扩展
 * Date   : 2020-02-19
 * Version: 2.0
 * author : Lith
 * email  : LithWang@outlook.com
 */
; (function (scope) {



    scope.Field = function (fieldConfig, controller) {
        //   field 对应 ig_id
        //  { field: 'name', title: '装修商', list_width: 200 ,visible:false,editable:false ,'ig_class':'','ig_param':{}  }

        var self = this;

        self.config = fieldConfig;

        // init default config
        (() => {
            let ig_attr = self.config?.ig_attr;
            if (!ig_attr) self.config.ig_attr = ig_attr = {};


            // ig_id
            ig_attr.ig_id = self.config.field;

            // ig_class
            if (!ig_attr.ig_class) ig_attr.ig_class = 'Text';

            // ig_param
            var ig_param = ig_attr.ig_param;
            if (typeof (ig_param) == 'string') {
                try {
                    ig_attr.ig_param = ig_param = eval('(' + ig_param + ')')
                } catch (e) {
                }
            }
            if (!ig_param) ig_attr.ig_param = ig_param = {};
            if (self.config.editable == false) {
                ig_param.disabled = true;
            }

        })();



        self.getVisible = function () {
            let visible = self.config?.visible;
            return visible !== false && visible !== 'false';
        }



        self.getEditable = function () {
            var value = self.config.editable;
            return value != false && value != 'false';
        }




        self.setValue = function (value) {
            var igWidget = getIgWidget();
            if (igWidget) igWidget.setValue(value);
        };



        self.getValue = function () {
            var igWidget = getIgWidget();
            if (igWidget) return igWidget.getValue();
            return null;
        };



        self.list_init = function () {

            let visible = self.config?.list_config?.visible;
            if (visible === false || visible === 'false') return null;

            if (!self.getVisible()) {
                return null;
            }
            //align: "center",
            return {
                field: self.config.field,
                title: self.config.list_config?.title || self.config.title || self.config.field,
                width: self.config.list_config?.width || 80,
                formatter: outSpan,
                sortable: true,
            };
        }



        function getIgWidget() {
            if (!arguments.callee.igWidget) {
                arguments.callee.igWidget = infoGet.getWidgetByIgId(self.config.ig_attr.ig_id);
            }
            return arguments.callee.igWidget;
        }

        self.build_infoGet_Html = function () {
            if (!self.getVisible()) {
                return '';
            }
            let igAttr = self.config.ig_attr;

            var html = "<li><table><tr><td class='mtbTitle'>";
            html += self.config.title || self.config.field;
            html += "</td><td class='mtbValue' >";


            html += infoGet.buildElementHtml(igAttr);
            //html += infoGet.buildHtml(igAttr);
            //html += "<input type='text'  id='" + domId+ "' class='ctl' />";

            html += "</td></tr></table></li>";

            return html;
        }

    };





    scope.Field.getFilter = function (filter, filterAtFields) {
        if (!filterAtFields || filterAtFields.length == 0) return;

        for (var t in filterAtFields) {
            var self = filterAtFields[t];
            var value = self.getValue();
            if (value) {
                filter.push({ field: self.config.field, operator: self.config.filterOpt || "=", value: value });
            }
        }
    };

    scope.Field.getValue = function (atFields, model) {
        if (!atFields || atFields.length == 0) return true;

        for (var t in atFields) {
            var self = atFields[t];

            if (!self.getVisible() || !self.getEditable()) {
                delete model[self.config.field];
                continue;
            }

            var value = self.getValue();
            model[self.config.field] = value;
        }

        return true;
    };


    scope.Field.setValue = function (atFields, model) {
        if (!atFields || atFields.length == 0) return;

        for (var t in atFields) {
            var self = atFields[t];

            var value = model[self.config.field];
            self.setValue(value);
        }
    };


})(autoTemp);