<!-- 岗位下拉选择 -->
<template>
  <el-select
    v-model="selected"
    v-bind="$attrs"
    :loading="loading"
    v-on="$listeners"
    @visible-change="handleVisibleChange"
    @change="change"
  >
    <el-option
      v-for="item in options"
      :key="item.id"
      :label="item.code"
      :value="item.code"
    >
      <el-row :gutter="10">
        <el-col :span="8">{{ item.code }}</el-col>
        <el-col :span="16">{{ item.name }}</el-col>
      </el-row>
    </el-option>
  </el-select>

</template>

<script>
import { listPost } from '@/api/system/post'

export default {
  props: {
    value: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      options: [],
      loading: false
    }
  },
  computed: {
    selected: {
      get() {
        return this.value
      },
      set(v) {
        this.$emit('input', v)
      }
    }
  },
  // created() {
  //   this.fetchPostList()
  // },
  methods: {
    fetchPostList() {
      this.loading = true
      const params = {
        pageNo: 1,
        pageSize: 100
      }
      listPost(params).then(res => {
        const { list = [] } = res.data || {}
        this.options = list
      }).finally(() => {
        this.loading = false
      })
    },
    handleVisibleChange(visible) {
      if (visible) {
        this.fetchPostList()
      }
    },
    change(v) {
      const item = this.options.find(i => i.code === v)
      this.$emit('postChange', item)
    }
  }
}
</script>

  <style>

  </style>
