<style>
    .table.gift-card td {
        padding: 0;
        vertical-align: middle;
    }

</style>
<div class="col-lg-10 col-md-10 col-sm-10 col-xs-10 bodyitem">
    <?php if ($this->session->userdata('gkpos_usertype') != 3): ?>
        <div class="row">
            <div class='col-lg-12 col-md-12 col-sm-12 col-xs-12 text-capitalize'>
                <input type="checkbox" class="checkbox-inline" id="showHideAddVoucher" onclick="checkBlock(this.id, 'AddNewVoucherBox')">
                <span style="color: #FFF"><?php echo $this->lang->line('gkpos_add_new_gift_card') ?></span>
            </div>
        </div>
        <div class="row no-padding" id="AddNewVoucherBox">
            <?php echo form_open('gkpos/settings/save_giftcard/', array('id' => 'giftcard_form', 'enctype' => 'multipart/form-data', 'class' => 'form-horizontal')); ?>
            <fieldset>
                <input type="hidden" id="giftcard_id" name="id">
                <div class="form-group form-group-sm">	
                    <?php echo form_label($this->lang->line('gkpos_voucher_title'), 'giftcard_title', array('class' => 'control-label col-lg-4 col-md-4 col-sm-4 col-xs-4 text-capitalize required')); ?>
                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                        <div class="input-group">
                            <span class="input-group-addon" style="background-color: #FF0000;"><a href="#"><i class="fa fa-table" aria-hidden="true"></i></a></span>
                            <input name="title" class="form-control required"  type="text" id="giftcard_title" value="<?php isset($title) ? print $title : print'' ?>">
                        </div>
                    </div>
                </div>
                <div class="form-group form-group-sm">	
                    <?php echo form_label($this->lang->line('gkpos_voucher_holder_name'), 'giftcard_holder', array('class' => 'control-label col-lg-4 col-md-4 col-sm-4 col-xs-4 text-capitalize')); ?>
                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                        <div class="input-group">
                            <span class="input-group-addon" style="background-color: #FF0000;"><a href="#"><i class="fa fa-table" aria-hidden="true"></i></a></span>
                            <input name="holder" class="form-control"  type="text" id="giftcard_holder" value="<?php isset($holder) ? print $holder : print'' ?>">
                        </div>
                    </div>
                </div>
                <div class="form-group form-group-sm">	
                    <?php echo form_label($this->lang->line('gkpos_voucher_holder_phone'), 'giftcard_phone', array('class' => 'control-label col-lg-4 col-md-4 col-sm-4 col-xs-4 text-capitalize')); ?>
                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                        <div class="input-group">
                            <span class="input-group-addon" style="background-color: #FF0000;"><a href="#"><i class="fa fa-table" aria-hidden="true"></i></a></span>
                            <input name="phone" class="form-control"  type="text" id="giftcard_phone" value="<?php isset($phone) ? print $phone : print'' ?>">
                        </div>
                    </div>
                </div>
                <div class="form-group form-group-sm">	
                    <?php echo form_label($this->lang->line('gkpos_voucher_code'), 'giftcard_code', array('class' => 'control-label col-lg-4 col-md-4 col-sm-4 col-xs-4 text-capitalize required')); ?>
                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                        <div class="input-group">
                            <span class="input-group-addon" style="background-color: #FF0000;"><a href="#"><i class="fa fa-table" aria-hidden="true"></i></a></span>
                            <input name="code" class="form-control required"  type="text" id="giftcard_code" value="<?php isset($giftcard_code) ? print $giftcard_code : print'' ?>">
                        </div>
                    </div>
                </div>
                <div class="form-group form-group-sm">	
                    <?php echo form_label($this->lang->line('gkpos_voucher_amount'), 'giftcard_amount', array('class' => 'control-label col-lg-4 col-md-4 col-sm-4 col-xs-4 text-capitalize required')); ?>
                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                        <div class="input-group">
                            <span class="input-group-addon" style="background-color: #FF0000;"><a href="#"><i class="fa fa-table" aria-hidden="true"></i></a></span>
                            <input name="amount" class="form-control required"  type="text" id="giftcard_amount" value="<?php isset($amount) ? print $amount : print'' ?>">
                        </div>
                    </div>
                </div>
                <div class="form-inline">	
                    <?php echo form_label($this->lang->line('gkpos_voucher_validity'), 'giftcard_amount', array('class' => 'control-label col-lg-4 col-md-4 col-sm-4 col-xs-4 text-capitalize required')); ?>
                    <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8">
                        <div class="input-group col-sm-1">FROM</div>
                        <div class="input-group form-inline col-sm-4" >
                            <span class="input-group-addon" style="background-color: #FF0000;"><a href="#"><i class="fa fa-table" aria-hidden="true"></i></a></span>
                            <input name="start" class="form-control required voucher_date" style="width: 100%"  type="text" id="giftcard_start" value="<?php isset($start) ? print $start : print'' ?>">
                        </div>
                        <div class="input-group col-sm-1">&nbsp;</div>
                        <div class="input-group col-sm-1">TO</div>
                        <div class="input-group col-sm-4">
                            <span class="input-group-addon" style="background-color: #FF0000;"><a href="#"><i class="fa fa-table" aria-hidden="true"></i></a></span>
                            <input  name="end" class="form-control required voucher_date" style="width: 110%"  type="text" id="giftcard_end" value="<?php isset($end) ? print $end : print'' ?>">
                        </div>
                    </div>
                </div>
                <div class="margin-bottom:20px">&nbsp;</div>
                <div class="form-group form-group-md">	
                    <div class="col-md-offset-4 col-md-8">
                        <ul id="giftcard_form_error_message_box" class="error_message_box"></ul>
                        <?php
                        echo form_submit(array(
                            'name' => 'submit_form',
                            'id' => 'submit_form',
                            'value' => $this->lang->line('gkpos_numpad_key_enter'),
                            'class' => 'btn btn-block btn-danger round btn-sm'));
                        ?>
                    </div>
                </div>
            </fieldset>
            <?php echo form_close(); ?>
        </div>
        <script type='text/javascript'>
            $(document).ready(function () {
                addjqueryValidatorFunction();
                $(".voucher_date").datepicker({
                    dateFormat: "dd/mm/yy"
                });
                $('#giftcard_form').validate({
                    submitHandler: function () {
                        var myform = document.getElementById('giftcard_form');
                        var formdata = new FormData(myform);
                        $.ajax({
                            url: $('#giftcard_form').attr('action'),
                            data: formdata,
                            cache: false,
                            processData: false,
                            contentType: false,
                            type: 'POST',
                            dataType: 'json',
                            success: function (response) {
                                if (response.success) {
                                    getSettingPage('<?php echo site_url('gkpos/settings/giftcard') ?>', 'Giftcard');
                                } else {
                                    alert(response.message);
                                }
                            }
                        });
                    },
                    errorClass: "has-error",
                    errorLabelContainer: "#giftcard_form_error_message_box",
                    wrapper: "li",
                    highlight: function (e) {
                        $(e).closest('.form-group').addClass('has-error');
                    },
                    unhighlight: function (e) {
                        $(e).closest('.form-group').removeClass('has-error');
                    },
                    rules: {
                        title: "required",
                        code: "required",
                        amount: "required",
                        start: "required",
                        end: "required",
                        phone: {phone: true}
                    },
                    messages: {
                        title: "<?php echo $this->lang->line('gkpos_voucher_title_required'); ?>",
                        code: "<?php echo $this->lang->line('gkpos_voucher_code_required'); ?>",
                        amount: "<?php echo $this->lang->line('gkpos_voucher_amount_required'); ?>",
                        start: "<?php echo $this->lang->line('gkpos_voucher_start_required'); ?>",
                        end: "<?php echo $this->lang->line('gkpos_voucher_end_required'); ?>"
                    }
                });
            });
        </script>
    <?php endif; ?>
    <div class="row voucher-list no-padding">
        <div class="modal-header">
            <div class="page-title col-md-12"><?php echo $this->lang->line('gkpos_system_gift_card') ?></div>
        </div>
        <div class="clearfix"></div>
        <table class="table table-responsive table-bordered fieldset gift-card">
            <tr>
                <th class="text-center text-capitalize" style="font-size: 11px;"><?php echo $this->lang->line('gkpos_title') ?></th> 
                <th class="text-center text-capitalize" style="font-size: 11px;"><?php echo $this->lang->line('gkpos_voucher_holder_name') ?></th> 
                <th class="text-center text-capitalize" style="font-size: 11px;"><?php echo $this->lang->line('gkpos_code') ?></th> 
                <th class="text-center text-capitalize" style="font-size: 11px;"><?php echo $this->lang->line('gkpos_amount') ?></th> 
                <th class="text-center text-capitalize" style="font-size: 11px;"><?php echo $this->lang->line('gkpos_voucher_balance') ?></th> 
                <th class="text-center text-capitalize" style="font-size: 11px;"><?php echo $this->lang->line('gkpos_voucher_validity') ?></th> 
                <th class="text-center text-capitalize" style="font-size: 11px;"><?php echo $this->lang->line('gkpos_status') ?></th> 
                <?php if ($this->session->userdata('gkpos_usertype') != 3): ?>
                    <th class="text-center text-capitalize" style="font-size: 11px;"><?php echo $this->lang->line('gkpos_action') ?></th> 
                <?php endif; ?>
            </tr>
            <?php if (!empty($voucher_list)): ?>
                <?php $voucher_list2 = array_reverse($voucher_list) ?>
                <?php foreach ($voucher_list2 as $voucher): ?> 
                    <tr <?php $voucher->status == 3 ? print "class='alert-warning'" : '' ?>>
                        <td class="text-center"><?php echo $voucher->title ?></td> 
                        <td class="text-center"><?php echo $voucher->holder ?></td> 
                        <td class="text-center"><?php echo $voucher->code ?></td> 
                        <td class="text-center"><?php echo to_currency($voucher->amount) ?></td> 
                        <td class="text-center"><?php echo to_currency($voucher->balance) ?></td> 
                        <td class="text-center"><?php echo date($this->config->item('dateformat'), strtotime($voucher->start)) . '&nbsp;TO&nbsp;' . date($this->config->item('dateformat'), strtotime($voucher->end)) ?></td> 
                        <?php if ($voucher->status == 3): ?>
                            <td>Dissolved</td>
                        <?php else: ?>
                            <td class="text-center"><?php $voucher->status == 1 ? print "Active" : print "Inactive" ?></td> 
                        <?php endif; ?>
                        <?php if ($this->session->userdata('gkpos_usertype') != 3): ?>
                            <td class="text-center">
                                <?php if ($voucher->status == 3): ?>
                                    <a href='javascript:void(0)' onclick="updateGiftcard('<?php echo $voucher->id ?>', '<?php echo site_url('gkpos/settings/update_giftcard') ?>', 'delete')" class="btn btn-danger btn-sm">Delete</a> 
                                <?php else: ?>
                                    <a href='javascript:void(0)' class="btn btn-success btn-sm gradient" onclick="updateGiftcard('<?php echo $voucher->id ?>', '<?php echo site_url('gkpos/settings/update_giftcard') ?>', '<?php $voucher->status == 1 ? print'deactivate' : print 'activate' ?>')"><?php $voucher->status == 1 ? print'Deactivate' : print 'Activate' ?> </a>
                                <?php endif; ?>
                                <a href='javascript:void(0)' onclick="printGiftCard('printVoucher_<?php echo $voucher->id ?>')" class="btn btn-info btn-sm">Print</a>      
                            </td> 
                        <?php endif; ?>
                    </tr>
                    <tr style="display: none;">
                        <td colspan="8">
                            <table id="printVoucher_<?php echo $voucher->id ?>" style="margin-top:-10mm;">
                                <tr>
                                    <td colspan="2" style="width: 75mm; text-align: center;text-transform: uppercase; border-bottom: 2px dotted #000;"><strong style="font-size: 20px;">Giftcard Voucher</strong></td>
                                </tr>
                                <tr>
                                    <td style="width: 50mm; text-align: left; font-size: 20px;">Name</td>
                                    <td style="width: 25mm;  text-align: right;font-size: 20px;"><?php echo $voucher->holder ?></td>
                                </tr>
                                <tr>
                                    <td style="width: 50mm;  text-align: left;font-size: 20px;">Code</td>
                                    <td style="width: 25mm;  text-align: right;font-size: 20px;"><?php echo $voucher->code ?></td>
                                </tr>
                                <tr>
                                    <td  style="width: 50mm; text-align: left;font-size: 20px;">Amount</td>
                                    <td  style="width: 25mm;  text-align: right;font-size: 20px;"><?php echo to_currency($voucher->amount) ?></td>
                                </tr>
                                <tr>
                                    <td style="width: 50mm; text-align: left;font-size: 20px;">Validity</td>
                                    <td style="width: 25mm; text-align: right;font-size: 20px;"><?php echo date($this->config->item('dateformat'), strtotime($voucher->start)) . '&nbsp;TO&nbsp;' . date($this->config->item('dateformat'), strtotime($voucher->end)) ?></td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                <?php endforeach; ?>
            <?php else: ?>
                <tr>
                    <td colspan="4">No Gift card Found</td> 
                </tr>
            <?php endif; ?>
        </table>                        
    </div>
