@extends('layouts.app')

@section('content')
<div class="row">
    <div class="col-sm-12">
        <div class="page-title-box">
            <h4 class="page-title">Edit Recurring Plan</h4>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <form id="recurring-form" action="{{ route('finance.recurring.update', $recurring->id) }}" method="POST">
                    @csrf
                    @method('PUT')
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Client</label>
                            <select name="client_id" class="form-select" required>
                                <option value="">Select Client</option>
                                @foreach($clients as $client)
                                    <option value="{{ $client->id }}" {{ $client->id == $recurring->client_id ? 'selected' : '' }}>{{ $client->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Plan Name / Description</label>
                            <input type="text" name="name" class="form-control" value="{{ $recurring->name }}" required>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Billing Frequency</label>
                            <select name="frequency" class="form-select" required>
                                <option value="daily" {{ $recurring->frequency == 'daily' ? 'selected' : '' }}>Daily</option>
                                <option value="weekly" {{ $recurring->frequency == 'weekly' ? 'selected' : '' }}>Weekly</option>
                                <option value="monthly" {{ $recurring->frequency == 'monthly' ? 'selected' : '' }}>Monthly</option>
                                <option value="yearly" {{ $recurring->frequency == 'yearly' ? 'selected' : '' }}>Yearly</option>
                            </select>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Amount (Price)</label>
                            <input type="number" name="price" class="form-control" value="{{ $recurring->price }}" min="0" step="0.01" required>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Tax</label>
                            <select name="tax_id" class="form-select">
                                <option value="">No Tax</option>
                                @foreach($taxes as $tax)
                                    <option value="{{ $tax->id }}" {{ $recurring->tax_id == $tax->id ? 'selected' : '' }}>{{ $tax->name }} ({{ $tax->rate }}%)</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Status</label>
                            <select name="status" class="form-select" required>
                                <option value="active" {{ $recurring->status == 'active' ? 'selected' : '' }}>Active</option>
                                <option value="paused" {{ $recurring->status == 'paused' ? 'selected' : '' }}>Paused</option>
                            </select>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">Start Date</label>
                            <input type="date" name="start_date" class="form-control" required value="{{ $recurring->start_date ? \Carbon\Carbon::parse($recurring->start_date)->format('Y-m-d') : date('Y-m-d') }}">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">End Date (Optional)</label>
                            <input type="date" name="end_date" class="form-control" value="{{ $recurring->end_date ? \Carbon\Carbon::parse($recurring->end_date)->format('Y-m-d') : '' }}">
                        </div>
                    </div>
                    
                    <div class="mt-4 text-end">
                        <a href="{{ route('finance.recurring.index') }}" class="btn btn-secondary me-2">Cancel</a>
                        <button type="button" class="btn btn-primary" id="save-btn">Update Plan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection

@section('scripts')
<script>
$(document).ready(function() {
    $('#save-btn').click(function() {
        $.ajax({
            url: $('#recurring-form').attr('action'),
            method: 'POST',
            data: $('#recurring-form').serialize(),
            success: function(res) {
                if(res.status) {
                    Swal.fire('Success', res.message, 'success').then(() => {
                        window.location.href = "{{ route('finance.recurring.index') }}";
                    });
                }
            },
            error: function(xhr) {
                Swal.fire('Error', 'Please fill all required fields correctly', 'error');
            }
        });
    });
});
</script>
@endsection
