var tool = {

    buildTree(data, pid) {
        let result = [];
        for (let item of data) {
            item.name = item.NAME;
            item.id = item.ID;
            if (item.PID === pid) {
                let children = this.buildTree(data, item.ID);
                if (children.length) {
                    item.children = children;
                }
                result.push(item);
            }
        }
        return result;
    },

    structureTree(data) {
        return data.map(i => {
            let newItem = {
                id: i.depInfo.departid,
                label: i.depInfo.departName,
                name: i.depInfo.departName,
                value: i.depInfo.departid,
                children: i.childDep,
                ...i.depInfo,
            };
            if (i.childDep && i.childDep.length > 0) {
                newItem.children = this.structureTree(i.childDep);
            }
            return newItem;
        });
    },

    objectToFormData(obj, form, namespace) {
        let fd = form || new FormData();
        let formKey;
        for (let property in obj) {
            if (obj && obj.hasOwnProperty && obj.hasOwnProperty(property)) {
                if (namespace) {
                    formKey = namespace + '[' + property + ']';
                } else {
                    formKey = property;
                }
                if (!(obj[property] instanceof Array) && !(obj[property] instanceof File) && !(typeof obj[property] == 'string')) {
                    console.log(obj[property] instanceof Array)
                    this.objectToFormData(obj[property], fd, formKey);
                } else {
                    fd.append(formKey, obj[property]);
                }
            }
        }
        return fd;
    },

    UrlSearch() {
        var name, value;
        var str = location.href; //取得整个地址栏
        var num = str.indexOf("?")
        str = str.substr(num + 1); //取得所有参数   stringvar.substr(start [, length ]
        var arr = str.split("&"); //各个参数放到数组里
        var obj = {};
        for (var i = 0; i < arr.length; i++) {
            num = arr[i].indexOf("=");
            if (num > 0) {
                name = arr[i].substring(0, num);
                value = arr[i].substr(num + 1);
                obj[name] = value;
            }
        }
        return obj;
    }
}

export default tool;