<?php
include "db.php";
header("Content-Type: application/json");

if($_SERVER['REQUEST_METHOD']=="GET"){
 $week=intval($_GET['week']);
 $r=$conn->query("
 SELECT assignyt.role, people.name
 FROM assignyt JOIN people ON people.id=assignyt.person_id
 WHERE week=$week
 ");
 echo json_encode($r->fetch_all(MYSQLI_ASSOC));
 exit;
}

if($_SERVER['REQUEST_METHOD']=="POST"){
 $x=json_decode(file_get_contents("php://input"),true);

 $week=$x['week'];
 $role=$x['role'];
 $ids=$x['personIds'];

 $conn->query("DELETE FROM assignyt WHERE week=$week AND role='$role'");

 foreach($ids as $id){
  $conn->query("INSERT INTO assignyt(week,role,person_id)
   VALUES($week,'$role',$id)");
 }

 echo json_encode(["ok"=>1]);
 exit;
}