@extends('layouts.app')

@section('page_title', 'Add Branch')

@section('content')
    <div class="card-header border-0 bg-transparent py-2">
        <h5 class="mb-0 fw-bold text-dark" style="font-size: 1.25rem;">Add Branch</h5>
    </div>
    <div class="row justify-content-center">
        <div class="card-body p-3">
            <form id="addBranchForm">
                <div class="mb-3">
                    <label for="branchCode" class="form-label">Branch Code</label>
                    <input type="text" class="form-control" id="branchCode" placeholder="Enter branch code" required>
                </div>
                <div class="mb-3">
                    <label for="branchName" class="form-label">Branch Name</label>
                    <input type="text" class="form-control" id="branchName" placeholder="Enter branch name" required>
                </div>
                <div class="mb-3">
                    <label for="details" class="form-label">Details</label>
                    <textarea class="form-control" id="details" rows="6" placeholder="Enter details" required></textarea>
                </div>
                <div class="d-flex justify-content-end mt-3">
                    <button type="button" class="btn btn-outline-secondary me-2" style="margin-right:5px">Cancel</button>
                    <button type="submit" class="btn btn-primary">Add Branch</button>
                </div>
            </form>
        </div>
    </div>

    <style>
        .card {
            border-radius: 5px;
            border: 1px solid #dee2e6;
        }
        .form-label {
            font-size: 0.95rem;
        }
        .form-control,
        textarea.form-control {
            font-size: 0.9rem;
            width: 100%;
        }
    </style>

    <script>
        document.getElementById('addBranchForm').addEventListener('submit', function(e) {
            e.preventDefault();

            const branchCode = document.getElementById('branchCode').value;
            const branchName = document.getElementById('branchName').value;
            const details = document.getElementById('details').value;

            if (!branchCode || !branchName || !details) {
                alert('Please fill out all required fields.');
                return;
            }

            const currentDate = new Date().toISOString().split('T')[0]; // e.g., "2025-04-15"
            const newBranch = {
                id: Date.now(),
                branchCode,
                branchName,
                details,
                dateCreated: currentDate,
                createdBy: 'Admin',
                dateModified: currentDate
            };

            let branches = JSON.parse(sessionStorage.getItem('branches') || '[]');
            branches.push(newBranch);
            sessionStorage.setItem('branches', JSON.stringify(branches));

            alert('Branch added successfully!');
            window.location.href = '/branches';
        });

        document.querySelector('.btn-outline-secondary').addEventListener('click', function() {
            window.location.href = '/branches';
        });
    </script>
@endsection