<?php
$this->load->view ( $left );
$this->load->view ( $header );
?>
<style type="text/css">
    .ibox-content {
        border-style : none;
    }

    .hr-line-dashed {
        margin: 5px 0;
    }
</style>
<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-sm-4">
        <h2><?php echo ($bank_id == 0) ? 'New Bank' : 'Edit Bank';?></h2>
    </div>
</div>
<div class="wrapper wrapper-content">
    <div class="row text-center animated fadeInRightBig">
        <div class="col-lg-12">
            <form method="post" class="form-horizontal" id="bank_data">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Account Name</label>
                            <div class="col-sm-10">
                                <input type="text" name="account_name" id="account_name" class="form-control" placeholder="Account Name" value="<?php echo !empty($bank_data->account_name) ? $bank_data->account_name : '';?>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Account Code</label>
                            <div class="col-sm-10">
                                <input type="text" name="account_code" id="account_code" class="form-control" placeholder="Account Code" value="<?php echo !empty($bank_data->account_code) ? $bank_data->account_code : '';?>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Account Number</label>
                            <div class="col-sm-10">
                                <input type="text" name="account_number" id="account_number" class="form-control" placeholder="Account Number" value="<?php echo !empty($bank_data->account_number) ? $bank_data->account_number : '';?>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Bank Name</label>
                            <div class="col-sm-10">
                                <input type="text" name="bank_name" id="bank_name" class="form-control" placeholder="Bank Name" value="<?php echo !empty($bank_data->bank_name) ? $bank_data->bank_name : '';?>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Routing Number</label>
                            <div class="col-sm-10">
                                <input type="text" name="routing_number" id="routing_number" class="form-control" placeholder="Routing Number" value="<?php echo !empty($bank_data->routing_number) ? $bank_data->routing_number : '';?>">
                            </div>
                        </div>
                        <div class="form-group" style="display:none;">
                            <label class="col-sm-2 control-label">Opening Balance</label>
                            <div class="col-sm-10">
                                <input type="text" name="opening_balance" id="opening_balance" class="form-control" placeholder="Opening Balance" value="0">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Description</label>
                            <div class="col-sm-10">
                                <input type="text" name="description" id="description" class="form-control" placeholder="Description" value="<?php echo !empty($bank_data->description) ? $bank_data->description : '';?>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Primary</label>
                            <div class="col-sm-10">
                                <select name="is_primary" id="is_primary" class="form-control">
                                    <option value="1" <?php echo (isset($bank_data->is_primary) && ($bank_data->is_primary == 1)) ? 'selected="selected"' : '';?>>Primary</option>
                                    <option value="0" <?php echo (isset($bank_data->is_primary) && ($bank_data->is_primary == 0)) ? 'selected="selected"' : '';?>>Non-Primary</option>
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">Status</label>
                            <div class="col-sm-10">
                                <select name="status" id="status" class="form-control">
                                    <option value="1" <?php echo (isset($bank_data->status) && ($bank_data->status == 1)) ? 'selected="selected"' : '';?>>Active</option>
                                    <option value="0" <?php echo (isset($bank_data->status) && ($bank_data->status == 0)) ? 'selected="selected"' : '';?>>In-Active</option>
                                </select>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="ibox">
                    <div class="ibox-content">
                        <div class="row">
                            <div class="col-lg-6 pull-right">
                                <div class="form-group">
                                    <div class="col-sm-12 col-sm-offset-2">
                                        <button class="btn btn-primary save_bank_details" type="button" data-status="1"><span class="m-l-xs">Save</span></button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<?php $this->load->view($footer); ?>
<script type="text/javascript">
    $(document).ready(function(){
        $('.save_bank_details').on('click',function(){
            if ($('#account_name').val() == '') {
                $('#account_name').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#account_name').parent().parent().removeClass('has-error');
            }

            if ($('#account_code').val() == '') {
                $('#account_code').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#account_code').parent().parent().removeClass('has-error');
            }

            if ($('#account_number').val() == '') {
                $('#account_number').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#account_number').parent().parent().removeClass('has-error');
            }

            if ($('#bank_name').val() == '') {
                $('#bank_name').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#bank_name').parent().parent().removeClass('has-error');
            }
            
            if ($('#opening_balance').val() == '') {
                $('#opening_balance').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#opening_balance').parent().parent().removeClass('has-error');
            }

            if ($('#routing_number').val() == '') {
                $('#routing_number').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#routing_number').parent().parent().removeClass('has-error');
            }

            if ($('#description').val() == '') {
                $('#description').parent().parent().addClass('has-error');
                return false;
            } else {
                $('#description').parent().parent().removeClass('has-error');
            }

            var bank_id         = '<?php echo $bank_id; ?>';
            var account_name    = $('#account_name').val();
            var account_code    = $('#account_code').val();
            var account_number  = $('#account_number').val();
            var bank_name       = $('#bank_name').val();
            var routing_number  = $('#routing_number').val(); 
            var opening_balance  = $('#opening_balance').val();
            var description     = $('#description').val();
            var is_primary      = $('#is_primary').val();
            var status          = $('#status').val();

            $.ajax({
                url: '<?php echo site_url('bank/save_bank');?>',
                type: "POST",
                data:{
                    'bank_id'       : bank_id,
                    'account_name'  : account_name,
                    'account_code'  : account_code,
                    'account_number': account_number,
                    'bank_name'     : bank_name,
                    'routing_number': routing_number,
                    'opening_balance': opening_balance,
                    'description'   : description,
                    'is_primary'    : is_primary,
                    'status'        : status
                },
                success: function(data){ 
                    window.location.href = '<?php echo site_url("bank");?>';
                }
            });
        });
    });
</script>
