import conditionObjectBase from '../condition-object-base.js'
import {
  getStudents
} from '@/api/webApi/back.js'

export default {
  ...conditionObjectBase,
  dependentKeys: ['roleType', 'chatClassId'],
  key: 'students',
  template: 'multiple',
  title: '学生',
  disableAllByForce: true,
  isDependencyReady(param) {
    return param.roleType == 3 && param.chatClassId
  },
  getList: function(param) {
    return new Promise((resolve, reject) => {
      getStudents({
        classId: param.chatClassId
      }).then(res => resolve(res.rows))
        .catch(e => reject(e))
    })
  },
  getCode(item) {
    return item.id
  },
  getLabel(item) {
    return item.customer?.name || ''
  },
  transferToEffectiveDisplayValue(val, list) {
    const safeArrayVal = Array.isArray(val) ? val : isNaN(val) ? [] : [val]
    return list.length && safeArrayVal.every(v => list.some(i => i.code == v)) ? safeArrayVal : []
  },
  codeEquals(codeL, codeR) {
    return codeL?.toString() == codeR?.toString()
  }
}
