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

export class WindowQueue extends Widget {

    private static Template0 = `<div class="row"></div>`;

    private static Template1 = `<div class="col s6 m4 l3 window_queue_container">
    <div>
        <div class="window_queue_header">
            <p class="window_queue_id"><!--窗口1-1-->
            </p>

            <p class="window_queue_status">空闲<img src="images/window_queue/kongxian.png"/></p>
        </div>
        <div class="window_queue_body">
            <p class="window_queue_body_teller_info window_queue_body_teller_no">柜员：暂无</p>

            <p class="window_queue_body_teller_info window_queue_body_teller_name">姓名：暂无</p>
            <img class="window_queue_body_img" src="images/window_queue/guiyuan.png"/>

            <div class="window_queue_body_line"></div>
            <p class="window_queue_body_service_custom">当前服务客户：暂无</p>

            <div class="window_queue_body_middle"></div>
            <div class="window_queue_body_custom">
                <p class="window_queue_body_custom_level">客户等级：暂无</p>
            </div>
            <div class="window_queue_body_line"></div>

            <div class="window_queue_body_footer">
                <p class="window_queue_body_footer_p">暂无此窗口详细数据......</p>
            </div>
        </div>
    </div>
   </div>`;

    private static Template2 = `<img/>`;

    private static Template3 = `<img src="images/window_queue/window.png"/>`;

    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.$thisNode = $(WindowQueue.Template0).attr({ id: id });
    }

    public addItems(args: { items: { win_num: string, status: string, tellerNo: string, tellerName: string, queue_num: string, serviceCustomLevel: string, windowInfo: string }[] }) {
        let items = args.items;
        if (!items || items.length === 0) {
            return;
        }
        let $father = this.$thisNode.parent();
        if ($father.length !== 0) {//回调函数调用addItems,初始化是调用addItems,$father不存在
            this.$thisNode.detach();
        }
        for (let i = 0; i < items.length; i++) {
            if (items[i].win_num) {
                if (this.$thisNode.find("#" + items[i].win_num).length !== 0) {
                    this.setItem(items[i]);
                } else {
                    this.addItem(items[i]);
                }
            }
        }
        if ($father.length !== 0) {//回调函数调用addItems
            this.$thisNode.appendTo($father);
        }
    }

    public setItems(args: { items: { win_num: string, status: string, tellerNo: string, tellerName: string, queue_num: string, serviceCustomLevel: string, windowInfo: string }[] }) {
        let items = args.items;
        this.clear();
        this.addItems({ items: items });
    }

    /**
     * @item{win_num:"",status:"",tellerNo:"",tellerName:"",queue_num:"",serviceCustomLevel:"",windowInfo:""}
     */
    private addItem(item: { win_num: string, status: string, tellerNo: string, tellerName: string, queue_num: string, serviceCustomLevel: string, windowInfo: string }) {
        let $itemNode = $(WindowQueue.Template1).attr({ id: item.win_num }); //创建模板
        $itemNode.appendTo(this.$thisNode); 
        this.setWindowAttr($itemNode, item);
        let own = this;
        //添加查看事件
        $itemNode.find("div.window_queue_body").on("click", function (e) {
            let index = own.indexOf($itemNode[0]);
            let win_num = $itemNode.attr("id");
            EventHub.dispatchEvent(MessageType.WIDGET, {
                id: own.id,
                evtType: Event.LOOK_OVER,
                index: index,
                win_num: win_num
            });
        });
    }

    /**
     * @item{win_num:"",status:"",tellerNo:"",tellerName:"",queue_num:"",serviceCustomLevel:"",windowInfo:""}
     */
    private setItem(item: { win_num: string, status: string, tellerNo: string, tellerName: string, queue_num: string, serviceCustomLevel: string, windowInfo: string }) {
        let $itemNode = this.$thisNode.find("#" + item.win_num);
        this.setWindowAttr($itemNode, item);
    }

    private setWindowAttr($itemNode: JQuery, item: { win_num: string, status: string, tellerNo: string, tellerName: string, queue_num: string, serviceCustomLevel: string, windowInfo: string }) {
        //设置id、服务状态
        $itemNode.find(".window_queue_id")[0].innerHTML = WindowQueue.Template3 + item.win_num;
        if (item.status) {
            let 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");
            let $statusNode = $itemNode.find(".window_queue_status");
            $statusNode.text(item.status);
            let $statusImgNode = $(WindowQueue.Template2).appendTo($statusNode);
            if (item.status === "空闲") {
                $statusImgNode.attr("src", "images/window_queue/kongxian.png");
                windowQueueBody.addClass("window_queue_body_border_style1");
            } else if (item.status === "服务中") {
                $statusImgNode.attr("src", "images/window_queue/fuwuzhong.png");
                windowQueueBody.addClass("window_queue_body_border_style2");
            } else if (item.status === "离线") {//离线
                $statusImgNode.attr("src", "images/window_queue/lixian.png");
                windowQueueBody.addClass("window_queue_body_border_style3");
            } else {//未知
                $statusImgNode.attr("src", "images/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);
            let customLevelNode = $itemNode.find(".window_queue_body_custom");
            customLevelNode.find("img").remove();
            let customLevelImg = $(WindowQueue.Template2).addClass("window_queue_body_custom_level_img").appendTo(customLevelNode);
            if (item.serviceCustomLevel === "黄金客户") {
                customLevelImg.attr("src", "images/custom_queue/custom_level/huangjin.png");
            } else if (item.serviceCustomLevel === "白金客户") {
                customLevelImg.attr("src", "images/custom_queue/custom_level/baijin.png");
            } else if (item.serviceCustomLevel === "钻石客户") {
                customLevelImg.attr("src", "images/custom_queue/custom_level/zuanshi.png");
            } else {
                customLevelImg.attr("src", "images/custom_queue/custom_level/putong.png");
            }
        }
        //设置窗口信息
        if (item.windowInfo) {
            $itemNode.find(".window_queue_body_footer_p").text(item.windowInfo);
        }
    }

    public removeItem(args: { itemIds: string[] }) {
        let ids = args.itemIds;
        if (ids && ids.length > 0) {
            for (let i = 0; i < ids.length; i++) {
                this.$thisNode.find("#" + ids[i]).remove();
            }
        }
    }

    private indexOf(item: HTMLElement) {
        let $items = this.$thisNode.find("div.window_queue_container");
        for (let i = 0; i < $items.length; i++) {
            if (item === $items[i]) {
                return i;
            }
        }
        return -1;
    }

    public clear() {
        this.$thisNode.find(".window_queue_container").remove();
    }
}