<?php include(APPPATH . 'views/layout/template-top.php'); ?>
<?php include(APPPATH . 'views/layout/header.php'); ?>
<!-- Main Container -->
<div class="main-container">
    <div class="xs-pd-20-10">
        <div class="min-height-200px">
            <!-- Page Header -->
            <div class="page-header mb-30">
                <div class="row">
                    <div class="col-md-6 col-sm-12">
                        <div class="title">
                            <h4>Add New Branch</h4>
                        </div>
                        <nav aria-label="breadcrumb" role="navigation">
                            <ol class="breadcrumb">
                                <li class="breadcrumb-item"><a href="<?php echo base_url(); ?>">Home</a></li>
                                <li class="breadcrumb-item"><a href="<?php echo base_url('branches'); ?>">Branches</a></li>
                                <li class="breadcrumb-item active" aria-current="page">Add</li>
                            </ol>
                        </nav>
                    </div>
                </div>
            </div>

            <!-- Add Branch Form -->
            <div class="pd-20 card-box mb-30">
                <form id="addBranchForm">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Branch Name <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" name="branch_name" required>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Branch Code <span class="text-danger">*</span></label>
                                <input type="text" class="form-control" name="branch_code" required>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                <label>Address</label>
                                <textarea class="form-control" name="branch_address" rows="2"></textarea>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>City</label>
                                <input type="text" class="form-control" name="branch_city">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>State</label>
                                <input type="text" class="form-control" name="branch_state">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Pincode</label>
                                <input type="text" class="form-control" name="branch_pincode" minlength="6" maxlength="6" pattern="[0-9]+" title="Only 6-digit numbers allowed" oninput="this.value = this.value.replace(/[^0-9]/g, '');">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Phone</label>
                                <input type="text" class="form-control" name="branch_phone" minlength="10" maxlength="15" pattern="[0-9]+" title="Only numbers allowed (10-15 digits)" oninput="this.value = this.value.replace(/[^0-9]/g, '');">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                <label>Email</label>
                                <input type="email" class="form-control" name="branch_email">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12 text-right">
                            <button type="submit" class="btn btn-primary" id="btnSaveBranch">Save Branch</button>
                            <a href="<?php echo base_url('branches'); ?>" class="btn btn-secondary">Cancel</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <?php include(APPPATH . 'views/layout/footer.php'); ?>
    </div>
</div>

<?php include(APPPATH . 'views/layout/template-bottom.php'); ?>

<script>
$(document).ready(function() {
    $('#addBranchForm').submit(function(e) {
        e.preventDefault();
        
        var formData = $(this).serialize();
        
        $.ajax({
            url: "<?php echo base_url('branches/store'); ?>",
            type: "POST",
            data: formData,
            dataType: "json",
            success: function(response) {
                if (response.status == 'success') {
                    alert(response.message);
                    window.location.href = response.redirect;
                } else {
                    alert(response.message);
                }
            },
            error: function() {
                alert("Something went wrong. Please try again.");
            }
        });
    });
});
</script>
