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

export class AppointmentTable extends Widget {
    private static TemplateTime = `
    <div>
    <div class="appointment_table_tabletime">
        <table>
            <tr><td>全天</td></tr>
            <tr><td>9:00</td></tr>
            <tr><td>10:00</td></tr>
            <tr><td>11:00</td></tr>
            <tr><td>12:00</td></tr>
            <tr><td>13:00</td></tr>
            <tr><td>14:00</td></tr>
            <tr><td>15:00</td></tr>
            <tr><td>16:00</td></tr>
            <tr><td>17:00</td></tr>
        </table>
    </div>
    <div class="appointment_table_line">
        <span class="appointment_table_color_gray_small"></span>
        <span class="appointment_table_line_font" >不开放预约</span>
        <span class="appointment_table_color_white_small"></span>
        <span class="appointment_table_line_font">无预约记录</span>
        <span class="appointment_table_color_blue_small"></span>
        <span class="appointment_table_line_font">有预约记录且未满</span>
        <span class="appointment_table_color_red_small"></span>
        <span class="appointment_table_line_font">已约满</span>
    </div>
    <div class="appointment_table">
        <div class="appointment_table_scrollbar">
        </div>
    </div>
</div>`;
    private static TemplateLeft = `
<table class="appointment_table_tablecontent appointment_table_border_left">
</table>`;
    private static TemplateContent = `
    <table class="appointment_table_tablecontent">
</table>`;
    private static Template = `<tr><td></td></tr>`;
    private static TemplateDate = `
    <tr><td><div class="appointment_table_data_yes"><div class="appointment_table_text">已预约1人</div></div></td></tr>
`;

    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.$thisNode = $(AppointmentTable.TemplateTime).attr("id", this.id);
    }
    /**
      * @item{id:date,handle_appointmentTime:"",limitNum:"",rowNum:""}
    */
    public addItems(args: { items: { id: string, handle_appointmentTime: string, limitNum: string, rowNum: string }[] }) {
        let items = args.items;
        if (!items || items.length === 0) {
            return;
        }
        // let $father = this.$thisNode.parent();
        // this.$thisNode.detach();
        for (let i = 0; i < items.length; i++) {
            if (items[i].id) {
                if (this.$thisNode.find("#" + items[i].id).length !== 0) {
                    this.setItem(items[i]);
                } else {
                    this.addItem(items[i]);
                }
            }
        }
        // this.$thisNode.appendTo($father);
    }
    /**
     * @item{id:date,handle_appointmentTime:"",limitNum:"",rowNum:""}
   */
    private addItem(item: { id: string, handle_appointmentTime: string, limitNum: string, rowNum: string }) {
        let $trNode = $(AppointmentTable.Template);
        let $table_containerNode = this.$thisNode.find(".appointment_table_scrollbar");
        if ($table_containerNode.find("table").length === 0) {
            let $first_tableNode = $(AppointmentTable.TemplateLeft).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 (let i = 0; i < Number(item.rowNum); i++) {
                if (i === Number(item.handle_appointmentTime) - 1) {
                    $trNode = $(AppointmentTable.TemplateDate);
                    $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 {
                    $trNode = $(AppointmentTable.Template);
                    $trNode.appendTo($first_tableNode);
                }
            }
        } else {
            let $tableNode = $(AppointmentTable.TemplateContent).attr("id", item.id);
            let item_year = Number(item.id.split("-")[0]);
            let item_month = Number(item.id.split("-")[1]);
            let item_date = Number(item.id.split("-")[2]);
            let i = 0;
            while ($table_containerNode.find("table").get(i)) {
                let table_id = $table_containerNode.find("table").get(i).id;
                let table_year = Number(table_id.split("-")[0]);
                let table_month = Number(table_id.split("-")[1]);
                let 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);
            let arr_width = $table_containerNode.css("width").split("px");
            let num_width = Number(arr_width[0]) + 280;
            let str_width = num_width + "px";
            $table_containerNode.css("width", str_width);
            for (let i = 0; i < Number(item.rowNum); i++) {
                if (i === Number(item.handle_appointmentTime) - 1) {
                    $trNode = $(AppointmentTable.TemplateDate);
                    $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 {
                    $trNode = $(AppointmentTable.Template);
                    $trNode.appendTo($tableNode);
                }
            }
        }
    }
    /**
        * @item{id:date,handle_appointmentTime:"",limitNum:"",rowNum:""}
      */
    private setItem(item: { id: string, handle_appointmentTime: string, limitNum: string, rowNum: string }) {
        if (item.handle_appointmentTime !== null && item.handle_appointmentTime.length !== 0) {
            let $tableNode = this.$thisNode.find("#" + item.id);
            let $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 {
                    let $new_trNode = $(AppointmentTable.TemplateDate);
                    $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) {
                    let appointNum = Number($trNode.find(".appointment_table_data_yes").attr("title"));
                    let arr_width = $trNode.find(".appointment_table_data_yes").css("width").split("px");
                    let num_width = Number(arr_width[0]) / appointNum + Number(arr_width[0]);
                    let 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[] time[0]:起始时间，time[1]:终止时间与起始时间之间的时间差
     */
    public changeTimeTable(args: { time: number[] }) {
        let $timeNode = this.$thisNode.find(".appointment_table_tabletime table");
        $timeNode.empty();
        let time: number[] = args.time;
        let $first_timeNode = $(AppointmentTable.Template);
        $first_timeNode.appendTo($timeNode);
        $first_timeNode.find("td").text("全天");
        let origin_time = time[0];
        let timeNum = time[1];
        for (let i = 0; i <= timeNum; i++) {
            let $item_timeNode = $(AppointmentTable.Template);
            $item_timeNode.appendTo($timeNode);
            $item_timeNode.find("td").text(origin_time + ":00");
            origin_time = origin_time + 1;
        }
    }
    /**
    * @item{date:"",disallowTime:""}
     */
    public setDisallowAppoint(args: { items: { date: string, disallowTime: string }[] }) {
        let items = args.items;
        if (!items || items.length === 0) {
            return;
        }
        for (let i = 0; i < items.length; i++) {
            if (items[i].date) {
                if (this.$thisNode.find("#" + items[i].date).length !== 0) {
                    let $tableNode = this.$thisNode.find("#" + items[i].date);
                    let $trNode = $tableNode.find("tr").eq(Number(items[i].disallowTime));
                    $trNode.find("td").css("background-color", "#f4f7f9");
                } else {
                    break;
                }
            }
        }
    }
    /**
     * @item{id:"",removeTime:"",limitNum:""}
     */
    public removeItem(args: { items: { id: string, removeTime: string, limitNum: string }[] }) {
        let items = args.items;
        if (!items || items.length === 0) {
            return;
        }
        for (let i = 0; i < items.length; i++) {
            if (items[i].id) {
                if (this.$thisNode.find("#" + items[i].id).length !== 0) {
                    let $tableNode = this.$thisNode.find("#" + items[i].id);
                    let $trNode = $tableNode.find("tr").eq(Number(items[i].removeTime));
                    if ($trNode.find(".appointment_table_data_yes").length !== 0) {
                        let 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) {
                            let 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;
                }
            }
        }
    }
    public removeTable(args: { itemIds: string[] }) {
        let itemIds = args.itemIds;
        if (!itemIds || itemIds.length === 0) {
            return;
        }
        for (let i = 0; i < itemIds.length; i++) {
            if (itemIds[i]) {
                if (this.$thisNode.find("#" + itemIds[i]).length !== 0) {
                    this.$thisNode.find("#" + itemIds[i]).remove();
                }
            }
        }
        //判断删除的表是否是第一个
        if (this.$thisNode.find(".appointment_table_scrollbar").find("table").length !== 0) {
            if (this.$thisNode.find(".appointment_table_border_left").length === 0) {
                this.$thisNode.find(".appointment_table_scrollbar").find("table").eq(0).addClass("appointment_table_border_left");
            }
        }
    }

    public clear() {
        this.$thisNode.find(".appointment_table_scrollbar").empty();
    }
}