{% extends 'base.html' %}
{% block title %}P&L Report{% endblock %}

{% block content %}
<div class="flex justify-between items-center mb-6">
  <h1 class="text-2xl font-bold">Profit & Loss Report</h1>
</div>

<!-- Date Filter -->
<form method="get" class="flex gap-2 items-end mb-6">
  <div>
    <label class="label label-text text-xs">From</label>
    <input type="date" name="start" value="{{ start_date|date:'Y-m-d' }}" class="input input-bordered input-sm"/>
  </div>
  <div>
    <label class="label label-text text-xs">To</label>
    <input type="date" name="end" value="{{ end_date|date:'Y-m-d' }}" class="input input-bordered input-sm"/>
  </div>
  <a href="{% url 'finance:export_profit_loss' %}?start={{ start_date|date:'Y-m-d' }}&end={{ end_date|date:'Y-m-d' }}"
   class="btn btn-outline btn-sm">
  ⬇ Export P&L CSV
</a>
  <button class="btn btn-primary btn-sm">Filter</button>
</form>

<div class="card bg-base-100 shadow max-w-xl">
  <div class="card-body">
    <div class="text-xs text-base-content/40 mb-4">
      Period: {{ start_date|date:"d M Y" }} — {{ end_date|date:"d M Y" }}
    </div>

    <!-- Income -->
    <div class="font-bold text-base mb-2 text-base-content/70">INCOME</div>
    <div class="flex justify-between py-1"><span>Sales Revenue</span><span>{{ business.currency }} {{ revenue|floatformat:0 }}</span></div>
    <div class="flex justify-between py-1 text-base-content/50"><span>Other Income</span><span>{{ business.currency }} {{ total_other_income|floatformat:0 }}</span></div>
    <div class="divider my-1"></div>
    <div class="flex justify-between font-bold py-1">
      <span>Total Income</span>
      <span>{{ business.currency }} {{ revenue|add:total_other_income|floatformat:0 }}</span>
    </div>

    <div class="mt-4"></div>

    <!-- COGS -->
    <div class="font-bold text-base mb-2 text-base-content/70">COST OF GOODS SOLD</div>
    <div class="flex justify-between py-1 text-warning"><span>COGS</span><span>{{ business.currency }} {{ cogs|floatformat:0 }}</span></div>
    <div class="divider my-1"></div>
    <div class="flex justify-between font-bold py-1 text-success">
      <span>GROSS PROFIT</span>
      <span>{{ business.currency }} {{ gross_profit|floatformat:0 }}</span>
    </div>

    <div class="mt-4"></div>

    <!-- Expenses -->
    <div class="font-bold text-base mb-2 text-base-content/70">EXPENSES</div>
    {% for cat in expenses_by_category %}
      <div class="flex justify-between py-1 text-sm">
        <span class="text-base-content/60">{{ cat.category__name }}</span>
        <span class="text-error">{{ business.currency }} {{ cat.total|floatformat:0 }}</span>
      </div>
    {% endfor %}
    <div class="divider my-1"></div>
    <div class="flex justify-between font-bold py-1 text-error">
      <span>Total Expenses</span>
      <span>{{ business.currency }} {{ total_expenses|floatformat:0 }}</span>
    </div>

    <div class="mt-4"></div>

    <!-- Net Profit -->
    <div class="flex justify-between text-xl font-bold py-3 border-t-2 border-base-content/20
      {% if net_profit >= 0 %}text-success{% else %}text-error{% endif %}">
      <span>NET PROFIT</span>
      <span>{{ business.currency }} {{ net_profit|floatformat:0 }}</span>
    </div>

  </div>
</div>
{% endblock %}