<?php
$vError = isset($validation_error) && !empty($validation_error) ? $validation_error : array();
$vSaved = isset($saved_data) && !empty($saved_data) ? $saved_data : array();
$vUrl = isset($edit_url) ? $edit_url : 'lbadmin/gateway/add';
$vPosted = array();
$dynamicFields = array(
    'trxid' => array(
        'type' => 'Text Field',
        'placeholder' => 'Ex. Transaction ID (trxid)'
    ),
    'senderac' => array(
        'type' => 'Text Field',
        'placeholder' => 'Ex. Account No'
    ),
    'docupload' => array(
        'type' => 'File Upload',
        'placeholder' => 'Ex. Upload Scan Copy'
    ),
    'trxnote' => array(
        'type' => 'Text Area',
        'placeholder' => 'Ex. Transaction Note'
    )
);

if(!empty(set_value('gatewayname'))){
    $vPosted['gatewayname'] = set_value('gatewayname');
}

if(!empty(set_value('gatewayslug'))){
    $vPosted['gatewayslug'] = set_value('gatewayslug');
}

if(!empty(set_value('gatewayrbkey'))){
    $vPosted['gatewayrbkey'] = set_value('gatewayrbkey');
}

if(!empty(set_value('gatewaycharge'))){
    $vPosted['gatewaycharge'] = set_value('gatewaycharge');
}

if(!empty(set_value('gatewayinstruction'))){
    $vPosted['gatewayinstruction'] = set_value('gatewayinstruction');
}

if(!empty(set_value('gatewayfields[]'))){
    $vPosted['gatewayfields'] = set_value('gatewayfields[]');
}

if(!empty(set_value('gatewayfieldssort'))){
    $vPosted['gatewayfieldssort'] = set_value('gatewayfieldssort');
}

$vDefault = array_merge($vSaved, $vPosted);
if(!isset($vDefault['gatewayfieldssort']) || empty($vDefault['gatewayfieldssort'])) {
    $vDefault['gatewayfieldssort'] = 'trxid,senderac,docupload,trxnote';
}
$vFieldsSort = !empty($vDefault['gatewayfieldssort']) ? explode(',',$vDefault['gatewayfieldssort']) : array();
?>

