/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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 daikuan on 2015/8/20 14:48
 */
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});
            },

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

            /**
             * @item{id:date+"k"+appointmentTime.split(":")[0]+"k"+phoneNum,mediumType:"",mediumNum:"",phoneNum:"",appointmentTime:""}
             */
            addItem: function (item) {
                var itemNode = $(this.templates[1]).attr({id: item.id});
                itemNode.appendTo(this.jqueryNode);
                this.setWindowAttr(itemNode, item);
                var own = this;
                itemNode.find(".appointment_queue_delete").on("click",function(e) {
                        var id_str = item.id.split("k");
                        id_str[1] = id_str[1] + ":00";
                         EventHub.dispatchEvent(EventHub.MSG_TYPE.WIDGET, {
                            id: own.id,
                            removeId: item.id,
                            removeDate: id_str[0],
                            removeTime: id_str[1],
                            evtType: EventHub.MODIFY
                         });
                });
            },


            /**
             * @item{id:"",mediumType:"",mediumNum:"",phoneNum:"",appointmentTime:""}
             */
            setItem: function (item) {
                var itemNode = this.jqueryNode.find("#" + item.id);
                this.setWindowAttr(itemNode, item);
            },
            /**
             * @item{id:"",mediumType:"",mediumNum:"",phoneNum:"",appointmentTime:""}
             */
            setWindowAttr: function (itemNode, item) {
                //设置id
                if(item.id){
                    var idNode = itemNode.find(".appointment_queue_id");
                    idNode.text("预约编号："+item.id);

                }

                //设置介质类型
                if (item.mediumType) {
                    var mtNode = itemNode.find(".appointment_queue_body_mediumtype");
                    mtNode.text("介质类型：" + item.mediumType);
                    var mtImg = $(this.templates[2]).appendTo(mtNode);
                    mtImg.attr("src", "resources/images/widget/appointment_queue/cardType.png");
                }
                //设置介质号码
                if (item.mediumNum) {
                    var mnNode = itemNode.find(".appointment_queue_body_mediumnum");
                    var begin_mediumNum = item.mediumNum.substring(0,6);
                    var end_mediumNum = item.mediumNum.substring(11);
                    var new_mediumNum = begin_mediumNum + "*****" + end_mediumNum
                    mnNode.text("介质号码：" + new_mediumNum);
                    var mnImg = $(this.templates[2]).appendTo(mnNode);
                    mnImg.attr("src", "resources/images/widget/appointment_queue/jizhiNum.png");
                }
                //设置手机号
                if (item.phoneNum) {
                    var pnNode = itemNode.find(".appointment_queue_body_phonenum");
                    var begin_phoneNum = item.phoneNum.substring(0,3);
                    var end_phoneNum = item.phoneNum.substring(7);
                    var new_phoneNum = begin_phoneNum + "****" + end_phoneNum;
                    pnNode.text("手机号码：" + new_phoneNum);
                    var pnImg = $(this.templates[2]).appendTo(pnNode);
                    pnImg.attr("src", "resources/images/widget/appointment_queue/phoneNum.png");
                }
                // 设置预约时间
                if (item.appointmentTime) {
                    var temp =item.id.split("k");//id格式：日期*时间*手机号码，这里只取日期
                    var s = temp[0].split("-");
                    var str = s[0] + "年" + s[1] + "月" + s[2] + "日";
                    var atNode = itemNode.find(".appointment_queue_body_appointmenttime");
                    atNode.text("预约时间：" + str + " " + item.appointmentTime);
                    var atImg = $(this.templates[2]).appendTo(atNode);
                    atImg.attr("src", "resources/images/widget/appointment_queue/time.png");
                }
            },

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

             _layout: function () {
                base._layout.call(this);
             }

        }
    })
})