@extends('layouts.app')

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">

    <div class="card">
        <div class="card-header">
            <div class="row align-items-center">
                <div class="col-md-6">
                    <h5 class="mb-0">Add Expense</h5>
                </div>

                <div class="col-md-6 text-end">
                    <a href="{{ route('expenses.index') }}" class="btn btn-secondary">
                        Back
                    </a>
                </div>
            </div>
            <hr>
        </div>

        <div class="card-body">
            <form action="{{ route('expenses.store') }}" method="POST">
                @csrf

                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label class="form-label">Cattle Category</label>
                        <select name="cattle_category_id" class="form-control @error('cattle_category_id') is-invalid @enderror">
                            <option value="">Select Cattle Category</option>
                            @foreach($cattleCategories as $category)
                                <option value="{{ $category->id }}" {{ old('cattle_category_id') == $category->id ? 'selected' : '' }}>
                                    {{ $category->title }}
                                </option>
                            @endforeach
                        </select>

                        @error('cattle_category_id')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="col-md-6 mb-3">
                        <label class="form-label">Expense Category</label>
                        <select name="expense_category_id" class="form-control @error('expense_category_id') is-invalid @enderror">
                            <option value="">Select Expense Category</option>
                            @foreach($expenseCategories as $category)
                                <option value="{{ $category->id }}" {{ old('expense_category_id') == $category->id ? 'selected' : '' }}>
                                    {{ $category->title }}
                                </option>
                            @endforeach
                        </select>

                        @error('expense_category_id')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="col-md-6 mb-3">
                        <label class="form-label">Title</label>
                        <input type="text"
                               name="title"
                               value="{{ old('title') }}"
                               class="form-control @error('title') is-invalid @enderror"
                               placeholder="Enter expense title">

                        @error('title')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="col-md-6 mb-3">
                        <label class="form-label">Expense Date</label>
                        <input type="date"
                               name="expense_date"
                               value="{{ old('expense_date') }}"
                               class="form-control @error('expense_date') is-invalid @enderror">

                        @error('expense_date')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="col-md-6 mb-3">
                        <label class="form-label">Amount</label>
                        <input type="number"
                               step="0.01"
                               name="amount"
                               value="{{ old('amount', 0) }}"
                               class="form-control @error('amount') is-invalid @enderror"
                               placeholder="Enter amount">

                        @error('amount')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="col-md-12 mb-3">
                        <label class="form-label">Description</label>
                        <textarea name="description"
                                  rows="4"
                                  class="form-control @error('description') is-invalid @enderror"
                                  placeholder="Enter description">{{ old('description') }}</textarea>

                        @error('description')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                </div>

                <button type="submit" class="btn btn-primary">
                    Save
                </button>

                <a href="{{ route('expenses.index') }}" class="btn btn-secondary">
                    Cancel
                </a>
            </form>
        </div>
    </div>

</div>
@endsection