import { ref } from 'vue' import { updateTech } from 'src/api/dispatch' export function useTechManagement (store, invalidateRoutes) { const editingTech = ref(null) const newTechName = ref('') const newTechPhone = ref('') const newTechDevice = ref('') const addingTech = ref(false) async function saveTechField (tech, field, value) { const trimmed = typeof value === 'string' ? value.trim() : value if (field === 'full_name') { if (!trimmed || trimmed === tech.fullName) return tech.fullName = trimmed } else if (field === 'status') { tech.status = trimmed } else if (field === 'phone') { if (trimmed === tech.phone) return tech.phone = trimmed } try { await updateTech(tech.name || tech.id, { [field]: trimmed }) } catch (_e) { /* save failed */ } } async function addTech () { const name = newTechName.value.trim() if (!name || addingTech.value) return addingTech.value = true try { const tech = await store.createTechnician({ full_name: name, phone: newTechPhone.value.trim() || '', traccar_device_id: newTechDevice.value || '', }) newTechName.value = '' newTechPhone.value = '' newTechDevice.value = '' if (tech.traccarDeviceId) await store.pollGps() } catch (e) { const msg = e?.message || String(e) alert('Erreur création technicien:\n' + msg.replace(/<[^>]+>/g, '')) } finally { addingTech.value = false } } async function removeTech (tech) { if (!confirm(`Supprimer ${tech.fullName} ?`)) return try { const linkedJobs = store.jobs.filter(j => j.assignedTech === tech.id) for (const job of linkedJobs) { await store.unassignJob(job.id) } await store.deleteTechnician(tech.id) } catch (e) { const msg = e?.message || String(e) alert('Erreur suppression:\n' + msg.replace(/<[^>]+>/g, '')) } } async function saveTraccarLink (tech, deviceId) { tech.traccarDeviceId = deviceId || null tech.gpsCoords = null tech.gpsOnline = false await updateTech(tech.name || tech.id, { traccar_device_id: deviceId || '' }) await store.pollGps() invalidateRoutes() } return { editingTech, newTechName, newTechPhone, newTechDevice, addingTech, saveTechField, addTech, removeTech, saveTraccarLink, } }