File manager - Edit - /home/webapp69.cm.in.th/u69319090041/Shop41/public/admin/bank_accounts.php
Back
<?php /** * public/admin/bank_accounts.php * จัดการบัญชีธนาคารกลางของเว็บไซต์ (Admin Bank Management) */ require_once __DIR__ . '/../../config/config.php'; require_once __DIR__ . '/../../includes/auth.php'; require_once __DIR__ . '/../../includes/image_helper.php'; requireAdmin(); $db = getDBConnection(); $error = ''; // ── HANDLE POST ACTIONS (Add / Edit) ────────────────────────── if ($_SERVER['REQUEST_METHOD'] === 'POST') { verifyCSRFToken($_POST['csrf_token'] ?? ''); $formAction = sanitize($_POST['form_action'] ?? ''); $bankId = (int)($_POST['bank_id'] ?? 0); $bankName = sanitize($_POST['bank_name'] ?? ''); $accNumber = sanitize($_POST['account_number'] ?? ''); $accName = sanitize($_POST['account_name'] ?? ''); $sortOrder = (int)($_POST['sort_order'] ?? 0); $isActive = isset($_POST['is_active']) ? 1 : 0; if (empty($bankName) || empty($accNumber) || empty($accName)) { $error = 'กรุณากรอกข้อมูลธนาคาร เลขที่บัญชี และชื่อบัญชีให้ครบถ้วน'; } else { $qrImagePath = ''; if (isset($_FILES['qr_code_image']) && $_FILES['qr_code_image']['error'] === UPLOAD_ERR_OK) { $uploadRes = processAndUploadImage($_FILES['qr_code_image'], 'bank_qrs'); if ($uploadRes['success']) { $qrImagePath = $uploadRes['main_path']; } else { $error = 'อัปโหลดรูปภาพล้มเหลว: ' . $uploadRes['error']; } } if (empty($error)) { if ($formAction === 'edit' && $bankId > 0) { if ($qrImagePath !== '') { $stmt = $db->prepare("UPDATE system_bank_accounts SET bank_name=?, account_number=?, account_name=?, qr_code_image=?, sort_order=?, is_active=? WHERE id=?"); $stmt->execute([$bankName, $accNumber, $accName, $qrImagePath, $sortOrder, $isActive, $bankId]); } else { $stmt = $db->prepare("UPDATE system_bank_accounts SET bank_name=?, account_number=?, account_name=?, sort_order=?, is_active=? WHERE id=?"); $stmt->execute([$bankName, $accNumber, $accName, $sortOrder, $isActive, $bankId]); } $_SESSION['flash_success'] = 'แก้ไขข้อมูลบัญชีธนาคารเรียบร้อยแล้ว'; } else { $stmt = $db->prepare("INSERT INTO system_bank_accounts (bank_name, account_number, account_name, qr_code_image, sort_order, is_active) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$bankName, $accNumber, $accName, $qrImagePath, $sortOrder, $isActive]); $_SESSION['flash_success'] = 'เพิ่มบัญชีธนาคารใหม่เรียบร้อยแล้ว'; } header('Location: bank_accounts.php'); exit; } } } // ── HANDLE GET ACTIONS (Delete / Toggle) ────────────────────── if (isset($_GET['action'])) { $actId = (int)($_GET['id'] ?? 0); if ($_GET['action'] === 'delete' && $actId > 0) { $stmt = $db->prepare("DELETE FROM system_bank_accounts WHERE id=?"); $stmt->execute([$actId]); $_SESSION['flash_success'] = 'ลบบัญชีธนาคารเรียบร้อยแล้ว'; header('Location: bank_accounts.php'); exit; } elseif ($_GET['action'] === 'toggle' && $actId > 0) { $stmt = $db->prepare("UPDATE system_bank_accounts SET is_active = 1 - is_active WHERE id=?"); $stmt->execute([$actId]); $_SESSION['flash_success'] = 'เปลี่ยนสถานะการใช้งานเรียบร้อยแล้ว'; header('Location: bank_accounts.php'); exit; } } // Fetch all bank accounts $bankAccounts = $db->query("SELECT * FROM system_bank_accounts ORDER BY sort_order ASC, id ASC")->fetchAll(); $pageTitle = "จัดการบัญชีธนาคารกลาง"; require_once __DIR__ . '/../../includes/admin_header.php'; ?> <div class="d-flex justify-content-between align-items-center mb-4"> <div> <h3 class="fw-bold mb-1">🏦 จัดการบัญชีธนาคารกลาง (Admin Bank Management)</h3> <p class="text-muted mb-0 small">จัดการบัญชีธนาคารหลักของเว็บไซต์สำหรับรับชำระเงินจากลูกค้าในหน้า Checkout</p> </div> <button class="btn btn-primary-custom px-4 rounded-pill" data-bs-toggle="modal" data-bs-target="#bankModal" onclick="resetBankForm()"> + เพิ่มบัญชีธนาคาร </button> </div> <?php if (!empty($error)): ?> <div class="alert alert-danger rounded-3"><?php echo $error; ?></div> <?php endif; ?> <?php if (isset($_SESSION['flash_success'])): ?> <div class="alert alert-success rounded-3 alert-dismissible fade show" role="alert"> <?php echo $_SESSION['flash_success']; unset($_SESSION['flash_success']); ?> <button type="button" class="btn-close" data-bs-dismiss="alert"></button> </div> <?php endif; ?> <div class="card card-custom p-4"> <div class="table-responsive"> <table class="table table-hover align-middle mb-0"> <thead class="table-light"> <tr> <th style="width:70px">ลำดับ</th> <th style="width:90px">รูป/QR Code</th> <th>ธนาคาร</th> <th>เลขที่บัญชี</th> <th>ชื่อบัญชี</th> <th class="text-center" style="width:90px">ลำดับแสดง</th> <th class="text-center" style="width:110px">สถานะ</th> <th class="text-center" style="width:160px">จัดการ</th> </tr> </thead> <tbody> <?php if (empty($bankAccounts)): ?> <tr> <td colspan="8" class="text-center py-5 text-muted"> <div style="font-size:2.5rem">🏦</div> <div>ยังไม่มีบัญชีธนาคารกลางในระบบ</div> <button class="btn btn-sm btn-primary-custom mt-2" data-bs-toggle="modal" data-bs-target="#bankModal" onclick="resetBankForm()">+ เพิ่มบัญชีแรก</button> </td> </tr> <?php else: ?> <?php foreach ($bankAccounts as $idx => $b): ?> <tr> <td class="fw-bold text-muted"><?php echo $idx + 1; ?></td> <td> <?php if ($b['qr_code_image']): ?> <img src="<?php echo UPLOAD_URL . htmlspecialchars($b['qr_code_image']); ?>" class="rounded border shadow-sm" style="width:50px;height:50px;object-fit:cover cursor:pointer" onclick="window.open(this.src, '_blank')" alt="QR Code"> <?php else: ?> <div class="bg-light border text-muted rounded text-center small py-2" style="width:50px;height:50px;font-size:10px">ไม่มีรูป</div> <?php endif; ?> </td> <td><strong class="text-primary"><?php echo htmlspecialchars($b['bank_name']); ?></strong></td> <td><span class="font-monospace fw-bold fs-6 text-dark"><?php echo htmlspecialchars($b['account_number']); ?></span></td> <td><?php echo htmlspecialchars($b['account_name']); ?></td> <td class="text-center"><span class="badge bg-light text-dark border"><?php echo $b['sort_order']; ?></span></td> <td class="text-center"> <a href="?action=toggle&id=<?php echo $b['id']; ?>" class="badge text-decoration-none px-3 py-2 rounded-pill <?php echo $b['is_active'] ? 'bg-success' : 'bg-secondary'; ?>"> <?php echo $b['is_active'] ? '✅ เปิดใช้งาน' : '⛔ ปิดใช้งาน'; ?> </a> </td> <td class="text-center"> <button class="btn btn-sm btn-edit-custom rounded-pill me-1" onclick='openEditBankModal(<?php echo json_encode($b); ?>)'>✏️ แก้ไข</button> <a href="?action=delete&id=<?php echo $b['id']; ?>" class="btn btn-sm btn-outline-danger rounded-pill" onclick="return confirm('ยืนยันลบบัญชีธนาคารนี้?')">🗑️ ลบ</a> </td> </tr> <?php endforeach; ?> <?php endif; ?> </tbody> </table> </div> </div> <!-- ── BANK ACCOUNT MODAL ────────────────────────────────────── --> <div class="modal fade" id="bankModal" tabindex="-1"> <div class="modal-dialog modal-lg"> <div class="modal-content rounded-4"> <form method="POST" enctype="multipart/form-data"> <input type="hidden" name="csrf_token" value="<?php echo generateCSRFToken(); ?>"> <input type="hidden" name="form_action" id="form_action" value="add"> <input type="hidden" name="bank_id" id="bank_id" value="0"> <div class="modal-header border-0 pb-0"> <h5 class="modal-title fw-bold" id="bankModalTitle">+ เพิ่มบัญชีธนาคารกลาง</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body pt-3"> <div class="row g-3"> <div class="col-md-6"> <label class="form-label fw-semibold">ชื่อธนาคาร / บริการ <span class="text-danger">*</span></label> <input type="text" name="bank_name" id="bank_name" class="form-control" required placeholder="เช่น ธนาคารกสิกรไทย, PromptPay"> </div> <div class="col-md-6"> <label class="form-label fw-semibold">เลขที่บัญชี / เบอร์พร้อมเพย์ <span class="text-danger">*</span></label> <input type="text" name="account_number" id="account_number" class="form-control" required placeholder="เช่น 123-4-56789-0 หรือ 0812345678"> </div> <div class="col-md-6"> <label class="form-label fw-semibold">ชื่อบัญชี <span class="text-danger">*</span></label> <input type="text" name="account_name" id="account_name" class="form-control" required placeholder="เช่น บริษัท 41Shop จำกัด"> </div> <div class="col-md-6"> <label class="form-label fw-semibold">ลำดับการแสดงผล</label> <input type="number" name="sort_order" id="sort_order" class="form-control" value="0" min="0"> </div> <div class="col-12"> <label class="form-label fw-semibold">รูปภาพ QR Code / โลโก้ธนาคาร</label> <input type="file" name="qr_code_image" class="form-control" accept="image/*" onchange="previewBankImg(this)"> <div id="bank_img_preview" class="mt-2" style="display:none"> <small class="text-muted d-block mb-1">รูปภาพปัจจุบัน/ตัวอย่าง:</small> <img id="bank_preview_tag" class="rounded border" style="max-width:140px;max-height:140px;object-fit:cover"> </div> </div> <div class="col-12"> <div class="form-check form-switch mt-2"> <input class="form-check-input" type="checkbox" name="is_active" id="is_active" checked> <label class="form-check-label fw-semibold" for="is_active">เปิดใช้งานบัญชีนี้ในหน้าชำระเงิน (Checkout)</label> </div> </div> </div> </div> <div class="modal-footer border-0"> <button type="button" class="btn btn-light rounded-pill" data-bs-dismiss="modal">ยกเลิก</button> <button type="submit" class="btn btn-primary-custom rounded-pill px-4">💾 บันทึกข้อมูล</button> </div> </form> </div> </div> </div> <script> function resetBankForm() { document.getElementById('bankModalTitle').textContent = '+ เพิ่มบัญชีธนาคารกลาง'; document.getElementById('form_action').value = 'add'; document.getElementById('bank_id').value = '0'; document.getElementById('bank_name').value = ''; document.getElementById('account_number').value = ''; document.getElementById('account_name').value = ''; document.getElementById('sort_order').value = '0'; document.getElementById('is_active').checked = true; document.getElementById('bank_img_preview').style.display = 'none'; } function openEditBankModal(b) { document.getElementById('bankModalTitle').textContent = '✏️ แก้ไขบัญชีธนาคารกลาง'; document.getElementById('form_action').value = 'edit'; document.getElementById('bank_id').value = b.id; document.getElementById('bank_name').value = b.bank_name; document.getElementById('account_number').value = b.account_number; document.getElementById('account_name').value = b.account_name; document.getElementById('sort_order').value = b.sort_order || 0; document.getElementById('is_active').checked = (parseInt(b.is_active) === 1); if (b.qr_code_image) { document.getElementById('bank_preview_tag').src = SITE_URL + '/uploads/' + b.qr_code_image; document.getElementById('bank_img_preview').style.display = 'block'; } else { document.getElementById('bank_img_preview').style.display = 'none'; } new bootstrap.Modal(document.getElementById('bankModal')).show(); } function previewBankImg(input) { if (input.files && input.files[0]) { const reader = new FileReader(); reader.onload = e => { document.getElementById('bank_preview_tag').src = e.target.result; document.getElementById('bank_img_preview').style.display = 'block'; }; reader.readAsDataURL(input.files[0]); } } </script> <?php require_once __DIR__ . '/../../includes/admin_footer.php'; ?>
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.28 |
proxy
|
phpinfo
|
Settings