using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using OilShopManagement.Data; using OilShopManagement.Models; namespace OilShopManagement.Controllers; [Authorize] public class CustomersController : Controller { private readonly ApplicationDbContext _context; private readonly ILogger _logger; public CustomersController(ApplicationDbContext context, ILogger logger) { _context = context; _logger = logger; } public async Task Index(string? search) { var query = _context.Customers.Where(c => c.IsActive).AsQueryable(); if (!string.IsNullOrWhiteSpace(search)) query = query.Where(c => c.Name.Contains(search) || (c.Phone != null && c.Phone.Contains(search))); ViewBag.Search = search; return View(await query.OrderBy(c => c.Name).ToListAsync()); } public async Task Details(int id) { var customer = await _context.Customers .Include(c => c.Invoices.OrderByDescending(i => i.InvoiceDate).Take(10)) .FirstOrDefaultAsync(c => c.Id == id); if (customer == null) return NotFound(); return View(customer); } public IActionResult Create() => View(); [HttpPost] [ValidateAntiForgeryToken] public async Task Create(Customer customer) { if (ModelState.IsValid) { customer.CreatedAt = DateTime.UtcNow; _context.Customers.Add(customer); await _context.SaveChangesAsync(); TempData["Success"] = "طھظ… ط¥ط¶ط§ظپط© ط§ظ„ط¹ظ…ظٹظ„ ط¨ظ†ط¬ط§ط­"; return RedirectToAction(nameof(Index)); } return View(customer); } public async Task Edit(int id) { var customer = await _context.Customers.FindAsync(id); if (customer == null) return NotFound(); return View(customer); } [HttpPost] [ValidateAntiForgeryToken] public async Task Edit(int id, Customer customer) { if (id != customer.Id) return BadRequest(); if (ModelState.IsValid) { _context.Customers.Update(customer); await _context.SaveChangesAsync(); TempData["Success"] = "طھظ… طھط¹ط¯ظٹظ„ ط§ظ„ط¹ظ…ظٹظ„ ط¨ظ†ط¬ط§ط­"; return RedirectToAction(nameof(Index)); } return View(customer); } [HttpPost] [ValidateAntiForgeryToken] public async Task Delete(int id) { var customer = await _context.Customers.FindAsync(id); if (customer == null) return NotFound(); customer.IsActive = false; await _context.SaveChangesAsync(); TempData["Success"] = "طھظ… ط­ط°ظپ ط§ظ„ط¹ظ…ظٹظ„"; return RedirectToAction(nameof(Index)); } [HttpGet] public async Task Search(string term) { var customers = await _context.Customers .Where(c => c.IsActive && (c.Name.Contains(term) || (c.Phone != null && c.Phone.Contains(term)))) .Select(c => new { c.Id, c.Name, c.Phone, c.CarModel, c.PlateNumber }) .Take(10).ToListAsync(); return Json(customers); } }