<?php $__env->startSection('header'); ?>
<script>

function getChangeIndex() {
    var strArr = $("#change-index").val().split(",");
    var arr = strArr.map(function(x) {
        if(x != "") {
            return parseInt(x, 10);
        }
    });
    arr = arr.filter(Number);

    return arr;
}

$(document).ready(function(){
    $('.percent').val(function(index, old) { return old.replace(/[^0-9|\.]/g, '') + '%'; });
    $('.percent').change(function(e) {
        $(this).val(function(index, old) { return old.replace(/[^0-9|\.]/g, '') + '%'; });
    });
    $('.bonus-detail input').keyup(function(e) {
        
        var arr = getChangeIndex();

        var id = $(this).parents("tr").data("id");
        var idx = arr.indexOf(id);

        if(idx == -1) {
            arr.push(id);
            var str = arr.join();
            $("#change-index").val(str);
            $(".save").removeClass("btn-primary").addClass("btn-success");
        }
    });

    $("#user_search").keyup(function(){
        
        // When value of the input is not blank
        if( $(this).val() != ""){
        // Show only matching TR, hide rest of them
            $(".bonus-detail tbody>tr").hide();
            $(".bonus-detail .td-code").each(function(){
                if($(this).html().indexOf($("#user_search").val()) != -1) {
                    $(this).parent("tr").show();
                }
            })
        }
        else{
            // When there is no input or clean again, show everything back
            $(".bonus-detail tbody>tr").show();
        }
    });
    $(".save").click(function(){
        var obj = [];

        var strArr = $("#change-index").val().split(",");
        console.log(strArr);
        strArr.forEach(function(id){
            var tr = $(".bonus-detail tr[data-id=" + id + "]");
            var entry = {};

            entry['id'] = id;
            entry['code'] = tr.find(".td-code").html();
            entry['category'] = tr.find(".name").val();
            entry['retail-value'] = tr.find(".retail-value").val();
            entry['introduction-value'] = tr.find(".introduction-value").val();
            entry['bonus-value'] = tr.find(".bonus-value").val();
            entry['value'] = tr.find(".bonus-value").val();
            obj.push(entry);
            console.log(entry);
        });
        

        $.get("<?php echo URL::to('admin/bonus/mupdate'); ?>", {"obj": JSON.stringify(obj)}, function(data){
            //console.log(data);
            if(data["success"] == 1) {
                alert(data["message"]);
            }
            $(".save").addClass("btn-primary").removeClass("btn-success");
            var arr = getChangeIndex();
            for(var i = 0; i < arr.length; i++) {
                $(".bonus-detail tr[data-id=" + arr[i] + "] .updated_at").html(data["time"]["date"]);
            }
            
            $("#change-index").val("");
        });
    });
});
    </script>
    <style>
        #header {
            background:#8d1c22;
        }

        .form-inline {
            margin:10px;
        }

        #bonus-form {
            text-align:center;
        }
        
        #bonus-form td {
            padding:10px;
        }
    </style>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('content'); ?>
<div class="row-fluid">
    <div class="span9">
    </div>
    <div>
        <h4 class="nametag">使用者管理系統</h4>
        <hr>
        <div class="button-menu row">
            <div class="col-md-8 col-xs-8">
                <?php if($errors->has()): ?>
                    <a class="btn btn-warning" data-toggle="modal" data-target=".bs-example-modal-sm">新增紅利類別</a>
                    <span class="label label-danger">新增失敗</span>
                <?php else: ?>
                    <a class="btn btn-primary" data-toggle="modal" data-target=".bs-example-modal-sm">新增紅利類別</a>
                <?php endif; ?>
                <a class="btn btn-primary save">儲存修改</a>
            </div>
            <div class="col-md-4 col-xs-4">
                <div class="input-group">
                    <input type="text" id="user_search" value="" class="form-control" placeholder="紅利編碼"/>
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="button" name="search" onclick="searchByIE()">Seach</button>
                    </span>
                </div>
            </div>
        </div>
        <hr>
        <input type="hidden" class="hidden" id="change-index"/>
        <table class="table table-bordered bonus-detail">
            <thead>
                <tr>
                    <th>紅利編碼</th>
                    <th>紅利名稱</th>
                    <th>門市點數</th>
                    <th>介紹點數</th>
                    <th>分紅點數</th>
                    <th>變更日期</th>
                </tr>
            </thead>
            <?php foreach($bonus_detail as $bonusData): ?>
                <tr data-id="<?php echo $bonusData->id; ?>">
                    <td class="td-code"><?php echo $bonusData->code; ?></td>
                    <td><input class="name" value="<?php echo $bonusData->category; ?>"></td>
                    <td><input class="percent retail-value" value="<?php echo $bonusData->retail_value; ?>"></td>
                    <td><input class="percent introduction-value" value="<?php echo $bonusData->introduction_value; ?>"></td>
                    <td><input class="percent bonus-value" value="<?php echo $bonusData->bonus_value; ?>"></td>
                    <td class="updated_at"><?php echo $bonusData->updated_at; ?></td>
                </tr>
            <?php endforeach; ?>
        </table>
        <?php if(Session::has('message')): ?>
            <div class="alert alert-warning" role="alert"><?php echo Session::get('message'); ?></div>
        <?php endif; ?>
    </div>
</div>

<?php $__env->stopSection(); ?>

<?php $__env->startSection('dialog'); ?>
    <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
          <h4 class="modal-title" id="mySmallModalLabel">新增紅利類別</h4>
    </div>
    <?php echo Form::open(array('url' => 'admin/bonus', 'id' => 'bonus-form')); ?>

    <table>
        <tr>
            <td>紅利編碼</td>
            <td><?php echo Form::text('code','', array('class' => 'form-control')); ?></td>
        </tr>
        <tr>
            <td>紅利名稱</td>
            <td><?php echo Form::text('name','', array('class' => 'form-control')); ?></td>
        </tr>
        <tr>
            <td>門市點數</td>
            <td><?php echo Form::text('retail-value','', array('class' => 'form-control')); ?></td>
        </tr>
        <tr>
            <td>介紹點數</td>
            <td><?php echo Form::text('introduction-value','', array('class' => 'form-control')); ?></td>
        </tr>
        <tr>
            <td>分紅點數</td>
            <td><?php echo Form::text('bonus-value','', array('class' => 'form-control')); ?></td>
        </tr>
        <tr>
            <td colspan=2>
                <?php if($errors->has()): ?>
                    <div class="alert alert-warning" role="alert"><?php echo $errors->first(); ?></div>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td></td>
            <td><?php echo Form::submit('送出', array('class' => 'btn btn-primary')); ?></td>
        </tr>
    </table>
    <?php echo Form::close(); ?>


<?php $__env->stopSection(); ?>

<?php echo $__env->make('Myadmin::layout.basic', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>