@extends('layouts.app')
@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-success"></i> Create Company Settings
        </h4>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-header bg-success 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">


            <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 @error('logo') is-invalid @enderror" name="logo" accept="image/*">
                        @error('logo')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </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 @error('company_name') is-invalid @enderror" name="company_name" value="{{ old('company_name') }}" required>
                                @error('company_name')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                            <div class="col-md-6">
                                <label class="form-label"><strong>Company Code *</strong></label>
                                <input type="text" class="form-control @error('company_code') is-invalid @enderror" name="company_code" value="{{ old('company_code') }}" required>
                                @error('company_code')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </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 @error('currency_id') is-invalid @enderror" 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>
                                @error('currency_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>
                            <div class="col-md-6">
                                <label class="form-label"><strong>TIN No</strong></label>
                                <input type="text" class="form-control @error('tax_id') is-invalid @enderror" name="tax_id" value="{{ old('tax_id') }}">
                                @error('tax_id')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </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 @error('website') is-invalid @enderror" name="website" value="{{ old('website') }}">
                        @error('website')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    <div class="col-md-6">
                        <label class="form-label"><strong>Physical Address *</strong></label>
                        <textarea class="form-control @error('address') is-invalid @enderror" name="address" rows="3" required>{{ old('address') }}</textarea>
                        @error('address')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </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-success"></i> Bank Details</h5>
                            <button type="button" class="btn btn-sm btn-outline-success" 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 @error('bank_name.0') is-invalid @enderror" name="bank_name[]" required>
                                        @error('bank_name.0')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                    </div>
                                    <div class="col-md-3">
                                        <label class="form-label">Account Name *</label>
                                        <input type="text" class="form-control @error('account_name.0') is-invalid @enderror" name="account_name[]" required>
                                        @error('account_name.0')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                    </div>
                                    <div class="col-md-2">
                                        <label class="form-label">Account Number *</label>
                                        <input type="text" class="form-control @error('account_number.0') is-invalid @enderror" name="account_number[]" required>
                                        @error('account_number.0')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                    </div>
                                    <div class="col-md-2">
                                        <label class="form-label">Branch</label>
                                        <input type="text" class="form-control @error('branch.0') is-invalid @enderror" name="branch[]">
                                        @error('branch.0')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                    </div>
                                    <div class="col-md-1">
                                        <label class="form-label">Swift Code</label>
                                        <input type="text" class="form-control @error('suffix_code.0') is-invalid @enderror" name="suffix_code[]">
                                        @error('suffix_code.0')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                    </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-success 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
