<template>
  <el-dialog
    :title="$t('selectStaff.title')"
    :visible.sync="visible"
    width="70%"
    top="5vh"
  >
    <el-row :gutter="20">
      <el-col :span="12" class="border-right">
        <div class="text-center section-title">
          {{ $t('selectStaff.orgInfo') }}
        </div>
        <div class="org-tree-container">
          <org-tree-show
            ref="orgTree"
            :call-back-listener="'selectStaff'"
          />
        </div>
      </el-col>
      <el-col :span="12">
        <div class="text-center section-title">
          {{ $t('selectStaff.staffInfo') }}
        </div>
        <div class="staff-list">
          <div
            v-for="(item, index) in selectStaffInfo.staffs"
            :key="index"
            class="staff-item"
            :class="{ 'selected': selectStaffInfo.curStaffId === item.staffId }"
            @click="_changeStaff(item)"
          >
            <div>
              <i class="el-icon-user"></i>
              {{ item.name }}
            </div>
            <div>{{ item.tel }}</div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div
      v-if="selectStaffInfo.staff.from === 'bpmn' || 
            selectStaffInfo.staff.from === 'purchase' || 
            selectStaffInfo.staff.from === 'contract'"
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="_firstUser">
        {{ $t('selectStaff.submitter') }}
      </el-button>
      <el-button @click="_customUser">
        {{ $t('selectStaff.dynamicAssign') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { queryStaffInfos } from '@/api/oa/editWorkApi'
import OrgTreeShow from './OrgTreeShow'

export default {
  name: 'SelectStaff',
  components: {
    OrgTreeShow
  },
  data() {
    return {
      visible: false,
      selectStaffInfo: {
        staffs: [],
        curStaffId: '',
        curStaffName: '',
        staff: {}
      }
    }
  },
  methods: {
    open(staff) {
      this.selectStaffInfo.staff = staff || {}
      this.visible = true
      this.$nextTick(() => {
        this.$refs.orgTree.refreshTree()
      })
    },
    _changeStaff(item) {
      this.selectStaffInfo.staff.staffId = item.userId
      this.selectStaffInfo.staff.staffName = item.userName
      this.selectStaffInfo.staff.staffTel = item.tel
      this.visible = false
      if (typeof this.selectStaffInfo.staff.call === 'function') {
        this.selectStaffInfo.staff.call(this.selectStaffInfo.staff)
      }
    },
    async loadStaff(org) {
      try {
        const params = {
          page: 1,
          rows: 50,
          row: 50,
          orgId: org.orgId
        }
        const { data } = await queryStaffInfos(params)
        this.selectStaffInfo.staffs = data.staffs
        if (data.staffs.length > 0) {
          this.selectStaffInfo.curStaffId = data.staffs[0].orgId
        }
      } catch (error) {
        console.error('加载员工失败:', error)
      }
    },
    _firstUser() {
      this.selectStaffInfo.staff.staffId = '${startUserId}'
      this.selectStaffInfo.staff.staffName = this.$t('selectStaff.submitter')
      this.visible = false
      if (typeof this.selectStaffInfo.staff.call === 'function') {
        this.selectStaffInfo.staff.call(this.selectStaffInfo.staff)
      }
    },
    _customUser() {
      this.selectStaffInfo.staff.staffId = '${nextUserId}'
      this.selectStaffInfo.staff.staffName = this.$t('selectStaff.dynamicAssign')
      this.visible = false
      if (typeof this.selectStaffInfo.staff.call === 'function') {
        this.selectStaffInfo.staff.call(this.selectStaffInfo.staff)
      }
    }
  },
  created() {
    this.$on('selectStaff', 'switchOrg', this.loadStaff)
  }
}
</script>

<style lang="scss" scoped>
.border-right {
  border-right: 1px solid #eee;
}

.section-title {
  font-weight: bold;
  margin-bottom: 15px;
}

.org-tree-container {
  padding: 10px;
  height: 500px;
  overflow-y: auto;
}

.staff-list {
  padding: 10px;
  height: 500px;
  overflow-y: auto;
}

.staff-item {
  padding: 10px;
  margin-bottom: 5px;
  border-radius: 4px;
  cursor: pointer;
  &:hover {
    background-color: #f5f7fa;
  }
  &.selected {
    background-color: #ecf5ff;
    color: #409eff;
  }
}

.dialog-footer {
  text-align: right;
}
</style>