/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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/8/18 16:49.
 */
define(["adoreapp/widget/BaseWidget"
    , "adoreapp/widget/EventHub"], function (BaseWidget, EventHub) {
    return BaseWidget.extend(function (base) {
        return {
            _initWidget: function (attrs) {
                var id = this.id;
                this.templates = attrs.template.split("<!-- split -->");
                this.jqueryNode = $(this.templates[0]).attr({id: id});
            },

            _addItems: function (attrs) {
                var items = attrs.items;
                if (!items || items.length === 0) {
                    return;
                }
                for (var i = 0; i < items.length; i++) {
                    if (items[i].win_num) {
                        if (this.jqueryNode.find("#" + items[i].win_num).length !== 0) {
                            this.setItem(items[i]);
                        } else {
                            this.addItem(items[i]);
                        }
                    }
                }
            },

            _setItems: function (attrs) {
                var items = attrs.items;
                this._clear();
                this._addItems({items: items});
            },

            /**
             * @item{win_num:"",status:"",tellerNo:"",tellerName:"",queue_num:"",serviceCustomLevel:"",windowInfo:""}
             */
            addItem: function (item) {
                var itemNode = $(this.templates[1]).attr({id: item.win_num});
                itemNode.appendTo(this.jqueryNode);
                this.setWindowAttr(itemNode, item);
                var own = this;
                //添加查看事件
                itemNode.find("div.window_queue_body").on("click", function (e) {
                    var index = own.indexOf(itemNode[0]);
                    var win_num = itemNode.attr("id");
                    EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, {
                        id: own.id,
                        evtType: EventHub.LOOK_OVER,
                        index: index,
                        win_num: win_num
                    });
                });
            },


            /**
             * @item{win_num:"",status:"",tellerNo:"",tellerName:"",queue_num:"",serviceCustomLevel:"",windowInfo:""}
             */
            setItem: function (item) {
                var itemNode = this.jqueryNode.find("#" + item.win_num);
                this.setWindowAttr(itemNode, item);
            },
            /**
             * @item{win_num:"",status:"",tellerNo:"",tellerName:"",queue_num:"",serviceCustomLevel:"",windowInfo:""}
             */
            setWindowAttr: function (itemNode, item) {
                //设置id、服务状态
                itemNode.find(".window_queue_id")[0].innerHTML = this.templates[3] + item.win_num;
                if (item.status) {
                    var windowQueueBody = itemNode.find(".window_queue_body");
                    windowQueueBody.removeClass("window_queue_body_border_style1").removeClass("window_queue_body_border_style2").removeClass("window_queue_body_border_style3");
                    var statusNode = itemNode.find(".window_queue_status");
                    statusNode.text(item.status);
                    var statusImgNode = $(this.templates[2]).appendTo(statusNode)
                    if (item.status === "空闲") {
                        statusImgNode.attr("src", "resources/images/widget/window_queue/kongxian.png");
                        windowQueueBody.addClass("window_queue_body_border_style1")
                    } else if (item.status === "服务中") {
                        statusImgNode.attr("src", "resources/images/widget/window_queue/fuwuzhong.png");
                        windowQueueBody.addClass("window_queue_body_border_style2")
                    } else if (item.status === "离线") {//离线
                        statusImgNode.attr("src", "resources/images/widget/window_queue/lixian.png");
                        windowQueueBody.addClass("window_queue_body_border_style3")
                    } else {//未知
                        statusImgNode.attr("src", "resources/images/widget/window_queue/weizhi.png");
                        windowQueueBody.addClass("window_queue_body_border_style3")
                    }
                }

                //设置柜员号
                if (item.tellerNo) {
                    itemNode.find(".window_queue_body_teller_no").text("柜员：" + item.tellerNo);
                }
                //设置柜员姓名
                if (item.tellerName) {
                    itemNode.find(".window_queue_body_teller_name").text("姓名：" + item.tellerName);
                }
                // 设置当前服务客户
                if (item.queue_num) {
                    itemNode.find(".window_queue_body_service_custom").text("当前服务客户：" + item.queue_num);
                }
                // 设置当前服务客户等级
                if (item.serviceCustomLevel) {
                    itemNode.find(".window_queue_body_custom_level").text("客户等级：" + item.serviceCustomLevel);
                    var customLevelNode = itemNode.find(".window_queue_body_custom");
                    customLevelNode.find("img").remove();
                    var customLevelImg = $(this.templates[2]).addClass("window_queue_body_custom_level_img").appendTo(customLevelNode);
                    if (item.serviceCustomLevel === "黄金客户") {
                        customLevelImg.attr("src", "resources/images/widget/custom_queue/custom_level/huangjin.png");
                    } else if (item.serviceCustomLevel === "白金客户") {
                        customLevelImg.attr("src", "resources/images/widget/custom_queue/custom_level/baijin.png");
                    } else if (item.serviceCustomLevel === "钻石客户") {
                        customLevelImg.attr("src", "resources/images/widget/custom_queue/custom_level/zuanshi.png");
                    } else {
                        customLevelImg.attr("src", "resources/images/widget/custom_queue/custom_level/putong.png");
                    }
                }
                //设置窗口信息
                if (item.windowInfo) {
                    itemNode.find(".window_queue_body_footer_p").text(item.windowInfo);
                }
            },

            _removeItem: function (attrs) {
                var ids = attrs.itemIds;
                if (ids && ids.length > 0) {
                    for (var i = 0; i < ids.length; i++) {
                        this.jqueryNode.find("#" + ids[i]).remove();
                    }
                }
            },

            indexOf: function (item) {
                var items = this.jqueryNode.find("div.window_queue_container");
                for (var i = 0; i < items.length; i++) {
                    if (item === items[i]) {
                        return i;
                    }
                }
                return -1;
            },

            _clear: function () {
                this.jqueryNode.find(".window_queue_container").remove();
            }
        };
    });
});
