<template>
  <el-dialog
    :visible.sync="visible"
    :title="$t('selectStaff.title')"
    width="80%"
    @close="handleClose"
  >
    <el-row :gutter="20">
      <el-col :span="12" class="border-right">
        <div class="text-center mb-20">
          <h4>{{ $t('selectStaff.orgInfo') }}</h4>
        </div>
        <div class="org-tree-container">
          <OrgTreeShow ref="orgTree" @switch-org="handleSwitchOrg" />
        </div>
      </el-col>
      <el-col :span="12">
        <div class="text-center mb-20">
          <h4>{{ $t('selectStaff.staffInfo') }}</h4>
        </div>
        <div class="staff-list-container">
          <div
            v-for="item in staffs"
            :key="item.staffId"
            class="staff-item"
            :class="{ 'selected': currentStaffId === item.staffId }"
            @click="selectStaff(item)"
          >
            <div>
              <i class="el-icon-user"></i>
              {{ item.name }}
            </div>
            <div>{{ item.tel }}</div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div 
      v-if="staff.from === 'bpmn' || staff.from === 'purchase' || staff.from === 'contract'"
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="selectFirstUser">{{ $t('selectStaff.submitter') }}</el-button>
      <el-button @click="selectCustomUser">{{ $t('selectStaff.dynamicAssign') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import OrgTreeShow from './orgTreeShow'
import { queryStaffInfos } from '@/api/work/addItemReleaseViewApi'
import { getCommunityId } from '@/api/community/communityApi'

export default {
  name: 'SelectStaff',
  components: {
    OrgTreeShow
  },
  data() {
    return {
      visible: false,
      staff: {},
      staffs: [],
      currentStaffId: '',
      communityId: ''
    }
  },
  created() {
    this.communityId = getCommunityId()
  },
  methods: {
    open(staff) {
      this.staff = staff || {}
      this.visible = true
      this.$nextTick(() => {
        this.$refs.orgTree.refreshTree()
      })
    },
    handleClose() {
      this.visible = false
      this.staffs = []
      this.currentStaffId = ''
    },
    async handleSwitchOrg(org) {
      try {
        const params = {
          page: 1,
          rows: 50,
          row: 50,
          orgId: org.orgId
        }
        const res = await queryStaffInfos(params)
        this.staffs = res.staffs || []
        if (this.staffs.length > 0) {
          this.currentStaffId = this.staffs[0].staffId
        }
      } catch (error) {
        console.error('Failed to load staffs:', error)
      }
    },
    selectStaff(item) {
      this.currentStaffId = item.staffId
      this.$emit('select', {
        staffId: item.userId,
        staffName: item.userName,
        staffTel: item.tel
      })
      this.visible = false
    },
    selectFirstUser() {
      this.$emit('select', {
        staffId: '${startUserId}',
        staffName: this.$t('selectStaff.submitter')
      })
      this.visible = false
    },
    selectCustomUser() {
      this.$emit('select', {
        staffId: '${nextUserId}',
        staffName: this.$t('selectStaff.dynamicAssign')
      })
      this.visible = false
    }
  }
}
</script>

<style scoped>
.border-right {
  border-right: 1px solid #ebeef5;
}
.org-tree-container,
.staff-list-container {
  height: 400px;
  overflow-y: auto;
  padding: 10px;
}
.staff-item {
  padding: 10px;
  margin-bottom: 10px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  cursor: pointer;
}
.staff-item:hover {
  background-color: #f5f7fa;
}
.staff-item.selected {
  background-color: #ecf5ff;
  border-color: #d9ecff;
}
.text-center {
  text-align: center;
}
.mb-20 {
  margin-bottom: 20px;
}
</style>