81 lines
2.8 KiB
PHP
81 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Pelanggan;
|
|
use App\Models\PelangganDetail;
|
|
use App\Models\Template; // ✅ tambahkan ini
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
|
|
class KhitanApiController extends Controller
|
|
{
|
|
public function store(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'template_id' => 'required|exists:templates,id',
|
|
'nama_pemesan' => 'required|string|max:255',
|
|
'no_hp' => 'required|string|max:20',
|
|
'email' => 'required|email',
|
|
|
|
// Anak
|
|
'nama_lengkap_anak' => 'required|string|max:255',
|
|
'nama_panggilan_anak' => 'required|string|max:255',
|
|
'bapak_anak' => 'nullable|string|max:255',
|
|
'ibu_anak' => 'nullable|string|max:255',
|
|
|
|
// Jadwal
|
|
'hari_tanggal_acara' => 'nullable|date',
|
|
'waktu_acara' => 'nullable|string',
|
|
'alamat_acara' => 'nullable|string',
|
|
'maps_acara' => 'nullable|string',
|
|
|
|
// Tambahan
|
|
'no_rekening1' => 'nullable|string',
|
|
'no_rekening2' => 'nullable|string',
|
|
'link_musik' => 'nullable|string',
|
|
'galeri' => 'nullable|array|max:5',
|
|
'galeri.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048',
|
|
]);
|
|
|
|
// --- PROSES UPLOAD GAMBAR ---
|
|
$galleryPaths = [];
|
|
if ($request->hasFile('galeri')) {
|
|
foreach ($request->file('galeri') as $file) {
|
|
// Simpan file ke storage/app/public/gallery dan dapatkan path-nya
|
|
$path = $file->store('gallery', 'public');
|
|
$galleryPaths[] = $path;
|
|
}
|
|
}
|
|
// Tambahkan path gambar ke dalam data yang akan disimpan
|
|
$data['galeri'] = $galleryPaths;
|
|
|
|
|
|
// ✅ Ambil template dari database
|
|
$template = Template::with('kategori')->findOrFail($data['template_id']);
|
|
|
|
// ✅ Simpan ke tabel pelanggan
|
|
$pelanggan = Pelanggan::create([
|
|
'nama_pemesan' => $data['nama_pemesan'],
|
|
'nama_template' => $template->nama_template,
|
|
'kategori' => $template->kategori->nama ?? 'khitan',
|
|
'email' => $data['email'],
|
|
'no_tlpn' => $data['no_hp'],
|
|
'harga' => $template->harga,
|
|
]);
|
|
|
|
// ✅ Simpan detail form ke tabel pelanggan_details
|
|
PelangganDetail::create([
|
|
'pelanggan_id' => $pelanggan->id,
|
|
'detail_form' => $data,
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Form khitan berhasil dikirim',
|
|
'data' => $pelanggan->load('details')
|
|
], 201);
|
|
}
|
|
}
|