/*
 * 特别声明：本技术材料受《中华人民共和国著作权法》、《计算机软件保护条例》等法律、法规、行政
 * 规章以及有关国际条约的保护，赞同科技享有知识产权、保留一切权利并视其为技术秘密。未经本公司书
 * 面许可，任何人不得擅自（包括但不限于：以非法的方式复制、传播、展示、镜像、上载、下载）使用，
 * 不得向第三方泄露、透露、披露。否则，本公司将依法追究侵权者的法律责任。特此声明！
 *
 * 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";
/**
 * 客户信息栏组件，暂时放other里面
 */
export class CustCollection extends Widget {
    private static TemplateBody = `
    <div class="adore-hidden-scrollbar" style="overflow: auto">
    <ul id="itemHeaderBody" class="collapsible" data-collapsible="expandable" style="background-color: #f5f5f5">
    </ul>
    </div>`;
    private static TemplateHeader = `
    <li style="width: 100%">
    <div id="itemHeader" class="collapsible-header" style="color: #6e7888;background-color: #e6e6e6"></div>
    <div class="collapsible-body">
        <div id="itemBody" class="collection">
        </div>
    </div>
    </li>`;
    private static TemplateContent = `
    <a href="#!" class="collection-item active waves-effect">
    <div id="itemContent"></div>
    </a>`;
    private static TemplateImage = `
    <img src="images/cust_collection/new.jpg"
     alt=""
     class="circle"
     style="width: 6%;float:left;margin-top: 2%;margin-left: -8%">`;
    private itemHeaderTemplate: string;
    private itemTemplate: string;
    private itemIconTemplate: string;
    private contentOne: any;
    private contentTwo: any;
    constructor(parentId: string, id: string, style: number) {
        super(parentId, id, style);
        this.$thisNode = $(CustCollection.TemplateBody).attr("id", this.id);
        this.itemHeaderTemplate = CustCollection.TemplateHeader;
        this.itemTemplate = CustCollection.TemplateContent;
        this.itemIconTemplate = CustCollection.TemplateImage;
        this.contentOne = new Array();
        this.contentTwo = new Array();
    }

    public postLayout() {
        $(".collapsible").collapsible({
            accordion: false // A setting that changes the collapsible behavior to expandable instead of the default accordion style
        });
    }
    /* args:{ degree:string, name: string [], parentId: string} */
    public setContents(args: { attribute: string }) {
        let data = JSON.parse(args.attribute);
        for (let key in data) {
            let belongTo = key.length - key.replace(/,/g, "").length;
            switch (belongTo) {
                case 0:
                    this.contentOne[parseInt(key)] = data[key];
                    this.contentTwo[parseInt(key)] = new Array();
                    break;

                case 1:
                    let cutKey = key.substr(1, key.length - 2);
                    let location = cutKey.split(",");
                    let m = Number(location[0]);
                    let n = Number(location[1]);
                    this.contentTwo[m][n] = data[key];
                    break;

                default:
                    console.log("data error");
                    break;
            }
        }
    }

    public initCollection() {
        let own = this;
        let flag = false;
        for (let i = 0; i < this.contentTwo.length; i++) {
            if (this.contentTwo[i].length > 0) {
                flag = true;
                break;
            }
        }
        if (flag) {
            for (let i = 0; i < this.contentOne.length; i++) {
                let itemHeaderNode = $(this.itemHeaderTemplate);
                itemHeaderNode.find("#itemHeader").text(this.contentOne[i]);
                this.$thisNode.find("#itemHeaderBody").append(itemHeaderNode);
                for (let j = 0; j < this.contentTwo[i].length; j++) {
                    let itemNode = $(this.itemTemplate);
                    let contentId = i + "_" + j;
                    let itemContentNode = itemNode.find("#itemContent").attr("id", contentId);
                    itemContentNode.text(this.contentTwo[i][j]);
                    itemContentNode.append(this.itemIconTemplate);
                    itemContentNode.on("click", function (event) {
                        let tempAttr = this.id.split("_");
                        $(this).children("img").remove()
                        let indexString = "[" + tempAttr[0] + "," + tempAttr[1] + "]";
                        EventHub.dispatchEvent(MessageType.WIDGET, {
                            id: own.id,
                            location: indexString,
                            evtType: Event.CLICK
                        });
                    });
                    itemHeaderNode.find("#itemBody").append(itemNode);
                }
            }
        }
    }
}