<link rel="stylesheet" href="<?php echo base_url();?>public/css/vendor/bootstrap/bootstrap-table.css">
<link rel="stylesheet" href="<?php echo base_url();?>public/css/vendor/bootstrap/table-fix-column.css">

<script src="<?php echo base_url();?>public/js/vendor/bootstrap/src/bootstrap-table.js"></script>
<script src="<?php echo base_url();?>public/js/vendor/bootstrap/src/table-fix-column.js"></script>

<style type="text/css">
    table {
        table-layout:fixed !important;
    }

    th{
        background: rgb(146, 208, 80);
    }

    td,th{
        max-width: 400px;
        word-wrap: break-word;
    }

    .table-hover > tbody > tr:hover {
        background-color: #c8bd6d;
    }

    input {
        width:60px;
    }

    #roleTable tr:nth-child(5n){
        background:rgb(198, 239, 206);
    } /**隔五行*/

    .fixed-table-toolbar {
        display: none;
    }
</style>
<div class="content_wrapper">
    <div id="msg-tip"></div>
    <div class="container-fluid ">
        <div class="row hide">
            <div class="col-sm-12">
                <aside>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <aside>
                    <header><i class="fa fa-fw fa-file"></i>触发条件</header>
                    <section>
                        <div class="container-fluid">
                            <button type="button" style="margin-bottom: 10px;width: 80px;" class="btn btn-danger" disabled id="commitBtn">提交</button>
                            <div class="row">

                                <div class="col-sm-12">
                                    <div class="table-responsive">
                                        <table id="roleTable" class="table table-bordered table-hover"
                                               data-toggle="table"
                                               data-classes="table table-hover"
                                               data-show-columns="true"
                                               data-striped="true"
                                               data-height="700">
                                            <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>名称</th>
                                                <th>X</th>
                                                <th>Y</th>
                                                <th>限定次数</th>
                                                <th>概率</th>
                                                <th>间隔</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php
                                            if(is_array($list)  && !empty($list)){
                                                foreach ($list as $key => $vl) { ?>
                                                    <tr>
                                                        <td><?php echo $vl['id'];?></td>
                                                        <td><?php echo $vl['name'];?></td>
                                                        <td><?php echo $vl['x'];?></td>
                                                        <td><?php echo $vl['y'];?></td>
                                                        <td><?php echo $vl['max'];?></td>
                                                        <td><?php echo $vl['rand'];?></td>
                                                        <td><?php echo $vl['interval'];?></td>
                                                    </tr>
                                                <?php } ?>
                                            <?php } ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
    </div>


</div>


<script src="<?php echo base_url();?>public/js/common.js"></script>
<!--<script src="--><?php //echo base_url();?><!--public/js/imageChange.js"></script>-->

<script>
    $('#roleTable').on('paste', 'input', function (e) {
        var _that =  $(this);
        var strArr = [];
        var pastedText = undefined;
        if (window.clipboardData && window.clipboardData.getData) { // IE
            pastedText = window.clipboardData.getData('Text');
        } else {
            pastedText = e.originalEvent.clipboardData.getData('Text');//e.clipboardData.getData('text/plain');
        }
        if(pastedText == undefined || pastedText == null || pastedText == ''){
            return;
        }
        //处理粘贴的数据,已空格拆分
        pastedText = pastedText.replace(/,/g, '');
        strArr = pastedText.split(/\s+/);

        setTimeout(function () {
            _that.val(strArr[0]);
        }, 200);

        for(var i=1; i < strArr.length; i++){
            _that.parent().siblings().eq(i-1).removeClass('input').html(strArr[i] || '');
        }
    });
    $('#roleTable').on('dblclick','td',function(){
        var _this = $(this),//保存td
            tdText = _this.text();
        var  cellIndex =  _this[0].cellIndex;
        if(cellIndex == 0) {return false;} //点击操作列不可编辑


        if(!_this.is('.input')){
            _this.addClass('input').html('<input type="text" value="'+ tdText +'" />').find('input').focus().blur(function(){
                var _input = $(this); //保存input对象
                var thisvalue = $(this).val();
                _input.parent().removeClass('input').html(thisvalue || '');
                if($('#commitBtn').prop('disabled')){
                    $('#commitBtn').attr('disabled', false);
                }
            });
        }

    });


    $('#roleTable').on('blur', '.add-row', function () {
        var _that = $(this);
        var thisval = _that.find('input').val();
        console.log(thisval);
        _that.removeClass('input').html(thisval || 0);
    });


    //提交表格数据
    $('#commitBtn').click(function () {
        var tableContent = $('#roleTable').html(),msgTip = $('#msg-tip');
        if($('#roleTable td').hasClass('input')){
            msgTip.messager().warn('列表中还有未编辑的');
            return;
        }

        var tr = $('#roleTable tr');
        var arr = [];
        for (var i = 1; i < tr.length; i++) {
            var tempData = {};
            td = $(tr[i]).find('td');
            tempData['id']   = $(td[0]).html();
            tempData['name'] = $(td[1]).html();
            tempData['x']    = $(td[2]).html();
            tempData['y']    = $(td[3]).html();
            tempData['max']  = $(td[4]).html();
            tempData['rand'] = $(td[5]).html();
            tempData['interval'] = $(td[6]).html();
            arr.push(tempData)
        }

        $.ajaxSetup({
            async: true
        });


        $.ajax({
            type: 'POST',
            dataType: 'json',
            data:{
                data: JSON.stringify(arr),
            },
            url:'shop/actionTrigger',
            success: function(data){
                if(data.ret == 0){
                    window.location.reload()
                    $('#commitBtn').attr('disabled', true);
                }else{
                    msgTip.messager().error(data.msg);
                }
            }
        });
    });



</script>

