/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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 10:14.
 */
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].queue_num) {
                        if (this.jqueryNode.find("#" + items[i].queue_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{queue_num:"",waitting:"",en_queue_time:"",name:"",sex:"",customLevel:"",businessFlag:""}
             */
            addItem: function (item) {
                var itemNode = $(this.templates[1]).attr({id: item.queue_num});
                itemNode.appendTo(this.jqueryNode);
                this.setCustomAttr(itemNode, item);
                var own = this;
                // 添加调整事件(底层用click)
                itemNode.find("p.custom_queue_footer").on("click", function (e) {
                    var index = own.indexOf(itemNode[0]);
                    var queueNO = itemNode.attr("id");
                    EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, {
                        id: own.id,
                        evtType: EventHub.CLICK,
                        index: index,
                        queue_num: queueNO
                    });
                });
                //添加查看客户事件
                itemNode.find("div[lookOver]").on("click", function (e) {
                    var index = own.indexOf(itemNode[0]);
                    var queueNO = itemNode.attr("id");
                    EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, {
                        id: own.id,
                        evtType: EventHub.LOOK_OVER,
                        index: index,
                        queue_num: queueNO
                    });
                });
            },

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

            /**
             * @item{queue_num:"",waitting:"",en_queue_time:"",name:"",sex:"",customLevel:"",businessFlag:""}
             */
            setItem: function (item) {
                var itemNode = this.jqueryNode.find("#" + item.queue_num);
                this.setCustomAttr(itemNode, item);
            },
            /**
             * @item{queue_num:"",waitting:"",en_queue_time:"",name:"",sex:"",customLevel:"",businessFlag:""}
             */
            setCustomAttr: function (itemNode, item) {
                //设置id、等待时间、取号时间
                itemNode.find(".custom_queue_id").text(item.queue_num);
                itemNode.find(".custom_queue_wait_time").text(item.waitting + "分钟");
                if (item.en_queue_time) {
                    itemNode.find(".custom_queue_get_no_time").text(item.en_queue_time);
                }
                //设置姓名性别
                var customNameNode = itemNode.find(".custom_queue_name");
                if (item.name) {
                    customNameNode.text(item.name);
                }
                if (item.sex) {
                    var customSex = $(this.templates[2]).appendTo(customNameNode);
                    customSex.css({marginTop: "-5px"});
                    if (item.sex === "男" || item.sex === "man" || item.sex === "1") {
                        customSex.attr("src", "resources/images/widget/custom_queue/man.png");
                    } else {
                        customSex.attr("src", "resources/images/widget/custom_queue/woman.png");
                    }
                }
                // 设置客户等级
                if (item.customLevel) {
                    var customLevelNode = itemNode.find(".custom_queue_custom_level");
                    customLevelNode.text("客户等级：" + item.customLevel);
                    var customLevelImg = $(this.templates[2]).appendTo(customLevelNode);
                    if (item.customLevel === "黄金客户") {
                        customLevelImg.attr("src", "resources/images/widget/custom_queue/custom_level/huangjin.png");
                    } else if (item.customLevel === "白金客户") {
                        customLevelImg.attr("src", "resources/images/widget/custom_queue/custom_level/baijin.png");
                    } else if (item.customLevel === "钻石客户") {
                        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.businessFlag) {
                    var imgArr = item.businessFlag.split(",");
                    if (imgArr.length > 0) {
                        var businessFlagNode = itemNode.find(".custom_queue_business_flag");
                        businessFlagNode.find("img").remove();
                        for (var i = 0; i < imgArr.length; i++) {
                            $(this.templates[2]).attr("src", "resources/images/widget/custom_queue/business_flag/" + imgArr[i] + ".png").appendTo(businessFlagNode);
                        }
                    }
                }
            },

            _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();
                    }
                }
            },

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