@extends('layouts.app')

@section('title', 'Edit Payment')

@section('content')

<div class="row">
    <div class="col-lg-12">

        <div class="card card-transparent card-block card-stretch card-height border-none">

            <div class="card-body p-0 mt-lg-2 mt-0">

                <div class="d-flex flex-wrap align-items-center justify-content-between">

                    <div>
                        <h3 class="mb-1">Edit Payment</h3>

                        <p class="mb-0 text-muted">
                            Update payment information.
                        </p>
                    </div>

                    <div>

                        <a href="{{ route('payments.index') }}"
                           class="btn btn-secondary">

                            <i class="ri-arrow-left-line mr-1"></i>
                            Back to Payments

                        </a>

                    </div>

                </div>

            </div>

        </div>

    </div>
</div>


<div class="row">

    <div class="col-lg-12">

        <div class="card card-block card-stretch card-height">

            <div class="card-header">

                <h4 class="card-title">
                    Edit Payment #{{ $payment->payment_id }}
                </h4>

            </div>

            <div class="card-body">

                @if(session('error'))

                    <div class="alert alert-danger">
                        {{ session('error') }}
                    </div>

                @endif


                @if($errors->any())

                    <div class="alert alert-danger">

                        <strong>Please fix the following errors:</strong>

                        <ul class="mb-0 mt-2">

                            @foreach($errors->all() as $error)

                                <li>{{ $error }}</li>

                            @endforeach

                        </ul>

                    </div>

                @endif


                <form action="{{ route('payments.update', $payment->payment_id) }}"
                      method="POST">

                    @csrf

                    @method('PUT')


                    <div class="row">


                        <!-- Order -->

                        <div class="form-group col-lg-6">

                            <label>
                                Invoice / Order
                                <span class="text-danger">*</span>
                            </label>

                            <select name="order_id"
                                    class="form-control"
                                    required>

                                @foreach($orders as $order)

                                    <option value="{{ $order->order_id }}"
                                        {{ old('order_id', $payment->order_id) == $order->order_id ? 'selected' : '' }}>

                                        {{ $order->invoice->invoice_number ?? 'No Invoice' }}
                                        —
                                        {{ $order->serial_number }}

                                    </option>

                                @endforeach

                            </select>

                        </div>


                        <!-- Customer -->

                        <div class="form-group col-lg-6">

                            <label>
                                Customer
                            </label>

                            <input type="text"
                                   class="form-control"
                                   value="{{ $payment->customer->customer_name ?? 'N/A' }}"
                                   readonly>

                        </div>


                        <!-- Amount -->

                        <div class="form-group col-lg-6">

                            <label>
                                Amount Paid
                                <span class="text-danger">*</span>
                            </label>

                            <div class="input-group">

                                <div class="input-group-prepend">

                                    <span class="input-group-text">
                                        Rs.
                                    </span>

                                </div>

                                <input type="number"
                                       name="amount_paid"
                                       class="form-control"
                                       min="0.01"
                                       step="0.01"
                                       value="{{ old('amount_paid', $payment->amount_paid) }}"
                                       required>

                            </div>

                        </div>


                        <!-- Payment Method -->

                        <div class="form-group col-lg-6">

                            <label>
                                Payment Method
                                <span class="text-danger">*</span>
                            </label>

                            <select name="payment_method"
                                    class="form-control"
                                    required>

                                <option value="">
                                    Select Payment Method
                                </option>

                                @foreach($paymentMethods as $method)

                                    <option value="{{ $method->method_id }}"
                                        {{ old('payment_method', $payment->payment_method) == $method->method_id ? 'selected' : '' }}>

                                        {{ $method->method_type }}

                                    </option>

                                @endforeach

                            </select>

                        </div>


                        <!-- Date -->

                        <div class="form-group col-lg-6">

                            <label>
                                Payment Date
                                <span class="text-danger">*</span>
                            </label>

                            <input type="datetime-local"
                                   name="payment_date"
                                   class="form-control"
                                   value="{{ old(
                                       'payment_date',
                                       $payment->payment_date
                                           ? $payment->payment_date->format('Y-m-d\TH:i')
                                           : ''
                                   ) }}"
                                   required>

                        </div>


                        <!-- Status -->

                        <div class="form-group col-lg-6">

                            <label>
                                Current Status
                            </label>

                            <input type="text"
                                   class="form-control"
                                   value="{{ ucfirst($payment->status->status_name ?? 'N/A') }}"
                                   readonly>

                        </div>


                    </div>


                    <div class="d-flex justify-content-end mt-3">

                        <a href="{{ route('payments.index') }}"
                           class="btn btn-secondary mr-2">

                            Cancel

                        </a>

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

                            <i class="ri-save-line mr-1"></i>
                            Update Payment

                        </button>

                    </div>


                </form>

            </div>

        </div>

    </div>

</div>

@endsection