<template>
  <CheckboxGroup v-model="sels" @on-change="onchange">
    <Checkbox
      v-for="item in datasource"
      :label="item.value"
      :key="item.value"
      :Border="isborder"
    >{{item.label}}</Checkbox>
  </CheckboxGroup>
</template>
<script>
import { refQuery } from "@/api/meta";
export default {
  name: "Checkgroups",
  components: {},
  model: {
    prop: "value",
    event: "change"
  },
  props: [
    "value",
    "dicName",
    "refModel",
    "labelField",
    "valueField",
    "refwhere",
    "isborder",
    "baseData"
  ],
  data() {
    return {
      sels: this.value ? this.value : [],
      datasource: this.baseData ? this.baseData : []
    };
  },
  watch: {
    value(nv, ov) {
      this.sels = nv;
    }
  },
  methods: {
    onchange(v) {
      this.$emit("change", this.sels);
    },
    initDataSource(slike) {
      if (this.dicName && this.dicName != "") {
        let objdic = this.$store.getters.dict_info[this.dicName];
        console.log(objdic);
        Object.keys(objdic).forEach(k => {
          this.datasource.push({
            value: k,
            label: objdic[k]
          });
        });
      } else {
        let query = {};
        let url = "/web/common/" + this.refModel + "Ctl/refQuery";
        query.fields = [this.labelField, this.valueField];
        query.likestr = slike;
        query.refwhere = this.refwhere ? this.refwhere : {};
        refQuery(url, query).then(r => {
          if (r.data) {
            this.datasource = r.data;
            console.log(">>>>>>>>>>>>>>>>>>>>", r.data);
          }
        });
      }
    }
  },
  created() {
    if (!this.baseData) {
      this.initDataSource();
    }
  },
  mounted() {}
};
</script>
<style>
</style>
