<template>
  <el-select
    v-model="selected"
    class="size-select"
    placeholder="请选择"
    @change="select"
    :style="styles"
    :multiple="multiple"
    clearable
  >
    <el-option
      v-for="item in optionList"
      :key="item.roleCode"
      :label="item.roleName"
      :value="item.roleCode"
    >
      <span>{{ item.roleName }}</span>
    </el-option>
  </el-select>
</template>

<script>
export default {
  name: "RoleSelect",
  props: {
    value: [String, Array],
    multiple: Boolean,
    styles: { type: String },
  },
  data() {
    return {
      optionList: [],
      selected: "",
    };
  },
  async created() {
    const res = await this.$http.post("/api/admin/sys/role/query");
    // console.log(res.data);
    this.optionList = res.data.data;
  },
  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.selected = val;
      },
    },
  },
  methods: {
    select() {
      if (this.multiple && this.selected.length > 1) {
        this.selected = this.selected.splice(1);
      }
      this.$emit("input", this.selected);
      this.$emit("change", this.selected);
    },
  },
};
</script>

<style></style>
