67 lines
6.5 KiB
TypeScript
67 lines
6.5 KiB
TypeScript
'use client'
|
||
|
||
import { useEffect, useRef, useState } from 'react'
|
||
import { SALARY_ROLES } from '@/lib/performance-salary-roles'
|
||
import { positionFieldsSchema, type PositionFields, type SalaryPosition } from '@/lib/salary-position-contract'
|
||
import { POSITION_ENDPOINT, salaryRequest } from './SalaryConfigurationContext'
|
||
import { SalaryConfigurationDialog, salaryDanger, salaryInput, salaryPrimary, salarySecondary } from './SalaryConfigurationUI'
|
||
|
||
// Both configuration entries maintain the same stable position, never a second personnel-only list.
|
||
export function CreatePersonnelSalaryPosition({ close, saved }: { close: () => void; saved: (position: SalaryPosition) => void }) {
|
||
const [draft, setDraft] = useState<PositionFields>({ name: '', salaryRole: 'ai-planning', description: '', dimensions: [] })
|
||
const [busy, setBusy] = useState(false), [error, setError] = useState(''), [errors, setErrors] = useState<Record<string, string>>({}), [discard, setDiscard] = useState(false)
|
||
const form = useRef<HTMLFormElement>(null), continueButton = useRef<HTMLButtonElement>(null)
|
||
const dirty = !!draft.name || !!draft.description || draft.salaryRole !== 'ai-planning'
|
||
useEffect(() => {
|
||
if (!dirty) return
|
||
const warn = (event: BeforeUnloadEvent) => { event.preventDefault(); event.returnValue = '' }
|
||
window.addEventListener('beforeunload', warn)
|
||
return () => window.removeEventListener('beforeunload', warn)
|
||
}, [dirty])
|
||
useEffect(() => { if (discard) continueButton.current?.focus() }, [discard])
|
||
function requestClose() { if (!busy) { if (dirty) setDiscard(true); else close() } }
|
||
async function save(event: React.FormEvent) {
|
||
event.preventDefault(); if (busy) return
|
||
const parsed = positionFieldsSchema.safeParse(draft), validation: Record<string, string> = {}
|
||
if (!parsed.success) parsed.error.issues.forEach(issue => { validation[String(issue.path[0])] = issue.message })
|
||
setErrors(validation); setError('')
|
||
if (!parsed.success) { requestAnimationFrame(() => form.current?.querySelector<HTMLElement>('[aria-invalid="true"]')?.focus()); return }
|
||
setBusy(true)
|
||
try { const position = await salaryRequest<SalaryPosition>(POSITION_ENDPOINT, { method: 'POST', body: JSON.stringify(parsed.data) }); saved(position) }
|
||
catch (failure) { setError(failure instanceof Error ? failure.message : '新增失败,草稿已保留,请重试。') }
|
||
finally { setBusy(false) }
|
||
}
|
||
return <SalaryConfigurationDialog title="新增岗位" busy={busy} onClose={requestClose}>
|
||
<form ref={form} onSubmit={save} noValidate className="p-5">
|
||
<p className="mb-5 text-sm leading-6 text-[#66758b]">岗位与“岗位绩效配置”共用。创建后可新增人员,再配置考核维度;岗位归属修改后适用于所有月份。</p>
|
||
<fieldset disabled={busy} className="min-w-0 space-y-4">
|
||
<div><label htmlFor="personnel-position-name" className="block text-sm font-semibold">岗位名称 *</label><input id="personnel-position-name" autoFocus maxLength={100} value={draft.name} aria-invalid={!!errors.name} aria-describedby={errors.name ? 'personnel-position-name-error' : undefined} className={`${salaryInput} mt-2`} onChange={event => setDraft(value => ({ ...value, name: event.target.value }))} />{errors.name && <span id="personnel-position-name-error" className="mt-1 block text-sm text-[#b42318]">{errors.name}</span>}</div>
|
||
<label className="block text-sm font-semibold">薪资展示入口 *<select value={draft.salaryRole} className={`${salaryInput} mt-2`} onChange={event => setDraft(value => ({ ...value, salaryRole: event.target.value as PositionFields['salaryRole'] }))}>{SALARY_ROLES.map(role => <option key={role.key} value={role.key}>{role.title}</option>)}</select></label>
|
||
<label className="block text-sm font-semibold">岗位说明<textarea maxLength={1000} rows={3} value={draft.description} className={`${salaryInput} mt-2 resize-y`} onChange={event => setDraft(value => ({ ...value, description: event.target.value }))} /></label>
|
||
</fieldset>
|
||
{error && <p role="alert" className="mt-4 text-sm leading-6 text-[#b42318]">{error}</p>}
|
||
{discard && <div role="alert" className="mt-5 border-t border-[#e2e8f0] pt-4"><p className="text-sm font-semibold">修改尚未保存,确定放弃吗?</p><div className="mt-3 flex flex-wrap gap-2"><button ref={continueButton} type="button" className={salarySecondary} onClick={() => setDiscard(false)}>继续编辑</button><button type="button" className={salaryDanger} onClick={close}>放弃修改</button></div></div>}
|
||
<footer className="mt-6 flex flex-wrap justify-end gap-3 border-t border-[#e2e8f0] pt-5"><button type="button" disabled={busy} className={salarySecondary} onClick={requestClose}>取消</button><button type="submit" disabled={busy} className={salaryPrimary}>{busy ? '保存中…' : '保存岗位'}</button></footer>
|
||
</form>
|
||
</SalaryConfigurationDialog>
|
||
}
|
||
|
||
export function DeletePersonnelSalaryPosition({ position, close, deleted, refresh }: { position: SalaryPosition; close: () => void; deleted: () => void; refresh: () => void }) {
|
||
const [busy, setBusy] = useState(false), [error, setError] = useState('')
|
||
async function remove() {
|
||
if (busy) return
|
||
setBusy(true); setError('')
|
||
try { await salaryRequest(`${POSITION_ENDPOINT}/${position.id}`, { method: 'DELETE', body: JSON.stringify({ revision: position.revision }) }); deleted() }
|
||
catch (failure) { setError(failure instanceof Error ? failure.message : '删除失败,请重试。') }
|
||
finally { setBusy(false) }
|
||
}
|
||
return <SalaryConfigurationDialog title="删除岗位" busy={busy} onClose={close}>
|
||
<div className="p-5">
|
||
<p className="break-words text-sm leading-6">确定删除“{position.name}”及其绩效配置?两个配置入口中的岗位会同步移除,历史变更记录保留。</p>
|
||
{position.personnelCount > 0 && <p role="alert" className="mt-3 text-sm leading-6 text-[#b42318]">该岗位仍有 {position.personnelCount} 人,请先转移或移除人员薪资档案。</p>}
|
||
{error && <p role="alert" className="mt-3 text-sm leading-6 text-[#b42318]">{error}</p>}
|
||
<footer className="mt-6 flex flex-wrap justify-end gap-3"><button autoFocus className={salarySecondary} disabled={busy} onClick={close}>取消</button>{error && <button className={salarySecondary} disabled={busy} onClick={refresh}>关闭并刷新</button>}<button className={salaryDanger} disabled={busy || position.personnelCount > 0} onClick={() => void remove()}>{busy ? '删除中…' : '确认删除'}</button></footer>
|
||
</div>
|
||
</SalaryConfigurationDialog>
|
||
}
|