"use strict";const t=require("./validate.js"),e=require("../../../../common/vendor.js"),i={name:"uniForms",components:{},emits:["input","reset","validate","submit"],props:{value:{type:Object,default:()=>({})},modelValue:{type:Object,default:()=>({})},rules:{type:Object,default:()=>({})},validateTrigger:{type:String,default:""},labelPosition:{type:String,default:"left"},labelWidth:{type:[String,Number],default:""},labelAlign:{type:String,default:"left"},errShowType:{type:String,default:"undertext"},border:{type:Boolean,default:!1}},data:()=>({formData:{}}),computed:{dataValue(){return"{}"===JSON.stringify(this.modelValue)?this.value:this.modelValue}},watch:{rules(t){this.init(t)},labelPosition(){this.childrens.forEach((t=>{t.init()}))}},created(){getApp().$vm.$.appContext.config.globalProperties.binddata||(getApp().$vm.$.appContext.config.globalProperties.binddata=function(t,e,i){if(i)this.$refs[i].setValue(t,e);else{let i;for(let t in this.$refs){const e=this.$refs[t];if(e&&e.$options&&"uniForms"===e.$options.name){i=e;break}}if(!i)return console.error("当前 uni-froms 组件缺少 ref 属性");i.setValue(t,e)}}),this.unwatchs=[],this.childrens=[],this.inputChildrens=[],this.checkboxChildrens=[],this.formRules=[],this.init(this.rules)},methods:{init(e){0!==Object.keys(e).length?(this.formRules=e,this.validator=new t.SchemaValidator(e),this.registerWatch()):this.formData=this.dataValue},registerWatch(){this.unwatchs.forEach((t=>t())),this.childrens.forEach((t=>{t.init()})),Object.keys(this.dataValue).forEach((t=>{let e=this.$watch("dataValue."+t,(e=>{if(e)if("[object Object]"===e.toString())for(let i in e){let a=`${t}[${i}]`;this.formData[a]=this._getValue(a,e[i])}else this.formData[t]=this._getValue(t,e)}),{deep:!0,immediate:!0});this.unwatchs.push(e)}))},setRules(t){this.init(t)},setValue(t,e,i){let a=this.childrens.find((e=>e.name===t));return a?(e=this._getValue(a.name,e),this.formData[t]=e,a.val=e,a.triggerCheck(e,i)):null},resetForm(t){this.childrens.forEach((t=>{t.errMsg="";const e=this.inputChildrens.find((e=>e.rename===t.name));e&&(e.errMsg="",e.is_reset=!0,e.$emit("input",e.multiple?[]:""),e.$emit("update:modelValue",e.multiple?[]:""))})),this.childrens.forEach((t=>{t.name&&(this.formData[t.name]=this._getValue(t.name,""))})),this.$emit("reset",t)},validateCheck(t){null===t&&(t=null),this.$emit("validate",t)},async validateAll(t,e,i,a){let s,r=[];for(let o in t){const t=this.childrens.find((t=>t.name===o));t&&r.push(t)}a||"function"!=typeof i||(a=i),!a&&"function"!=typeof a&&Promise&&(s=new Promise(((t,e)=>{a=function(i,a){i?e(i):t(a)}})));let l=[],n={};if(this.validator)for(let o in r){const e=r[o];let i=e.isArray?e.arrayField:e.name;if(e.isArray){if(-1!==e.name.indexOf("[")&&-1!==e.name.indexOf("]")){const a=e.name.split("["),s=a[0],r=a[1].replace("]","");n[s]||(n[s]={}),n[s][r]=this._getValue(i,t[i])}}else n[i]=this._getValue(i,t[i]);const a=await e.triggerCheck(t[i],!0);if(a&&(l.push(a),"toast"===this.errShowType||"modal"===this.errShowType))break}else n=t;return Array.isArray(l)&&0===l.length&&(l=null),Array.isArray(i)&&i.forEach((t=>{n[t]=this.dataValue[t]})),"submit"===e?this.$emit("submit",{detail:{value:n,errors:l}}):this.$emit("validate",l),a&&"function"==typeof a&&a(l,n),s&&a?s:null},submitForm(){},submit(t,e,i){for(let a in this.dataValue){this.childrens.find((t=>t.name===a))&&void 0===this.formData[a]&&(this.formData[a]=this._getValue(a,this.dataValue[a]))}return i||console.warn("submit 方法即将废弃，请使用validate方法代替！"),this.validateAll(this.formData,"submit",t,e)},validate(t,e){return this.submit(t,e,!0)},validateField(t,e){t=[].concat(t);let i={};return this.childrens.forEach((e=>{-1!==t.indexOf(e.name)&&(i=Object.assign({},i,{[e.name]:this.formData[e.name]}))})),this.validateAll(i,"submit",[],e)},resetFields(){this.resetForm()},clearValidate(t){t=[].concat(t),this.childrens.forEach((e=>{const i=this.inputChildrens.find((t=>t.rename===e.name));(0===t.length||-1!==t.indexOf(e.name))&&(e.errMsg="",i&&(i.errMsg=""))}))},_getValue(t,e){const i=this.formRules[t]&&this.formRules[t].rules||[],a=i.find((t=>t.format&&this.type_filter(t.format))),s=i.find((t=>t.format&&"boolean"===t.format||"bool"===t.format));return a&&(e=isNaN(e)?e:""===e||null===e?null:Number(e)),s&&(e=!!e),e},type_filter:t=>"int"===t||"double"===t||"number"===t||"timestamp"===t}};const a=e._export_sfc(i,[["render",function(t,i,a,s,r,l){return{a:e.o(((...t)=>l.submitForm&&l.submitForm(...t))),b:e.o(((...t)=>l.resetForm&&l.resetForm(...t))),c:a.border?"":1}}]]);wx.createComponent(a);
