<template>
  <div>
    <base-select v-model="currentValue"
                 v-bind="$attrs"
                 :selectedField="selectedField"
                 @loaded="loaded"
                 @selectChanged="selectChanged"
                 :api="api">
    </base-select>
  </div>
</template>

<script>
import { BaseSelect } from 'newepoch-components'
import confirmationRightDataApi from '@/api/confirmationRightDataApi'
export default {
  name: 'SelectBusinessProcessStatus',
  props: {
    value: {
      type: [Array, String],
      default: function () {
        return null
      }
    }
  },
  components: {
    BaseSelect
  },
  data () {
    return {
      selectedField: ['businessProcessStatusCode', 'businessProcessStatus']
    }
  },
  computed: {
    currentValue: {
      get: function () {
        return this.value
      },
      set: function (value) {
        this.$emit('input', value)
      }
    },
    api () {
      return confirmationRightDataApi.assetModelList
    }
  },
  methods: {
    selectChanged (val, row) {
      this.$emit('selectChanged', val, this.$attrs.name, row)
    },
    loaded (val) {
      let $this = this
      let selectItemts = null
      if (val.length > 0) {
        if (!this.$attrs.multiple) {
          selectItemts = val.find(it => {
            return $this.currentValue === it[$this.selectedField[0]]
          })
        } else {
          selectItemts = val.filter(it => {
            var isTrue = false
            $this.currentValue.forEach(kit => {
              if (kit === it[$this.selectedField[0]]) {
                isTrue = true
              }
            })
            return isTrue
          })
        }
        this.$emit('selectChanged', '', this.name, selectItemts)
      }
    }
  }
}
</script>

<style scoped>

</style>