</div>
<script>
    function updateGiftcard(voucher_id, url, action) {
        $.ajax({
            url: url,
            data: {
                id: voucher_id,
                action: action
            },
            dataType: 'json',
            type: "POST",
            success: function (response) {
                if (response.success) {
                    ALERT_TITLE = "Success!";
                    alert(response.message);
                    getSettingPage('<?php echo site_url("gkpos/settings/giftcard") ?>', 'Giftcard');
                } else {
                    ALERT_TITLE = "Error!";
                    alert(response.message);
                }

            },
        });
    }
    $(document).ready(function () {
        if ($('#showHideAddVoucher').is(":checked")) {
            $('#AddNewVoucherBox').show();
        } else {
            $('#AddNewVoucherBox').hide();
        }
    });
    function printGiftCard(div) {
        var header = '<div>';
<?php if ($this->config->item('gk_name_show') != null) { ?>
            header = "<p style='text-align:center; font-weight:bold; text-transform:uppercase; text-decoration:underline;font-size:20px;'><?php echo $this->config->item('gk_name') ?></p>"
<?php } ?>
        header += '<p style="text-align: center"><img src="<?php echo UPLOAD_PATH ?>gkpos/logo/<?php echo $this->config->item('gk_logo') ?>" class="center-block"></p>';
        header += '</div>';
        $("#" + div).printThis({
            header: header
        });
    }
</script>