(function(f){
    var nameSpace = 'field_5209586381190735608';
    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;
                // 计费方式
                var field0021 = {fieldId: 'field0021'};
                var field0021value = csdk.core.getFieldData(field0021).showValue;
                // 缴费方式
                var field0019 = {fieldId: 'field0019'};
                var field0019value = csdk.core.getFieldData(field0019).showValue;
                // 年缴费方式类型
                var field0025 = {fieldId: 'field0025'};
                var field0025value = csdk.core.getFieldData(field0025).showValue;
                // 面积单价
                var field0022 = {fieldId: 'field0022'};
                var field0022value = csdk.core.getFieldData(field0022).value;
                // 固定租金单价
				var field0023 = {fieldId: 'field0023'};
				var field0023value = csdk.core.getFieldData(field0023).value;
				// 合同开始日期
				var field0027 = {fieldId: 'field0027'};
				var field0027value = csdk.core.getFieldData(field0027).value;
                // 合同结束日期
				var field0028 = {fieldId: 'field0028'};
				var field0028value = csdk.core.getFieldData(field0028).value;
				// 租赁面积
				var field0060 = {fieldId: 'field0060'};
				var field0060value = csdk.core.getFieldData(field0060).value;
                // 账单编号
                // var field0083 = {fieldId: 'field0083'};
                // var field0083value = csdk.core.getFieldData(field0083).value;
                var field0083 = {fieldId: 'field0082'};
                var field0083value = csdk.core.getFieldData(field0083).value;

				// 判断合同开始日期是否在合同结束日期之前
                if(!isEmpty(field0027value)|| !isEmpty(field0028value)){
                    if(Date.parse(field0027value)>Date.parse(field0028value)){
                        $.alert("请正确填写合同日期");
                        return ;
                    }
                }else{
                    $.alert("请填写合同日期");
                    return ;
                }

                // 当缴费方式为年缴费时年缴费类型必填
                if(field0019value=='年'){
                    if(isEmpty(field0025value)){
                        $.alert("请选择年缴费方式选项");
                        return ;
                    }
                }

                console.log(url2);
                var str = "";
				
                $.ajax({
                    type : 'post',
                    async : true,
                    // 记得加随机数，不然如果ajax轮询请求会不执行
                    url : encodeURI('/seeyon/ydctLeaseBillController.do?datetime=' + Math.random()),
                    data:{
                        "jifeifs":field0021value,
                        "jiaofeifs":field0019value,
                        "njiaofeifs":field0025value,
                        "startDate":field0027value,
                        "endDate":field0028value,
                        "mj":field0060value,
                        "mjzj":field0022value,
                        "gdzj":field0023value,
                        "bdid":field0083value
                    },
                    dataType : 'json',
                    contentType : 'application/json; charset=UTF-8',
                    success : function(res) {
                        var randomNum = Math.floor(Math.random() * 10001);
                        if(res.success){
                            // $.alert("账单明细生成完成");
                            if(field0083value=="" ){
                               //self.adaptation.formdata.field0215__.formmains.formmain_0725.field0216.value = res.num;
                                var data = {
                                    fieldId: 'field0083',
                                    fieldData: {
                                        value: res.num+'', //数据值，存入数据库中的value值
                                        display: res.num+'', //字段渲染在页面上的显示值，通常是经过format后的值
                                        auth: ''
                                    }
                                };
                                csdk.core.setFieldData(data);
                            }
                            // var field0084 = {fieldId: 'field0084'};
                            // let field0084val = csdk.core.getFieldData(field0084)
                            // field0084val.value = randomNum+"";
                            // field0084val.showValue = randomNum+"";
                            // field0084val.showValue2 = randomNum+"";
                            // var adddata = {tableName : "formmain_0126",
                            //     tableCategory : "formmain",
                            //     updateData : {
                            //         field0084 :field0084val
                            //     }
                            // };
                            // window.thirdPartyFormAPI.backfillFormControlData(adddata);
                            self.adaptation.formdata.field0082__.formmains.formmain_0126.field0084.value = randomNum;
                            // for(var i = 0 ; i < 20 ; i++){
                            //     var data1 ={
                            //         tableName:"formson_0128",
                            //         isFormRecords:false,
                            //         chooseRecords:{
                            //             formson_0128 : 0,
                            //         },
                            //         callbackFn:test()
                            //     }
                            //
                            //     window.thirdPartyFormAPI.insertFormsonRecords(data1);
                            // }



                                //self.adaptation.formdata.field0215__.formmains.formmain_0725.field0211.value = randomNum;
                                //self.adaptation.formdata.field0215__.formmains.formmain_0725.field0216.value = res.num;
                                // 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>';
            }
        }
	};

    function test() {
        console.log("明细行生成成功");;
    }


    function isEmpty(text) {
        return !text || text.trim() === '';
    }

    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;
});