<template>
  <el-dialog
    :title="$t('selectStaff.title')"
    :visible.sync="dialogVisible"
    width="70%"
    :close-on-click-modal="false">
    
    <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'">
          </org-tree-show>
        </div>
      </el-col>
      
      <el-col :span="12">
        <div class="text-center section-title">
          {{ $t('selectStaff.staffInfo') }}
        </div>
        <div class="staff-list-container">
          <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 margin-right-xs"></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'"
      class="dialog-footer text-right">
      <el-button size="small" @click="_firstUser">
        {{ $t('selectStaff.submitter') }}
      </el-button>
      <el-button size="small" @click="_customUser">
        {{ $t('selectStaff.dynamicAssign') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { queryStaffInfos } from '@/api/oa/addExamineStaffApi'
import OrgTreeShow from '@/components/oa/OrgTreeShow'

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

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

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

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

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

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

.dialog-footer {
  margin-top: 20px;
}

.margin-right-xs {
  margin-right: 5px;
}

.text-center {
  text-align: center;
}

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