@extends('layouts.app')
@php $edit = isset($return); @endphp
@section('title', $edit ? "Edit Return {$return->voucher_no}" : 'Sales Return')
@section('breadcrumb')
    @include('components.breadcrumb', ['items' => ['Sales' => null, 'Sales Return' => route('sale-return.index'), $edit ? "Edit" : 'New' => null]])
@endsection
@section('content')
<div class="max-w-3xl mx-auto">
    <x-card :title="$edit ? 'Edit Sales Return' : 'New Sales Return'" icon="undo-2">
        <form action="{{ $edit ? route('sale-return.update', $return->id) : route('sale-return.store') }}" method="POST" class="space-y-4">
            @csrf @if($edit) @method('PUT') @endif
            <div class="grid grid-cols-2 gap-4">
                <x-form-input name="party_id" label="Customer" type="select" icon="user" :options="$customers->pluck('name','id')" :value="old('party_id', $edit ? $return->party_id : '')" required />
                <x-form-input name="return_date" label="Return Date" type="date" icon="calendar" :value="old('return_date', $edit ? $return->return_date->format('Y-m-d') : now()->format('Y-m-d'))" required />
                <x-form-input name="sale_id" label="Original Sale" type="select" icon="receipt" :options="['' => 'Select'] + $sales->pluck('voucher_no','id')->toArray()" :value="old('sale_id', $edit ? $return->sale_id : '')" />
                <x-form-input name="reason" label="Return Reason" type="select" icon="alert-circle" :options="['' => 'Select','defective' => 'Defective','wrong' => 'Wrong Item','excess' => 'Excess','damaged' => 'Damaged','other' => 'Other']" :value="old('reason', $edit ? $return->reason : '')" />
                <x-form-input name="subtotal" label="Return Amount" type="number" icon="circle-dollar-sign" :value="old('subtotal', $edit ? $return->subtotal : 0)" />
                <x-form-input name="deduction" label="Deduction" type="number" icon="minus-circle" :value="old('deduction', $edit ? $return->deduction : 0)" />
            </div>
            <x-form-input name="remarks" label="Remarks" type="textarea" icon="message-square" :value="old('remarks', $edit ? $return->remarks : '')" />
            <div class="flex justify-end gap-2 pt-2">
                <x-button variant="secondary" :href="route('sale-return.index')">Cancel</x-button>
                <x-button variant="danger" icon="save" type="submit">{{ $edit ? 'Update' : 'Save' }} Return</x-button>
            </div>
        </form>
    </x-card>
</div>
@endsection
