<template>
  <el-select 
    v-model="selectValue" 
    :placeholder="placeholder"
    clearable
    filterable
    :disabled="disabled"
    :multiple="multiple"
    @change="handleChange"
    style="width: 100%;"
  >
    <el-option
      v-for="item in list"
      :key="item.id"
      :label="item.name"
      :value="item.id"
    />
  </el-select>
</template>

<script>
import { listAllApp } from "@/api/bst/app";

export default {
  name: "AppSelect",
  props: {
    value: {
      type: [String, Array],
      default: null
    },
    placeholder: {
      type: String,
      default: "请选择应用"
    },
    disabled: {
      type: Boolean,
      default: false
    },
    multiple: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      list: [],
    };
  },
  computed: {
    selectValue: {
      get() {
        return this.value;
      },
      set(val) {
        this.$emit("input", val);
      }
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      listAllApp().then(response => {
        this.list = response.data;
      });
    },  
    handleChange(val) {
      if (this.multiple) {
        let appList = this.list.filter(item => val.includes(item.id));
        this.$emit("change", appList);
      } else {
        let app = this.list.find(item => val === item.id);
        this.$emit("change", app);
      }
    }
  }
};
</script>