import os
import platform

from django import get_version as django_version
from django.shortcuts import render, redirect
from django.utils import timezone


from .models import Product, TankOrder
from .forms import TankOrderForm

def index(request):
    if request.method == 'POST':
        form = TankOrderForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('order_success')
    else:
        form = TankOrderForm()

    products = [
        {
            'name': 'Tangki Air 500L',
            'capacity': 500,
            'price': 150000,
            'description': 'Cocok untuk kebutuhan rumah tangga kecil.'
        },
        {
            'name': 'Tangki Air 1000L',
            'capacity': 1000,
            'price': 250000,
            'description': 'Ideal untuk keluarga sedang atau bisnis kecil.'
        },
        {
            'name': 'Tangki Air 2000L',
            'capacity': 2000,
            'price': 450000,
            'description': 'Pilihan tepat untuk kebutuhan air yang lebih besar.'
        },
    ]

    context = {
        "form": form,
        "products": products,
        "project_description": os.getenv("PROJECT_DESCRIPTION", ""),
        "project_image_url": os.getenv("PROJECT_IMAGE_URL", ""),
    }
    return render(request, "core/index.html", context)

from django.contrib.admin.views.decorators import staff_member_required

def order_success(request):
    return render(request, 'core/order_success.html')

@staff_member_required
def order_list(request):
    orders = TankOrder.objects.all().order_by('-order_date')
    context = {
        'orders': orders,
    }
    return render(request, 'core/order_list.html', context)
