/**
 *  自定义控件书写模板
 */
(function (KDApi, $) {
    // 构造函数，变量名随意，与最后一句代码的KDApi.register的第二个参数一致即可
    function OrgChart(model) {
        this._setModel(model)
    }

    // 原型中封装生命周期函数，固定格式
    OrgChart.prototype = {
        _setModel: function (model) {
            this.model = model
        },
        init: function (props) {
            // TO DO
            initFunc(this.model, props)
        },
        update: function (props) {
            if (!props.data) return
            var eventName = props.data.eventName
            if (eventName == 'init') {
                return
            } else if (eventName == 'search') {
                var that = window.vueObj;
				 props.data ? window.resultData = props.data : ''
                that.initData(that.verticalLayout, that.cardSize, parseInt(that.showLevelCount), that.verticalShow)
            } else if (eventName == 'f7click') {
                window.vueObj.topOrgan = props.data.name
                window.vueObj.topOrganId = props.data.id
            }
        },
        destoryed: function () {
            console.log('distroyed')
        }
    }

    var initFunc = function (model, props) {
        window.KDmodel = model //将model挂在window上供vue中的index.js使用
        props.data ? window.resultData = props.data : ''

        KDApi.loadFile(['./js/ppt/pptxgen.bundle.js', './js/ppt/kdPPT.js'], model, function () {
            KDApi.loadFile('./css/index.css', model, function () {
                KDApi.loadFile('./js/element-ui/style.css', model, function () {
                    KDApi.loadFile('./js/element-ui/vue.js', model, function () {
                        KDApi.loadFile('./js/element-ui/element-ui.js', model, function () {
                            KDApi.loadFile('./js/element-ui/element-ui.css', model, function () {
                                KDApi.getTemplateStringByFilePath('./html/index.art', model, {}).then(function (result) {
                                    model.dom.innerHTML = result
                                    $('#wrapper', model.dom).css('height', ($(document.body).height() - 162) + 'px')
                                    KDApi.loadFile(['./js/d3.js', './js/kdorg.js', './js/data.js', './js/jsPdf.debug.js'], model, function () {
                                        KDApi.loadFile('./js/index.js', model, function () {
                                            window.vueObj = window.vueObj ? window.vueObj : {} 
                                            window.vueObj.topOrgan = props.data ? props.data.topOrgan : ''
                                            window.vueObj.topOrganId = props.data ? props.data.topOrgId : ''
                                        })
                                    })
                                })
                            })
                        })
                    })
                })

            })
        })
    }

   

    // 你只需知道第一个参数是你接下来要新增控件方案时填的方案id，第二个参数是上面声明的构造函数
    KDApi.register('orgchart', OrgChart)
})(window.KDApi, jQuery) // 这里的jQuery不是必须要传进去的，可移除，要用到的时候才传，PC端系统默认会有jQuery对象，版本是1.12.4