(function(f){
    var nameSpace = 'field_6664988151731237757';
    if(!window[nameSpace]){
        var Builder = f();
        window[nameSpace] = {
            instance: {}
        };
        window[nameSpace].init = function (options) {
            window[nameSpace].instance[options.privateId] = new Builder(options);
        };
    }
})(function(){
    /**
	 * 构造函数
     * @param options
     * @constructor
     */
	function App(options) {
		var self = this;
		//初始化参数
		self.initParams(options);
		//初始化dom
		self.initDom();
		//事件
		self.events();
    }
    
    App.prototype = {
		initParams : function (options) {
			var self = this;
            self.adaptation = options.adaptation;
            self.adaptation.formMessage = options.formMessage;
            self.privateId = options.privateId;
            self.messageObj = options.getData;
            self.preUrl = options.url_prefix;
        },
		initDom : function () {
			var self = this;
            dynamicLoading.css(self.preUrl + 'css/formQueryBtn.css');
            self.appendChildDom();
        },
		events : function () {
			var self = this;
            // 监听是否数据刷新
			
			$(".field0215__").css("background-color","#008BFF");
			$(".field0215__").css("color","#FFFFFF");
			$(".field0215__").css("border-radius","10px");
			$(".field0215__").mouseover(function (e) {
				var $this = $(this);// 当前触发事件的标签对象
			}).mouseout(function (e) {
				$(".field0215__").css("background-color","#008BFF");
			}).mousemove(function (e) {
				$(".field0215__").css("background-color","#005297");
			});

        },
        appendChildDom : function () {
			var self = this;
            var domStructure = '<section class="customButton_box_content">'+
                '<div class="customButton_class_box '+ self.privateId + '" title="' + self.messageObj.display.escapeHTML() + '">'+ self.messageObj.display.escapeHTML() +'</div>'+
                '</section>';
            document.querySelector('#' + self.privateId).innerHTML = domStructure;
            var jumpFun = function() {
				var url2 = window.location.origin;
				var s = self;
                var recordId = self.messageObj.formdata.formsons.front_formson_7.records[0].recordId;
                opts = {
                    fieldId: 'field0191',
                    tableName:'formson_0744',
                    recordId:recordId
                };
                var data = csdk.core.getFieldData(opts);
				var field0206 = {fieldId: 'field0206'};
				var field0206value = csdk.core.getFieldData(field0206).value;
				var field0001 = {fieldId: 'field0001'};
				var field0001value = csdk.core.getFieldData(field0001).value;
                var field0007 = {fieldId: 'field0007'};
                var field0007value = csdk.core.getFieldData(field0007).value;
				var field0068 = {fieldId: 'field0068'};
				var field0068value = csdk.core.getFieldData(field0068).value;
				var field0212 = {fieldId: 'field0212'};
				var field0212value = csdk.core.getFieldData(field0212).value;
				var field0213 = {fieldId: 'field0213'};
				var field0213value = csdk.core.getFieldData(field0213).value;
				var field0214 = {fieldId: 'field0214'};
				var field0214value = csdk.core.getFieldData(field0214).value;
                var field0216 = {fieldId: 'field0216'};
                var field0216value = csdk.core.getFieldData(field0216).value;
				var field0207 = {fieldId: 'field0207'};
                var field0207value = csdk.core.getFieldData(field0207).showValue;
                var field0014 = {fieldId: 'field0014'};
                var field0014value = csdk.core.getFieldData(field0014).showValue;
                var field0011 = {fieldId: 'field0011'};
                var field0011value = csdk.core.getFieldData(field0011).showValue;
                console.log(url2);
                var str = "";
				
                $.ajax({
                    type : 'post',
                    async : true,
                    // 记得加随机数，不然如果ajax轮询请求会不执行
                    url : encodeURI('/seeyon/interestMeasurementController.do?datetime=' + Math.random()),
                    data:{"startDate":field0206value,
                        "cycle":field0207value,
                        "contractDate":field0007value,
                        "credit":field0068value,
                        "principal":field0212value,
                        "money":field0213value,
                        "interestRate":field0214value,
                        "loanType":field0014value,
                        "contractCode":field0001value,
                        "updateCode":field0216value,
                        "period":field0011value},
                    dataType : 'json',
                    contentType : 'application/json; charset=UTF-8',
                    success : function(res) {

                        var randomNum = Math.floor(Math.random() * 10001);
                        if(res.success){
                            var money = data.showValue;
                            if(money!="" ){

                                self.adaptation.formdata.field0215__.formmains.formmain_0725.field0216.value = res.num;
                            }else{
                                self.adaptation.formdata.field0215__.formmains.formmain_0725.field0211.value = randomNum;
                                self.adaptation.formdata.field0215__.formmains.formmain_0725.field0216.value = res.num;
                            }
                                $.alert(利息明细生成完成);
                                // var data = {
                                //     fieldId: 'field0211',
                                //     fieldData: {
                                //         value: randomNum+'', //数据值，存入数据库中的value值
                                //         display: randomNum+'', //字段渲染在页面上的显示值，通常是经过format后的值
                                //         auth: ''
                                //     }
                                // };
                                // csdk.core.setFieldData(data);
                            // self.adaptation.formdata.field0210__.formmains.formmain_0087.field0215.value = randomNum;


                        }else{
                            $.alert(res.s);
                        }
                    }
                });
                var content = self.messageObj.formdata.content;
				
            };
            document.querySelector('.' + self.privateId).removeEventListener('click', jumpFun);
            document.querySelector('.' + self.privateId).addEventListener('click', jumpFun);
            //渲染隐藏权限
            if (self.messageObj.auth === 'hide') {
                document.querySelector('#' + self.privateId).innerHTML = '<div class="cap4-text__browse" style="line-height: 1.8; color: rgb(0, 0, 0) !important;">***</div>';
            }
        }
	};

    var dynamicLoading = {
        css: function(path) {
            if(!path || path.length === 0) {
                throw new Error('argument "path" is required !');
            }
            var head = document.getElementsByTagName('head')[0];
            var link = document.createElement('link');
            link.href = path;
            link.rel = 'stylesheet';
            link.type = 'text/css';
            head.appendChild(link);
        },
        js: function(path) {
            if(!path || path.length === 0) {
                throw new Error('argument "path" is required !');
            }
            var head = document.getElementsByTagName('head')[0];
            var script = document.createElement('script');
            script.src = path;
            script.type = 'text/javascript';
            head.appendChild(script);
        }
    }

    return App;
});