<template>
  <div>
    <div class="white-bg border radius-5 picker flex flex-center">
      <span class="padding">项目阶段：</span>
      <el-select
        class="padding-right"
        style="width: 200px"
        clearable
        v-model="stage"
        @change="changeStage"
      >
        <el-option
          v-for="item in stages"
          :key="item"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    projectId: {
      required: true,
      type: String,
      default: ''
    }
  },
  watch: {
    projectId: {
      handler(val) {
        if (val.length > 0) {
          this.getStage()
        }
      },
      immediate: true
    }
  },
  data() {
    return {
      stages: [],
      stage: ''
    }
  },
  methods: {
    /**
     * 获取项目阶段
     */
    getStage() {
      this.$api.project
        .includeStage({ projectId: this.projectId })
        .then(res => {
          if (res.code === 200) {
            this.stages = res.data
            const tmp = this.stages.find(ele => ele.isLastSelect === 1)
            if (tmp) {
              this.stage = tmp.id
            } else {
              this.stage = this.stages[0].id
            }
            this.$emit('change', this.stage)
          }
        })
    },
    changeStage(res) {
      const tmp = this.stages.find(ele => ele.id === res)
      this.$emit('filter', tmp.sort)
      this.$emit('change', res)
    }
  }
}
</script>

<style lang="scss" scoped>
:deep(.el-input__wrapper) {
  box-shadow: none;
}
</style>
