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

                var $trNode = $(this.templates[3]);
                var $table_containerNode = this.jqueryNode.find('.appointment_table_scrollbar');
                if($table_containerNode.find("table").length === 0) {
                    var $first_tableNode = $(this.templates[1]).attr("id", item.id);
                    $first_tableNode.appendTo($table_containerNode);
                    $trNode.appendTo($first_tableNode);
                    $trNode.find("td").text(item.id);
                    $table_containerNode.css("width","280px");
                    for (var i = 0; i < Number(item.rowNum); i++) {
                        if(i === Number(item.handle_appointmentTime)-1) {
                            var $trNode = $(this.templates[4]);
                            $trNode.appendTo($first_tableNode);
                            $trNode.find(".appointment_table_data_yes").attr("style","width: "+ 1/Number(item.limitNum)*100 + "%");
                            $trNode.find(".appointment_table_data_yes").attr("title","1");
                        } else {
                            var $trNode = $(this.templates[3]);
                            $trNode.appendTo($first_tableNode);
                        }
                    }
                } else {
                    var $tableNode = $(this.templates[2]).attr("id", item.id);
                    var item_year = Number(item.id.split("-")[0]);
                    var item_month = Number(item.id.split("-")[1]);
                    var item_date = Number(item.id.split("-")[2]);
                    var i = 0;
                    while($table_containerNode.find("table").get(i)) {
                        var table_id = $table_containerNode.find("table").get(i).id;
                        var table_year = Number(table_id.split("-")[0]);
                        var table_month = Number(table_id.split("-")[1]);
                        var table_date = Number(table_id.split("-")[2]);
                        if(item_year < table_year) {
                            $tableNode.insertBefore($("#" + table_id));
                            break;
                        }
                        else if(item_year === table_year) {
                            if(item_month < table_month) {
                                $tableNode.insertBefore($("#" + table_id));
                                break;
                            }
                            else if(item_month === table_month) {
                                if(item_date < table_date) {
                                    $tableNode.insertBefore($("#" + table_id));
                                    break;
                                }
                            }
                        }
                        i++;
                        if(i === $table_containerNode.find("table").length) {
                            $tableNode.appendTo($table_containerNode);
                        }
                    }
                    if($table_containerNode.find(".appointment_table_border_left")) {
                        $table_containerNode.find(".appointment_table_border_left").removeClass("appointment_table_border_left");
                    }//删除原来的左边线
                    $("#" + $table_containerNode.find("table").get(0).id).addClass("appointment_table_border_left");//增加左边线
                    $trNode.appendTo($tableNode);
                    $trNode.find("td").text(item.id);
                    var arr_width = $table_containerNode.css("width").split("px");
                    var num_width = Number(arr_width[0]) + 280;
                    var str_width = num_width + "px";
                    $table_containerNode.css("width",str_width);
                    for (var i = 0; i < Number(item.rowNum); i++) {
                        if(i === Number(item.handle_appointmentTime)-1) {
                            var $trNode = $(this.templates[4]);
                            $trNode.appendTo($tableNode);
                            $trNode.find(".appointment_table_data_yes").attr("style","width: "+ 1/Number(item.limitNum)*100 + "%");
                            $trNode.find(".appointment_table_data_yes").attr("title","1");
                        } else {
                            var $trNode = $(this.templates[3]);
                            $trNode.appendTo($tableNode);
                        }
                    }
                }
            },

            setItem: function (item) {
                if(item.handle_appointmentTime !==null && item.handle_appointmentTime.length !== 0) {
                    var $tableNode = this.jqueryNode.find("#" + item.id);
                    var $trNode = $tableNode.find("tr").eq(Number(item.handle_appointmentTime));
                    if($trNode.find("div").length === 0) {
                        if($trNode.find("td").css("background-color")=="rgb(244, 247, 249)") {
                            alert("此时间段不开放预约！");
                        } else {
                            var $new_trNode = $(this.templates[4]);
                            $trNode.replaceWith($new_trNode);
                            $new_trNode.find(".appointment_table_data_yes").attr("style","width: "+ 1/Number(item.limitNum)*100 + "%");
                            $new_trNode.find(".appointment_table_data_yes").attr("title","1");
                        }
                    } else {
                        if($trNode.find(".appointment_table_data_yes").length !== 0) {
                            var appointNum = Number($trNode.find(".appointment_table_data_yes").attr("title"));
                            var arr_width = $trNode.find(".appointment_table_data_yes").css("width").split("px");
                            var num_width = Number(arr_width[0])/appointNum + Number(arr_width[0]);
                            var str_width = num_width + "px";
                            $trNode.find(".appointment_table_data_yes").css("width",str_width);
                            appointNum = appointNum + 1;
                            $trNode.find(".appointment_table_data_yes").attr("title",String(appointNum));
                            $trNode.find(".appointment_table_text").text("已预约" + appointNum + "人");
                            if(appointNum === Number(item.limitNum)) {
                                $trNode.find(".appointment_table_data_yes").addClass("appointment_table_data_full")
                                .removeClass("appointment_table_data_yes").css("width","100%");
                                $trNode.find(".appointment_table_text").text("已约满");
                            }
                        } else {
                            if($trNode.find(".appointment_table_data_full").length !== 0) {
                                alert(item.id + "存在超出预约人数，请检查");
                            }
                        }
                    }
                }
            },
            /**
             * @time{originTime:"",rowNum:""}
             */
            _changeTimeTable: function (attrs) {
                var $timeNode = this.jqueryNode.find('.appointment_table_tabletime table');
                $timeNode.empty();
                var time = attrs.time;
                var $first_timeNode = $(this.templates[3]);
                $first_timeNode.appendTo($timeNode);
                $first_timeNode.find("td").text("全天");
                var origin_time = time[0];
                for(var i=0; i<=time[1]; i++){
                    var $item_timeNode = $(this.templates[3]);
                    $item_timeNode.appendTo($timeNode);
                    $item_timeNode.find("td").text(origin_time+":00");
                    origin_time = origin_time + 1;
                }
            },
            /**
             * @item{id:"",disallowTime:""}
             */
            _setDisallowAppoint: function (attrs) {
                var items = attrs.items;
                if (!items || items.length === 0) {
                    return;
                }
                for (var i = 0; i < items.length; i++) {
                    if (items[i].date) {
                        if (this.jqueryNode.find("#" + items[i].date).length !== 0) {
                            var $tableNode = this.jqueryNode.find("#" + items[i].date);
                            var $trNode = $tableNode.find("tr").eq(Number(items[i].disallowTime));
                            $trNode.find("td").css("background-color","#f4f7f9");
                        } else {
                            break;
                        }
                    }
                }
            },
            /**
             * @item{id:"",removeTime:"",limitNum:""}
             */
            _removeItem: 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) {
                            var $tableNode = this.jqueryNode.find("#" + items[i].id);
                            var $trNode = $tableNode.find("tr").eq(Number(items[i].removeTime));
                            if($trNode.find(".appointment_table_data_yes").length !== 0) {
                                var appointNum = Number($trNode.find(".appointment_table_data_yes").attr("title")) - 1;
                                if(appointNum == 0) {
                                    $trNode.find("td").empty();
                                } else {
                                    $trNode.find(".appointment_table_data_yes").attr("style","width: "+ appointNum/Number(items[i].limitNum)*100 + "%");
                                    $trNode.find(".appointment_table_data_yes").attr("title",String(appointNum));
                                    $trNode.find(".appointment_table_text").text("已预约" + appointNum + "人");
                                }
                            } else {
                                if($trNode.find(".appointment_table_data_full").length !== 0) {
                                    var appointNum = Number($trNode.find(".appointment_table_data_full").attr("title")) - 1;
                                    $trNode.find(".appointment_table_data_full").addClass("appointment_table_data_yes").removeClass("appointment_table_data_full");
                                    $trNode.find(".appointment_table_data_yes").attr("style","width: " + appointNum/Number(items[i].limitNum)*100 + "%");
                                    $trNode.find(".appointment_table_data_yes").attr("title",String(appointNum));
                                    $trNode.find(".appointment_table_text").text("已预约" + appointNum + "人");
                                }
                            }
                        } else {
                            break;
                        }
                    }
                }
            },
            /**
             * @itemIds{date:""}
             */
            _removeTable: function(attrs) {
                var itemIds = attrs.itemIds;
                if (!itemIds || itemIds.length === 0) {
                    return;
                }
                for (var i = 0; i < itemIds.length; i++) {
                    if(itemIds[i]) {
                        if(this.jqueryNode.find("#" + itemIds[i]).length !== 0) {
                            this.jqueryNode.find("#" + itemIds[i]).remove();
                        }
                    }
                }
                //判断删除的表是否是第一个
                if(this.jqueryNode.find(".appointment_table_scrollbar").find("table").length !== 0) {
                    if(this.jqueryNode.find(".appointment_table_border_left").length === 0) {
                        this.jqueryNode.find(".appointment_table_scrollbar").find("table").eq(0).addClass("appointment_table_border_left");
                    }
                }
            },

            _clear: function () {
                this.jqueryNode.find(".appointment_table_scrollbar").empty();
            },

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

