/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * Special Declaration: These technical material reserved as the technical secrets by AGREE
 * TECHNOLOGY have been protected by the "Copyright Law" "ordinances on Protection of Computer
 * Software" and other relevant administrative regulations and international treaties. Without
 * the written permission of the Company, no person may use (including but not limited to the
 * illegal copy, distribute, display, image, upload, and download) and disclose the above
 * technical documents to any third party. Otherwise, any infringer shall afford the legal
 * liability to the company.
 */

/**
 * Created by huangdeyun on 2015/3/31.
 */
define(["adoreapp/widget/BaseWidget", "adoreapp/widget/EventHub",
    "adoreapp/widget/constants/ADORE"], function (BaseWidget, EventHub, Adore) {
    return BaseWidget.extend(function (base) {
        return {
            _initWidget: function (attrs) {
                var template = attrs.template;
                this.jqueryNode = $(template).attr("id", this.id);
                this.attachNode = this.jqueryNode;
                // 默认的cellType是None
                this._type = "NONE";
                this.visible = true;
            },

            _setText: function (attrs) {
                this.jqueryNode.text(attrs.text);
            },

            _layout: function () {
                this.append();
                if (this.father) {
                    this.father.refreshColumnsWidth();
                }
            },

            _setType: function (attrs) {
                this._type = attrs.type;
            },

            _setOptions: function (attrs) {
                this._options = attrs.options;
            },

            append: function () {
                if (this.parent) {
                    if (this.father._tHead) {
                        this.jqueryNode.appendTo(this.father._tHead);
                    } else {
                        this.jqueryNode.appendTo(this.father.attachNode);
                    }
                } else {
                    this.jqueryNode.appendTo(document.body);
                }
            },

            _setAlignment: function (attrs) {
                this.alignment = attrs.alignment;
                this.alignmentContent = "left";
                if (attrs.alignment === Adore.RIGHT) {
                    this.alignmentContent = "right";
                } else if (attrs.alignment === Adore.CENTER) {
                    this.alignmentContent = "center";
                }
                this.jqueryNode.css("text-align", this.alignmentContent);
                var index = this.father.columns.indexOf(this);
                var tableItems = this.father._tBody.find("tr");
                for (var i = 0; i < tableItems.length; i++) {
                    $($(tableItems[i]).find("td")[index]).css({"text-align": this.alignmentContent});
                }
            },

            _setVisible: function (attrs) {
                base._setVisible.call(this, attrs);
                this.visible = attrs.visible;
                var index = this.father.columns.indexOf(this);
                var tableItems = this.father._tBody.find("tr");
                var i;
                if (this.visible) {
                    //刷新列可见
                    for (i = 0; i < tableItems.length; i++) {
                        $($(tableItems[i]).find("td")[index]).css({"display": "block"});
                    }
                } else {
                    //刷新列不可见
                    for (i = 0; i < tableItems.length; i++) {
                        $($(tableItems[i]).find("td")[index]).css({"display": "none"});
                    }
                }
                if (this.father) {
                    this.father.refreshColumnsWidth();
                }
            },

            _setEnabled: function (attrs) {
                base._setEnabled.call(this, attrs);
                this.enabled = attrs.enabled;
                if (this._type === "NONE") {
                    return;//NONE类型无需刷新enabled
                }
                var index = this.father.columns.indexOf(this);
                var tableItems = this.father._tBody.find("tr");
                var i;
                var typeCell = "input";
                if (this._type === "BUTTON") {
                    typeCell = "a";
                } else if (this._type === "COMBO") {
                    typeCell = "select";
                }
                if (this.enabled) {
                    //刷新列可用
                    for (i = 0; i < tableItems.length; i++) {
                        $($(tableItems[i]).find("td")[index]).find(typeCell).removeAttr("disabled");
                    }
                } else {
                    //刷新列不不用
                    for (i = 0; i < tableItems.length; i++) {
                        $($(tableItems[i]).find("td")[index]).find(typeCell).attr("disabled", "disabled");
                    }
                }
            },

            _dispose: function () {
                base._dispose.call(this);
                if (this.father) {
                    var index = this.father.columns.indexOf(this);
                    this.father.columns.splice(index, 1);
                }
            }
        };
    });
})