<template>
  <el-select :value="value" clearable @clear="() => emit(null)" @input="emit">
    <el-option
        v-for="item in storehouse"
        :key="item.id"
        :label="item.name"
        :value="item.id"
    />
  </el-select>
</template>

<script>
import {get} from "@/api/system/storehouse"

export default {
  name: "StorehouseSelector",

  props: {value: Number},

  data: () => ({storehouse: []}),

  methods: {
    init() {
      get.request().then(({data}) => this.storehouse = data)
    },

    emit(v) {
      this.$emit('input', v)
      if (v === 0 || v) {
        let item = this.storehouse.find(i => i.id === v)
        this.$emit('get-name', item ? item.name : null)
      }
      else {
        this.$emit('get-name', null)
      }
    }
  },

  mounted() {
    this.init()
  }
}
</script>
