<template>
  <uni-data-select
    v-model="innerValue"
    :localdata="projectOptions"
    :placeholder="placeholder"
    :clear="clear"
    :filterable="filterable"
    @change="onChange"
  />
</template>

<script>
import uniDataSelect from '@/uni_modules/uni-data-select/components/uni-data-select/uni-data-select.vue'
import { listProject } from '@/api/oa/project'

export default {
  name: 'OaProjectSelect',
  components: { uniDataSelect },
  props: {
    value: [String, Number],
    placeholder: {
      type: String,
      default: '请选择项目'
    },
    clear: {
      type: Boolean,
      default: true
    },
    filterable: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      projectOptions: [],
      innerValue: this.value
    }
  },
  watch: {
    value(val) {
      this.innerValue = val
    },
    innerValue(val) {
      this.$emit('input', val)
      this.$emit('update:value', val)
      this.$emit('change', val)
    }
  },
  mounted() {
    this.getProjectOptions()
  },
  methods: {
    getProjectOptions() {
      listProject().then(res => {
        this.projectOptions = (res.rows || []).map(item => ({
          text: item.projectName,
          value: item.projectId
        }))
      })
    },
    onChange(val) {
      this.innerValue = val
    }
  }
}
</script>
