<template>
  <el-select
    v-model="xmId"
    filterable
    placeholder="请选择"
    :loading="loading"
    :disabled="disabled"
    style="width: 100%;"
    @change="handleProjectChange"
  >
    <el-option
      v-for="item in projectList"
      :key="item.id"
      :label="item.xmmc"
      :value="item.id">
    </el-option>
  </el-select>
</template>

<script>
import { getAllProjectList, getDetail } from "@/api/projectManage/projectList";

export default {
  name: 'ProjectSelect',
  props: {
    value: [String, Number],
    params: Object,
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      xmId: '',
      loading: false,
      projectList: []
    }
  },
  mounted() {
    this.getProjectList();
  },
  methods: {
    findProjectObj(id, callback) {
      if (!id) {
        return;
      }
      getDetail({ id, ...this.params }).then(({ data }) => {
        let detailData = {};
        if (data.success) {
          detailData = {
            ...data.data,
            xmfzrxm: data.data.technicianEntity ? data.data.technicianEntity.name : ''
          };
        }
        callback && callback(detailData);
      })
    },
    getProjectList() {
      getAllProjectList(1, 99999).then(({ data }) => {
        if (data.success) {
          this.projectList = data.data.records;
          this.xmId = this.value;

          this.findProjectObj(this.value, proData => {
            this.$emit('change', proData);
          });
        }
      })
    },
    handleProjectChange(val) {
      this.findProjectObj(val, proData => {
        this.$emit('input', val);
        this.$emit('change', proData);
      });
    },
  }
}
</script>