@extends('backEnd.layouts.master')
@section('title','Add Supplier Purchase')
@section('css')
<link href="{{asset('public/backEnd')}}/assets/libs/select2/css/select2.min.css" rel="stylesheet" type="text/css" />
<link href="{{asset('public/backEnd')}}/assets/css/switchery.min.css" rel="stylesheet" type="text/css" />
@endsection
@section('content')
<div class="container-fluid">
    
    <!-- start page title -->
    <div class="row">
        <div class="col-12">
            <div class="page-title-box">
                <div class="page-title-right">
                    <a href="{{route('supplier-purchases.index')}}" class="btn btn-primary rounded-pill">Manage</a>
                </div>
                <h4 class="page-title">Add Supplier Purchase</h4>
            </div>
        </div>
    </div>       
    <!-- end page title --> 
   <div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-body">
                <form action="{{route('supplier-purchases.store')}}" method="POST" class="row" data-parsley-validate="">
                    @csrf
                    
                    <div class="col-sm-12">
                        <div class="form-group mb-3">
                            <label for="supplier_name" class="form-label">Supplier Name *</label>
                            <input type="text" class="form-control @error('supplier_name') is-invalid @enderror" 
                                   name="supplier_name" value="{{ old('supplier_name') }}" id="supplier_name" 
                                   placeholder="Enter supplier name" required>
                            @error('supplier_name')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>
                    
                    <div class="col-sm-12">
                        <div class="form-group mb-3">
                            <label for="product_name" class="form-label">Product Name *</label>
                            <input type="text" class="form-control @error('product_name') is-invalid @enderror" 
                                   name="product_name" value="{{ old('product_name') }}" id="product_name" 
                                   placeholder="Enter product name" required>
                            @error('product_name')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>
                    
                    <div class="col-sm-6">
                        <div class="form-group mb-3">
                            <label for="quantity" class="form-label">Quantity *</label>
                            <input type="number" class="form-control @error('quantity') is-invalid @enderror" 
                                   name="quantity" value="{{ old('quantity') }}" id="quantity" 
                                   placeholder="0" min="1" step="1" required>
                            @error('quantity')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>
                    
                    <div class="col-sm-6">
                        <div class="form-group mb-3">
                            <label for="unit_price" class="form-label">Unit Price (Tk.) *</label>
                            <input type="number" class="form-control @error('unit_price') is-invalid @enderror" 
                                   name="unit_price" value="{{ old('unit_price') }}" id="unit_price" 
                                   placeholder="0.00" min="0.01" step="0.01" required>
                            @error('unit_price')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>
                    
                    <div class="col-sm-12">
                        <div class="form-group mb-3">
                            <label for="total_price" class="form-label">Total Price (Tk.) <small class="text-muted">(Auto-calculated)</small></label>
                            <input type="number" class="form-control" name="total_price" id="total_price" 
                                   placeholder="0.00" disabled>
                            <small class="text-muted">This will be calculated automatically as: Quantity × Unit Price</small>
                        </div>
                    </div>
                    
                    <div class="col-sm-12">
                        <div class="form-group mb-3">
                            <label for="purchase_date" class="form-label">Purchase Date *</label>
                            <input type="date" class="form-control @error('purchase_date') is-invalid @enderror" 
                                   name="purchase_date" value="{{ old('purchase_date', date('Y-m-d')) }}" 
                                   id="purchase_date" required>
                            @error('purchase_date')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>
                    
                    <div class="col-sm-12">
                        <div class="form-group mb-3">
                            <label for="notes" class="form-label">Notes</label>
                            <textarea class="form-control" name="notes" id="notes" rows="4" 
                                      placeholder="Add any notes about this purchase (optional)">{{ old('notes') }}</textarea>
                        </div>
                    </div>
                    
                    <div class="col-sm-12">
                        <button type="submit" class="btn btn-success rounded-pill">
                            <i class="fe-save"></i> Save Purchase
                        </button>
                        <a href="{{route('supplier-purchases.index')}}" class="btn btn-secondary rounded-pill">
                            <i class="fe-x"></i> Cancel
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
   </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const quantityInput = document.getElementById('quantity');
    const unitPriceInput = document.getElementById('unit_price');
    const totalPriceInput = document.getElementById('total_price');
    
    function calculateTotal() {
        const quantity = parseFloat(quantityInput.value) || 0;
        const unitPrice = parseFloat(unitPriceInput.value) || 0;
        const total = (quantity * unitPrice).toFixed(2);
        totalPriceInput.value = total;
    }
    
    quantityInput.addEventListener('input', calculateTotal);
    unitPriceInput.addEventListener('input', calculateTotal);
    
    // Calculate on page load if values exist
    calculateTotal();
});
</script>

@endsection
