<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");

require_once "../config.php";

$json = file_get_contents("php://input");
$data = json_decode($json);

$modules = $data->modules;

$conn = connect_db();

$res = array();

if ($conn->connect_error) {
	die("Connection failed: " . $conn->connect_error);
} else {
	foreach ($modules as $module) {
		$sql = "
			UPDATE `module`
			SET nameZh = ?, order_sequence = ?, access = ?, active = ?
			WHERE ID = ?
		";

		if ($stmt = $conn->prepare($sql)) {
			$stmt->bind_param("sisii", $param_name_zh, $param_order_sequence, $param_access, $param_active, $param_id);

			$param_name_zh = $module->nameZh;
			$param_order_sequence = intval($module->order_sequence);
			$param_access = $module->access;
			$param_active = intval($module->active);
			$param_id = intval($module->ID);

			if ($stmt->execute()) {
				array_push($res, array("success" => ""));
			} else {
				array_push($res, array("failed" => ""));
			}
		}
	}

}

$conn->close();
echo json_encode($res);
