<script lang='tsx'>
import { Component, Vue, Prop, Model, Watch } from "vue-property-decorator";
import { VNode, CreateElement } from "vue";
import { interval, Subject, Subscription } from "rxjs";
import { Http, Util } from "@/utils";
import JSONEditor from "@json-editor/json-editor";
import BootstrapVue from "bootstrap-vue";
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap-vue/dist/bootstrap-vue.css";
import CodeListService from "@/codelist/codelist-service";
import AppFormJson from "./app-form-json.vue";

@Component({
  components: {},
})
export default class AppFormJsonLogic extends AppFormJson {
  public codeListService: CodeListService = new CodeListService({
    $store: this.$store,
  });

  public async getSchema(): Promise<any> {
      window["showdetail"]=this.showdetail;
    let _field = {
      type: "object",
      title: "值项",
      grid_columns: 12,
      headerTemplate:
        "{{ self.fieldname }} - {{ self.fieldcond }} - {{ self.condvalue }}",
      required: ["entityname", "fieldname", "fieldcond", "condvalue"],
      properties: {
        entityname: {
          type: "selectnew",
          title: "实体",
          watch: {
            modelid: "root.rootmodelid",
          },
          enumSource: [
            {
              source: [...[{ label: "--", id: "" }]],
              p0: "modelid",
              url: "/dst/datamodels/${p0}/propertys",
              title: "{{item.label}}",
              value: "{{item.id}}",
            },
          ],
          options: {
            select2_options: {
              width: 300,
              value: "",
            },
          },
        },
        fieldname: {
          type: "selectnew",
          title: "属性",
          watch: {
            modelid: "root.rootmodelid",
            entity: "arr_field.entityname",
          },
          enumSource: [
            {
              source: [...[{ label: "--", id: "" }]],
              p0: "modelid",
              p1: "entity",
              url: "/dst/datamodels/${p0}/propertys/${p1}/fields",
              title: "{{item.label}}",
              value: "{{item.id}}",
            },
          ],
          links: [
            {
              rel: "查看",
               "href": "javascript:window.showdetail('DICT',{'field':'{{self}}'})"
            },
          ],

          options: {
            select2_options: {
              width: 250,
              value: "",
            },
          },
        },
        fieldcond: {
          type: "string",
          title: "判断",
          enumSource: [],
          options: {
            select2_options: {
              width: 100,
              value: "",
            },
          },
        },
        condvalue: {
          title: "值",
          type: "string",
        },
        condref: {
          title: "辅助字典",
          type: "string",
          
          options: {
            hidden:true
          },
        },
      },
    };

    let _releationfield = JSON.parse(JSON.stringify(_field));
    _releationfield.properties.entityname.watch = {
      modelid: "root.rootmodelid",
      submodel: "arr_releation.releationentity",
    };
    _releationfield.properties.entityname.enumSource = [
      {
        source: [...[{ label: "--", id: "" }]],
        p0: "modelid",
        p1: "submodel",
        url: "/dst/datamodels/${p0}/name/${p1}/propertys",
        title: "{{item.label}}",
        value: "{{item.id}}",
      },
    ];
    _releationfield.properties.fieldname.watch = {
      modelid: "root.rootmodelid",
      submodel: "arr_releation.releationentity",
      entity: "arr_field.entityname",
    };
    _releationfield.properties.fieldname.enumSource = [
      {
        source: [...[{ label: "--", id: "" }]],
        p0: "modelid",
        p1: "submodel",
        p2: "entity",
        url: "/dst/datamodels/${p0}/name/${p1}/propertys/${p2}/fields",
        title: "{{item.label}}",
        value: "{{item.id}}",
      },
    ];
 

    let _group = {
      type: "object",
      title: "组逻辑",
      headerTemplate: "{{ self.groupcond }}",
      required: ["groupcond"],
      properties: {
        groupcond: {
          grid_columns: 12,
          type: "string",
          title: "AND/OR",
          default: "AND",
          enumSource: [
            {
              source: [{ id: "AND" }, { id: "OR" }],
              title: "{{item.id}}",
              value: "{{item.id}}",
            },
          ],
          options: {
            select2_options: {
              width: 120,
              value: "",
              placeholder: "请选择",
            },
          },
        },
        group: {
          type: "array",
          required: ["groupcond"],
          title: "组",
          items: {
            $ref: "#/definitions/group",
          },
        },
        field: {
          grid_columns: 12,
          type: "array",
          format: "table",
          title: "值项",
          items: {
            id: "arr_field",
            $ref: "#/definitions/field",
          },
        },
      },
    };

    let _releationgroup = JSON.parse(JSON.stringify(_group));

    _releationgroup.properties.field.items.$ref =
      "#/definitions/releationfield";
    _releationgroup.properties.group.items.$ref =
      "#/definitions/releationgroup";
    _releationgroup.required = ["groupcond", "field"];

    let _schema = {
      title: "逻辑规则",
      type: "object",
      $ref: "#/definitions/root",
      definitions: {
        root: {
          type: "object",
          title: "逻辑",
          headerTemplate: "{{ self.groupcond }}",
          required: ["groupcond", "rootmodelid"],
          properties: {
            rootmodelid: {
              grid_columns: 6,
              type: "string",
              title: "模型",
              enumSource: [
                {
                  source: [
                    { id: this.data.modelid, text: this.data.modelname },
                  ],
                  title: "{{item.text}}",
                  value: "{{item.id}}",
                },
              ],
              options: {
                hidden: true,
              },
            },
            groupcond: {
              grid_columns: 6,
              type: "string",
              title: "AND",
              default: "AND",
              enumSource: [
                {
                  source: [{ id: "AND" }],
                  title: "{{item.id}}",
                  value: "{{item.id}}",
                },
              ],
              options: {
                hidden: true,
              },
            },
            field: {
              grid_columns: 12,
              type: "array",
              format: "table",
              title: "值项",
              items: {
                id: "arr_field",
                $ref: "#/definitions/field",
              },
            },
            group: {
              type: "array",
              required: ["groupcond", "field"],
              title: "组逻辑",
              format: "tabs",
              items: {
                $ref: "#/definitions/group",
              },
            },
            releation: {
              type: "array",
              required: ["releationcond"],
              title: "子数据逻辑",
              format: "tabs",
              items: {
                id: "arr_releation",
                $ref: "#/definitions/releation",
              },
            },
          },
        },
        releation: {
          type: "object",
          title: "子数据逻辑",
          format: "grid",
          headerTemplate: "{{ self.releationcond }}-{{ self.releationentity }}",
          required: ["releationcond", "releationentity", "group"],
          properties: {
            releationcond: {
              grid_columns: 3,
              type: "string",
              title: "存在/不存在",
              default: "exists",
              enumSource: [
                {
                  source: [{ id: "exists" }, { id: "not exists" }],
                  title: "{{item.id}}",
                  value: "{{item.id}}",
                },
              ],
              options: {
                  grid_columns: 3,
                select2_options: {
                  width: 120,
                  value: "",
                  placeholder: "请选择",
                },
              },
            },
            releationentity: {
              grid_columns: 3,
              type: "selectnew",
              title: "子数据实体",
              watch: {
                modelid: "root.rootmodelid",
              },
              enumSource: [
                {
                  source: [...[{ label: "--", id: "" }]],
                  p0: "modelid",
                  url: "/dst/datamodels/${p0}/submodels",
                  title: "{{item.label}}",
                  value: "{{item.id}}",
                },
              ],
              options: {
                  grid_columns: 3,
                select2_options: {
                  width: 250,
                  value: "",
                  placeholder: "请选择",
                },
              },
            },
            group: {
              type: "array",
              required: ["groupcond","field"],
              title: "组逻辑",
              items: {
                $ref: "#/definitions/releationgroup",
              },
            },
          },
        },
        group: _group,
        releationgroup: _releationgroup,
        field: _field,
        releationfield: _releationfield,
      },
    };

    let _fcond = JSON.parse(
      JSON.stringify(this.$store.getters.getCodeList("OPCond"))
    );
    if (_fcond) {
      let _items = [..._fcond.items];
      let sourceobj = {
        source: _items,
        title: "{{item.text}}",
        value: "{{item.id}}",
      };
      _schema.definitions.field.properties.fieldcond.enumSource = [sourceobj];
      _schema.definitions.releationfield.properties.fieldcond.enumSource = [
        sourceobj,
      ];
    }

    // let _fset: Array<any> = [];

    //  try{
    //   const res = await this.codeListService.getItems("CLMetric");
    //   _fset.push(...res);
    //   }catch(er){

    //   }

    // if (_fset) {
    //     let _items = [...[{text:"--",id:""}],..._fset];
    //     let sourceobj={"source":_items,"filterText":"entity12","title": "{{item.text}}", "value": "{{item.id}}"};
    //     _schema.definitions.columnDefs.properties.field.enumSource=[sourceobj];
    // }
    return _schema;
  }

  public showdetail(path:any,arg:any)
  {
       if(path === 'DICT')
       {
           
           let fieldname:any = arg.field;
           this.$http.get("/dst/datamodels/"+this.data.modelid+"/"+fieldname+"/dict").then((response: any) => {
                if(response.data)
                {
                    let newitem : Array<any>=[];
                    response.data.forEach((item:any) =>{
                        if(item.pvalue) 
                            newitem.push({id:item.id,label:item.text,parent:item.pvalue})
                        else
                            newitem.push({id:item.id,label:item.text})
                        })
                    this.$alert('<div style="height:500px;width:100%;overflow:auto;background:#EEEEEE;"><pre>'+JSON.stringify(newitem,null,4)+'<PRE></div>', '字典', {
                    dangerouslyUseHTMLString: true
                    });    
                }
                
            }).catch((response: any) => {
                 
            });
       }
       else
       {
           super.showdetail(path,arg);
       }
  }
}
</script>