<template>
  <el-dialog 
    :title="$t('selectStaff.title')" 
    :visible.sync="visible" 
    width="70%"
    :close-on-click-modal="false"
  >
    <el-row :gutter="20">
      <el-col :span="12" class="border-right">
        <div class="text-center mb-20">
          <h4>{{ $t('selectStaff.orgInfo') }}</h4>
        </div>
        <org-tree-show 
          ref="orgTree" 
          @switchOrg="handleSwitchOrg"
        ></org-tree-show>
      </el-col>

      <el-col :span="12">
        <div class="text-center mb-20">
          <h4>{{ $t('selectStaff.staffInfo') }}</h4>
        </div>
        <div class="staff-list">
          <div 
            v-for="(staff, index) in staffList" 
            :key="index" 
            class="staff-item" 
            :class="{ 'selected': currentStaffId === staff.staffId }"
            @click="handleSelectStaff(staff)"
          >
            <div>
              <i class="el-icon-user"></i>
              {{ staff.name }}
            </div>
            <div>{{ staff.tel }}</div>
          </div>
        </div>
      </el-col>
    </el-row>

    <div 
      v-if="staffData.from === 'bpmn' || staffData.from === 'purchase' || staffData.from === 'contract'"
      slot="footer" 
      class="dialog-footer"
    >
      <el-button @click="handleFirstUser">{{ $t('selectStaff.submitter') }}</el-button>
      <el-button @click="handleCustomUser">{{ $t('selectStaff.dynamicAssign') }}</el-button>
    </div>
  </el-dialog>
</template>

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

export default {
  name: 'SelectStaff',
  components: {
    OrgTreeShow
  },
  data() {
    return {
      visible: false,
      staffData: {},
      staffList: [],
      currentStaffId: '',
      currentOrgId: ''
    }
  },
  methods: {
    open(staff) {
      this.staffData = staff
      this.visible = true
      this.$nextTick(() => {
        this.$refs.orgTree.refreshTree()
      })
    },
    async handleSwitchOrg(org) {
      this.currentOrgId = org.orgId
      try {
        const params = {
          page: 1,
          row: 50,
          orgId: org.orgId
        }
        const res = await queryStaffInfos(params)
        this.staffList = res.data.staffs
        if (this.staffList.length > 0) {
          this.currentStaffId = this.staffList[0].staffId
        }
      } catch (error) {
        this.$message.error(this.$t('selectStaff.loadStaffFailed'))
      }
    },
    handleSelectStaff(staff) {
      this.$emit('change', {
        staffId: staff.userId,
        staffName: staff.userName,
        staffTel: staff.tel
      })
      this.visible = false
    },
    handleFirstUser() {
      this.$emit('change', {
        staffId: '${startUserId}',
        staffName: this.$t('selectStaff.submitter')
      })
      this.visible = false
    },
    handleCustomUser() {
      this.$emit('change', {
        staffId: '${nextUserId}',
        staffName: this.$t('selectStaff.dynamicAssign')
      })
      this.visible = false
    }
  }
}
</script>

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

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

.mb-20 {
  margin-bottom: 20px;
}

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

.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;
  }
}
</style>