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

/**
 * Created by heweijun on 2016/5/26.
 */

export class AppointmentQueue extends Widget {

    private static TemplateCotainer: string = `<div class="row"></div>`;
    private static TemplateQueue: string = `
            <div class="col s6 m6 l5 appointment_queue">
                <div class="appointment_queue_shadow">
                    <div class="appointment_queue_header">
                        <div class="appointment_queue_id"></div>
                        <input class="appointment_queue_delete" type="image" src="images/appointment_queue/del.png"/>
                    </div>
                    <div class="appointment_queue_body">
                        <p class="appointment_queue_body_content appointment_queue_body_mediumtype"><img class="appointment_queue_body_img" src="images/appointment_queue/cardType.png"/>介质类型：</p>

                        <p class="appointment_queue_body_content appointment_queue_body_mediumnum"><img class="appointment_queue_body_img" src="images/appointment_queue/jizhiNum.png"/>介质号码：</p>

                        <p class="appointment_queue_body_content appointment_queue_body_phonenum"><img class="appointment_queue_body_img" src="images/appointment_queue/phoneNum.png"/>手机号码：</p>

                        <p class="appointment_queue_body_content appointment_queue_body_appointmenttime"><img class="appointment_queue_body_img" src="images/appointment_queue/time.png"/>预约时间：</p>
                    </div>
                </div>
            </div>
            `;
    private static TemplateImage = `<img/>`;

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

    /**
     * @item{id:date+"k"+appointmentTime.split(":")[0]+"k"+phoneNum,mediumType:"",mediumNum:"", appointmentTime:""}
     */
    public addItems(args: {items: {id: string, mediumType: string, mediumNum: string, appointmentTime: 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) {
                    this.setItem(items[i]);
                } else {
                    this.addItem(items[i]);
                }
            }
        }
    }

    /**
     * @item{id:date+"k"+appointmentTime.split(":")[0]+"k"+phoneNum,mediumType:"",mediumNum:"", appointmentTime:""}
     */
    private addItem(item: {id: string, mediumType: string, mediumNum: string, appointmentTime: string}) {
        let $itemNode = $(AppointmentQueue.TemplateQueue).attr({id: item.id});
        $itemNode.appendTo(this.$thisNode);
        this.setWindowAttr($itemNode, item);
        let own = this;
        $itemNode.find(".appointment_queue_delete").on("click", function (e: BaseJQueryEventObject) {
            EventHub.dispatchEvent(MessageType.WIDGET, {
                id: own.id,
                removeId: item.id,
                evtType: Event.MODIFY
            });
        });
    }

    private setWindowAttr($itemNode: JQuery, item: {id: string, mediumType: string, mediumNum: string, appointmentTime: string}) {
        let temp = item.id.split("k"); //id格式：日期k时间k手机号码
        //设置id
        if (item.id) {
            let $idNode = $itemNode.find(".appointment_queue_id");
            $idNode.text("预约编号：" + item.id);
        }

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

    private setItem(item: {id: string, mediumType: string, mediumNum: string, appointmentTime: string}) {
        let $itemNode = this.$thisNode.find("#" + item.id);
        this.setWindowAttr($itemNode, item);
    }

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

    public clean() {
        this.$thisNode.find(".appointment_queue").remove();
    }

    public setFocus() {

    }
}