@extends('layouts.master')
@section('title', 'Company Settings - Create')
@section('content')

<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="fw-bold mb-0">
            <i class="fas fa-building me-2 text-primary"></i> Create Company Settings
        </h4>
        <a href="{{ route('home') }}" class="btn btn-outline-primary">
            <i class="fas fa-arrow-left me-2"></i> Back to Dashboard
        </a>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-header bg-primary text-white py-3">
            <h5 class="mb-0"><i class="fas fa-plus-circle me-2"></i> Create Company Information</h5>
        </div>
        
        <div class="card-body">
            @if($errors->any())
                <div class="alert alert-danger">
                    <ul class="mb-0">
                        @foreach($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif
            
            <form action="{{ route('company-settings.store') }}" method="POST" enctype="multipart/form-data" id="companySettingsForm">
                @csrf
                
                <div class="row mb-4">
                    <div class="col-md-6">
                        <label class="form-label"><strong>Company Logo</strong></label>
                        <input type="file" class="form-control" name="logo" accept="image/*">
                    </div>
                    <div class="col-md-6">
                        <div class="row">
                            <div class="col-md-6">
                                <label class="form-label"><strong>Company Name *</strong></label>
                                <input type="text" class="form-control" name="company_name" value="{{ old('company_name') }}" required>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label"><strong>Company Code *</strong></label>
                                <input type="text" class="form-control" name="company_code" value="{{ old('company_code') }}" required>
                            </div>
                        </div>
                        
                        <div class="row mt-3">
                            <div class="col-md-6">
                                <label class="form-label"><strong>Currency *</strong></label>
                                <select class="form-control select2" name="currency_id" required>
                                    <option value="">Select Currency</option>
                                    @foreach($currencies as $currency)
                                        <option value="{{ $currency->id }}" {{ old('currency_id') == $currency->id ? 'selected' : '' }}>
                                            {{ $currency->name }} ({{ $currency->symbol }})
                                        </option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="col-md-6">
                                <label class="form-label"><strong>TIN No</strong></label>
                                <input type="text" class="form-control" name="tax_id" value="{{ old('tax_id') }}">
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="row mb-4">
                    <div class="col-md-6">
                        <label class="form-label"><strong>Phone *</strong></label>
                        <input type="text" class="form-control" name="phone" value="{{ old('phone') }}" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label"><strong>Email</strong></label>
                        <input type="email" class="form-control" name="email" value="{{ old('email') }}">
                    </div>
                </div>
                
                <div class="row mb-4">
                    <div class="col-md-6">
                        <label class="form-label"><strong>Website</strong></label>
                        <input type="url" class="form-control" name="website" value="{{ old('website') }}">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label"><strong>Physical Address *</strong></label>
                        <textarea class="form-control" name="address" rows="3" required>{{ old('address') }}</textarea>
                    </div>
                </div>
                
                <div class="row mb-4">
                    <div class="col-12">
                        <div class="d-flex justify-content-between align-items-center mb-3">
                            <h5 class="mb-0"><i class="fas fa-university me-2 text-primary"></i> Bank Details</h5>
                            <button type="button" class="btn btn-sm btn-outline-primary" id="addBankDetail">
                                <i class="fas fa-plus me-1"></i> Add Another Bank
                            </button>
                        </div>
                        
                        <div id="bankDetailsContainer">
                            <div class="bank-detail-row card p-3 mb-3 shadow-sm">
                                <div class="row">
                                    <div class="col-md-3">
                                        <label class="form-label">Bank Name *</label>
                                        <input type="text" class="form-control" name="bank_name[]" required>
                                    </div>
                                    <div class="col-md-3">
                                        <label class="form-label">Account Name *</label>
                                        <input type="text" class="form-control" name="account_name[]" required>
                                    </div>
                                    <div class="col-md-2">
                                        <label class="form-label">Account Number *</label>
                                        <input type="text" class="form-control" name="account_number[]" required>
                                    </div>
                                    <div class="col-md-2">
                                        <label class="form-label">Branch</label>
                                        <input type="text" class="form-control" name="branch[]">
                                    </div>
                                    <div class="col-md-1">
                                        <label class="form-label">Swift Code</label>
                                        <input type="text" class="form-control" name="suffix_code[]">
                                    </div>
                                    <div class="col-md-1 d-flex align-items-end">
                                        <button type="button" class="btn btn-sm btn-danger remove-bank" style="display: none;">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="d-flex justify-content-end pt-3 border-top">
                    <button type="submit" class="btn btn-primary me-2">
                        <i class="fas fa-save me-1"></i> Save Settings
                    </button>
                    <button type="reset" class="btn btn-outline-secondary">Reset</button>
                </div>
            </form>
        </div>
    </div>
</div>

@endsection

@section('styles')
<style>
.card {
    border-radius: 12px;
}
.card-header {
    border-bottom: 1px solid rgba(255,255,255,0.2);
}
.form-label {
    font-weight: 500;
    margin-bottom: 0.5rem;
}
.btn {
    border-radius: 6px;
    padding: 8px 16px;
    font-weight: 500;
}
.bank-detail-row {
    border-radius: 8px;
    border: 1px solid #e9ecef;
}
</style>
@endsection

@section('scripts')
<script>
    $(document).ready(function() {
        $('.select2').select2({
            placeholder: "Select Currency",
            allowClear: true,
            width: '100%'
        });
        
        // Add bank detail row
        $('#addBankDetail').click(function() {
            const newRow = `
                <div class="bank-detail-row card p-3 mb-3 shadow-sm">
                    <div class="row">
                        <div class="col-md-3">
                            <label class="form-label">Bank Name *</label>
                            <input type="text" class="form-control" name="bank_name[]" required>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Account Name *</label>
                            <input type="text" class="form-control" name="account_name[]" required>
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">Account Number *</label>
                            <input type="text" class="form-control" name="account_number[]" required>
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">Branch</label>
                            <input type="text" class="form-control" name="branch[]">
                        </div>
                        <div class="col-md-1">
                            <label class="form-label">Swift Code</label>
                            <input type="text" class="form-control" name="suffix_code[]">
                        </div>
                        <div class="col-md-1 d-flex align-items-end">
                            <button type="button" class="btn btn-sm btn-danger remove-bank">
                                <i class="fas fa-trash"></i>
                            </button>
                        </div>
                    </div>
                </div>
            `;
            $('#bankDetailsContainer').append(newRow);
            
            // Show remove buttons if there's more than one bank
            if ($('.bank-detail-row').length > 1) {
                $('.remove-bank').show();
            }
        });
        
        // Remove bank detail row
        $(document).on('click', '.remove-bank', function() {
            $(this).closest('.bank-detail-row').remove();
            
            // Hide remove buttons if there's only one bank left
            if ($('.bank-detail-row').length === 1) {
                $('.remove-bank').hide();
            }
        });
    });
</script>
@endsection