import { defineComponent as f, getCurrentInstance as b, watch as h, h as n } from "vue";
import { useNamespace as C, useController as y } from "@ibiz-template/vue-util";
import { FormMDCtrlModel as v } from "@ibiz-template/model";
import { FormMDCtrlController as D } from "@ibiz-template/controller";
const M = f({
  name: "KqbcFormMDCtrl",
  props: {
    modelData: {
      type: v,
      required: !0
    },
    controller: {
      type: D,
      required: !0
    }
  },
  setup(a) {
    const m = C("form-mdctrl"), {
      proxy: r
    } = b();
    y(r, a.controller);
    const e = a.controller;
    h(() => e.form.isLoaded, (t, o) => {
      t !== o && t === !0 && e.loadData();
    });
    const c = async () => {
      var o;
      const t = e.form;
      if (t) {
        const l = (o = t.model.appEntity) == null ? void 0 : o.codeName, d = t.context;
        if (l && d) {
          const p = {
            ...d
          }, u = await ibiz.entityService.getService(l, p);
          if (u && e.data) {
            const i = await u.exec("Getkqzgzsc", p, {
              ...e.data
            });
            i.ok && i.data && (e.data.gzsc = i.data.gzsc, e.data.bcs = i.data.bcs);
          }
        }
      }
    }, s = (t) => (o) => {
      e.formNeurons.get(t) || o.evt.on("afterSave", c), e.onNeuronInit(t)(o);
    };
    return h(() => {
      var t;
      return (t = e.contentCtrlData) == null ? void 0 : t.length;
    }, () => {
      c();
    }), {
      ns: m,
      c: e,
      onNeuronInit: s
    };
  },
  render() {
    const a = () => this.c.contentCtrlData.length > 0 && this.c.contentCtrlData.map((r, e) => {
      var t;
      let c = null;
      const {
        form: s
      } = this.c.model;
      if (s && this.c.providers[s.name] && this.c.model.appEntity) {
        const o = Object.assign(this.c.form.context.clone(), {
          [(t = this.c.model.appEntity.codeName) == null ? void 0 : t.toLowerCase()]: r[this.c.model.appEntity.keyName]
        });
        c = n(this.c.providers[s.name].component, {
          props: {
            modelData: s,
            context: o,
            params: this.c.form.params,
            enableAutoSave: !0
          },
          on: {
            neuronInit: this.onNeuronInit(r.srfkey)
          }
        });
      }
      return n("div", {
        class: this.ns.b("form-content"),
        key: r.srfkey
      }, [c, n("div", {
        class: this.ns.b("btn")
      }, [n("i-button", {
        class: this.ns.b("remove-btn"),
        attrs: {
          type: "text"
        },
        on: {
          click: () => this.c.handleRemove(r, e)
        }
      }, ["\u5220\u9664"])])]);
    }), m = () => n("div", {
      class: this.ns.b("main")
    }, [[n("div", {
      class: this.ns.b("title")
    }, [n("span", {
      class: this.ns.b("caption")
    }, [this.modelData.source.caption]), n("i-button", {
      class: this.ns.b("add-btn"),
      attrs: {
        type: "text"
      },
      on: {
        click: () => this.c.handleAdd()
      }
    }, ["\u6DFB\u52A0"])]), a()]]);
    return n("div", {
      class: this.ns.b()
    }, [m()]);
  }
});
export {
  M as KqbcFormMDCtrl,
  M as default
};