<template>
  <Dropdown
    v-model="internalValue"
    :options="options"
    optionValue="value"
    optionLabel="label"
    placeholder="Choisir un chauffeur"
    :disabled="loading || disabled"
    :class="`w-full`"
  >
    <template #option="slotProps">
      {{ slotProps.option.label }}
    </template>
  </Dropdown>
</template>
<script>
import _ from "lodash";

export default {
  props: {
    placeholder: {
      type: String,
      default: "",
    },
    modelValue: {
      type: Number,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },

  watch: {
    internalValue(val) {
      this.$emit("update:modelValue", val);
    },
  },
  data() {
    return {
      internalValue: null,
      loading: false,
      options: [],
    };
  },
  mounted: function () {
    this.getVehicles();
  },
  methods: {
    select(e) {
      if (e.value) {
        this.$emit("update:modelValue", e.value);
      }
    },
    getVehicles() {
      this.loading = true;
      return this.$api
        .call("get", this.$api.route("me.operators.index"))
        .then((payload) => {
          this.options = payload.map((operator) => ({
            label: `${operator.identifier}`,
            value: operator.id,
          }));
        })
        .finally(() => {
          this.loading = false;
        });
    },
  },
};
</script>
