85 lines
3.0 KiB
PHP
85 lines
3.0 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
require_once '../includes/db.php';
|
|
require_once '../includes/auth.php';
|
|
|
|
if (!isset($_SESSION['user_id'])) {
|
|
echo json_encode(['success' => false, 'message' => 'Unauthorized']);
|
|
exit;
|
|
}
|
|
|
|
// Ensure tables exist
|
|
try {
|
|
$pdo->exec("
|
|
CREATE TABLE IF NOT EXISTS `radio_test_sessions` (
|
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
|
`date` date NOT NULL,
|
|
`time` time NOT NULL,
|
|
`shift` varchar(20) NOT NULL,
|
|
`tester_id` int(11) NOT NULL,
|
|
`tested_count` int(11) NOT NULL DEFAULT 0,
|
|
`untested_count` int(11) NOT NULL DEFAULT 0,
|
|
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
|
|
PRIMARY KEY (`id`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
|
|
CREATE TABLE IF NOT EXISTS `radio_test_records` (
|
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
|
`session_id` int(11) NOT NULL,
|
|
`station_id` varchar(50) NOT NULL,
|
|
`station_name` varchar(100) NOT NULL,
|
|
`tx_level` varchar(20) NOT NULL,
|
|
`rx_level` varchar(20) NOT NULL,
|
|
`channel_type` varchar(10) NOT NULL DEFAULT 'T',
|
|
`note` text,
|
|
PRIMARY KEY (`id`),
|
|
KEY `session_id` (`session_id`),
|
|
CONSTRAINT `radio_test_records_ibfk_1` FOREIGN KEY (`session_id`) REFERENCES `radio_test_sessions` (`id`) ON DELETE CASCADE
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
");
|
|
} catch (Exception $e) {}
|
|
|
|
// Process request
|
|
$data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$data) {
|
|
echo json_encode(['success' => false, 'message' => 'Invalid data payload.']);
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$pdo->beginTransaction();
|
|
|
|
$stmtSession = $pdo->prepare("INSERT INTO radio_test_sessions (date, time, shift, tester_id, tested_count, untested_count) VALUES (?, ?, ?, ?, ?, ?)");
|
|
$stmtSession->execute([
|
|
$data['date'],
|
|
$data['time'],
|
|
$data['shift'],
|
|
$_SESSION['user_id'],
|
|
$data['tested_count'],
|
|
$data['untested_count']
|
|
]);
|
|
|
|
$sessionId = $pdo->lastInsertId();
|
|
|
|
$stmtRecord = $pdo->prepare("INSERT INTO radio_test_records (session_id, station_id, station_name, tx_level, rx_level, channel_type, note) VALUES (?, ?, ?, ?, ?, ?, ?)");
|
|
|
|
foreach ($data['items'] as $item) {
|
|
$stmtRecord->execute([
|
|
$sessionId,
|
|
$item['station_id'],
|
|
$item['station_name'],
|
|
$item['tx_level'],
|
|
$item['rx_level'],
|
|
$item['channel_type'],
|
|
$item['note'] ?? ''
|
|
]);
|
|
}
|
|
|
|
$pdo->commit();
|
|
echo json_encode(['success' => true, 'message' => 'บันทึกผลทดสอบวิทยุ ว.16 สำเร็จเรียบร้อยแล้ว!', 'session_id' => $sessionId]);
|
|
} catch (PDOException $e) {
|
|
$pdo->rollBack();
|
|
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
|
|
}
|