<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php init_head(); ?>
<div id="wrapper">
    <div class="content">
        <div class="row">
            <div class="col-md-6 col-md-offset-3">
                <div class="panel panel-primary">
                    <div class="panel-heading">
                        <h4 class="panel-title"><i class="fa fa-unlock"></i> <?php echo $title; ?></h4>
                    </div>
                    <div class="panel-body">
                        <?php echo form_open(admin_url('pos/open_session'), ['id' => 'session-form']); ?>
                        
                        <div class="form-group">
                            <label for="register_id"><?php echo _l('pos_register'); ?> *</label>
                            <select name="register_id" id="register_id" class="form-control selectpicker" required data-live-search="true">
                                <option value=""><?php echo _l('pos_select'); ?></option>
                                <?php foreach ($registers as $register) { ?>
                                    <?php if ($register['is_active']) { ?>
                                        <option value="<?php echo $register['id']; ?>">
                                            <?php echo $register['name'] . ' - ' . $register['branch_name']; ?>
                                        </option>
                                    <?php } ?>
                                <?php } ?>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="opening_balance"><?php echo _l('pos_opening_balance'); ?> *</label>
                            <input type="number" step="0.01" name="opening_balance" id="opening_balance" 
                                   class="form-control" value="0.00" required>
                        </div>

                        <div class="form-group">
                            <label for="notes"><?php echo _l('pos_session_notes'); ?></label>
                            <textarea name="notes" id="notes" class="form-control" rows="3"></textarea>
                        </div>

                        <div class="form-group text-center">
                            <button type="submit" class="btn btn-lg btn-primary">
                                <i class="fa fa-unlock"></i> <?php echo _l('pos_open_session'); ?>
                            </button>
                        </div>

                        <?php echo form_close(); ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php init_tail(); ?>
<script>
$(function() {
    $('#session-form').on('submit', function(e) {
        e.preventDefault();
        
        $.post($(this).attr('action'), $(this).serialize(), function(response) {
            var result = JSON.parse(response);
            if (result.success) {
                alert_float('success', result.message);
                setTimeout(function() {
                    window.location = admin_url + 'pos/screen';
                }, 1000);
            } else {
                alert_float('danger', result.message);
            }
        });
    });
});
</script>
