import Vue from "vue";
import FEEDBACKCOMP from "./index.vue";
const FBConstructor = Vue.extend(FEEDBACKCOMP);
let FBCINSTANCELIST = [];
const FB = options => {
    const { type, callBack, cancelBack, trialId = null, Id = null, visitTaskId = null, SubjectVisitId = null } = options;
    if (!type) throw `type is requred.but ${type}`
    const id = `FB${new Date().getTime()}`;
    const instance = new FBConstructor();
    FBCINSTANCELIST.push(instance);
    instance.id = id;
    instance.vm = instance.$mount();
    if (instance.vm.visible) return;
    document.body.appendChild(instance.vm.$el);
    instance.vm.open({ type, trialId, Id, visitTaskId, SubjectVisitId });
    instance.vm.$on("success", (Id) => {
        if (callBack) callBack();

    });
    instance.vm.$on("closed", () => {
        document.body.removeChild(instance.vm.$el);
        instance.vm.$destroy();
        let index = FBCINSTANCELIST.findIndex(item => item.id === id);
        FBCINSTANCELIST.splice(index, 1)
        if (cancelBack) cancelBack()
    });
    return instance.vm;
}
FB.close = () => {
    if (FBCINSTANCELIST.length <= 0) return;
    FBCINSTANCELIST.forEach(item => {
        document.body.removeChild(item.vm.$el);
        item.vm.$destroy();
    })
    FBCINSTANCELIST = [];
}
export default FB;