<div class="row clearfix">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <div class="card">
            <div class="header">
                <h2>
                    <?php echo !empty($vSaved) ? "Edit" : "Add" ?> Gateway
                </h2>
            </div>
            <div class="body">
                <?php echo form_open_multipart($vUrl, array("id"=>"gatewayAddForm") ); ?>
                <div class="row clearfix">
                    <div class="col-lg-2 col-md-2 col-sm-4 col-xs-12 form-control-label">
                        <label for="gatewayname">Name</label>
                    </div>
                    <div class="col-lg-10 col-md-10 col-sm-8 col-xs-12">
                        <div class="form-group">
                            <input type="text" value="<?php echo getValue('gatewayname', $vDefault) ?>" name="gatewayname" id="gatewayname" class="form-control" placeholder="Enter gateway name here" required>
                        </div>
                    </div>
                </div>
                <div class="row clearfix">
                    <div class="col-lg-2 col-md-2 col-sm-4 col-xs-12 form-control-label">
                        <label for="gatewayslug">Slug</label>
                    </div>
                    <div class="col-lg-10 col-md-10 col-sm-8 col-xs-12">
                        <div class="form-group">
                            <input type="text" value="<?php echo getValue('gatewayslug', $vDefault) ?>" name="gatewayslug" id="gatewayslug" class="form-control" placeholder="Enter gateway unique slug for url" required>
                        </div>
                    </div>
                </div>
                <div class="row clearfix">
                    <div class="col-lg-2 col-md-2 col-sm-4 col-xs-12 form-control-label">
                        <label for="gatewayrbkey">Gateway Key</label>
                    </div>
                    <div class="col-lg-10 col-md-10 col-sm-8 col-xs-12">
                        <div class="form-group">
                            <input type="text" value="<?php echo getValue('gatewayrbkey', $vDefault) ?>" name="gatewayrbkey" id="gatewayrbkey" class="form-control" placeholder="Enter Resell.Biz 32 bit secure key" required>
                        </div>
                    </div>
                </div>
                <div class="row clearfix">
                    <div class="col-lg-2 col-md-2 col-sm-4 col-xs-12 form-control-label">
                        <label for="gatewaycharge">Charge/Fee (Percentage)</label>
                    </div>
                    <div class="col-lg-10 col-md-10 col-sm-8 col-xs-12">
                        <div class="form-group">
                            <div class="input-group spinner m-b-0" data-trigger="spinner">
                                <input type="text" value="<?php echo getValue('gatewaycharge', $vDefault) ?>" name="gatewaycharge" id="gatewaycharge" class="form-control" value="1" data-rule="currency">
                                <span class="input-group-addon">
                                        <a href="javascript:;" class="spin-up" data-spin="up"><i class="glyphicon glyphicon-chevron-up"></i></a>
                                        <a href="javascript:;" class="spin-down" data-spin="down"><i class="glyphicon glyphicon-chevron-down"></i></a>
                                    </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row clearfix">
                    <div class="col-md-12 m-b-15"><div class="acl-form-header">INPUT FIELDS</div></div>
                </div>
                <div class="acl-sort-container">
                    <div class="acl-sort-path">
                        <?php foreach ($vFieldsSort as $field) : ?>
                        <div class="acl-sort-selector" data-name="<?php echo xss_clean($field) ?>">
                            <div class="row clearfix">
                                <div class="col-lg-2 col-md-2 col-sm-4 col-xs-12 form-control-label">
                                    <label for="gatewayfields_<?php echo $field ?>"><?php echo  getValue('type', getValue($field, $dynamicFields, true)) ?></label>
                                </div>
                                <div class="col-lg-7 col-md-6 col-sm-4 col-xs-12">
                                    <div class="form-group">
                                        <input type="text" value="<?php echo  getValue('label', getValue($field, getValue('gatewayfields', $vDefault, true), true)) ?>" name="gatewayfields[<?php echo $field ?>][label]" id="gatewayfields_<?php echo $field ?>" class="form-control" placeholder="<?php echo  getValue('placeholder', getValue($field, $dynamicFields, true)) ?>">
                                    </div>
                                </div>
                                <div class="col-lg-1 col-md-2 col-sm-2 col-xs-6">
                                    <div class="form-group">
                                        <input type="checkbox" id="gatewayfields_<?php echo $field ?>_enable" name="gatewayfields[<?php echo $field ?>][enable]" class="filled-in"<?php echo  selectMenu(getValue('enable', getValue($field, getValue('gatewayfields', $vDefault, true), true)), 'on', ' checked') ?>>
                                        <label for="gatewayfields_<?php echo $field ?>_enable">Enable</label>
                                    </div>
                                </div>
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-6">
                                    <div class="form-group">
                                        <input type="checkbox" id="gatewayfields_<?php echo $field ?>_required" name="gatewayfields[<?php echo $field ?>][required]" class="filled-in"<?php echo  selectMenu(getValue('required', getValue($field, getValue('gatewayfields', $vDefault, true), true)), 'on', ' checked') ?>>
                                        <label for="gatewayfields_<?php echo $field ?>_required">Required</label>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                    <input type="hidden" name="gatewayfieldssort" value="<?php echo getValue('gatewayfieldssort', $vDefault) ?>">
                </div>
                <div class="row clearfix">
                    <div class="col-md-12 m-b-15"><div class="acl-form-header">PAYMENT INSTRUCTION</div></div>
                </div>
                <div class="row clearfix">
                    <div class="col-xs-12">
                        <div class="form-group">
                            <textarea id="tinymce" name="gatewayinstruction" placeholder="Type payment procedures here.."><?php echo getValue('gatewayinstruction', $vDefault) ?></textarea>
                        </div>
                    </div>
                </div>
                <div class="row clearfix">
                    <button type="submit" class="btn btn-success m-l-15 waves-effect">Save Changes</button>
                    <a href="<?php echo base_url('/lbadmin/gateway') ?>" class="btn btn-default waves-effect">Cancel</a>
                </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    var gatewayError = <?php echo json_encode($vError) ?>;
</script>
