<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">
                                    <!--                                    <form class="form-inline" role="form" method="POST" action="#config/role">-->
                                    <!---->
                                    <!--                                        <button type="submit" class="btn btn-primary">查询</button>-->
                                    <!--                                    </form>-->
                                </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>
                                                <?php if(!empty($title)){
                                                    $width = [6,12,8,8];
                                                    foreach ($title as $key=>$val){ ?>
                                                        <th<?php if($key < 4) echo " style='width: {$width[$key]}%;'"?>><?php echo $val;?></th>
                                                    <?php }
                                                }?>
                                                <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['memo'];?></td>
                                                        <td><?php echo $vl['item'];?></td>
                                                        <td><?php echo $vl['title'];?></td>
                                                        <td><?php echo $vl['content'];?></td>
                                                        <?php
                                                            if($vl['title']=='crazyBetCfg'){
                                                                echo "<td>此项配置值需先复制出来修改,再粘贴,spins(自动触发体力条件),ts(自动触发周期,单位/分钟),rewardTime(奖励时长,单位分钟)</td>";
                                                            }else{
                                                                echo "<td></td>";
                                                            }
                                                        ?>
<!--                                                        <td><button type="button"  class="btn btn-danger delete-row" ><i class="fa fa-minus-square"></i>记录</button></td>-->
                                                    </tr>
                                                <?php } } else{?>
                                                <tr>
                                                    <td colspan="14">暂无数据</td>
                                                </tr>
                                            <?php } ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <button type="button"  class="btn btn-primary"  id="addBtn"><i class="fa fa-plus-square"></i>添加</button>

<!--                                    <nav>-->
<!--                                        <ul class="pagination">-->
<!--                                            <li class="disabled"><a href="#">共<span class="pagetotal">--><?php //echo $total;?><!--</span>条</a></li>-->
<!--                                            --><?php //echo $pageurl;?>
<!--                                        </ul>-->
<!--                                    </nav>-->
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
    </div>


</div>


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

<script>
    //新增一行
    $('#addBtn').click(function () {
        if($('#commitBtn').prop('disabled')){
            $('#commitBtn').attr('disabled', false);
        }
        var html = '<tr>';
        for(var i=0; i<5; i++){
            if(i==0){
                html += '<td class="add-row">自动生成</td>';
            }else if(i==2){
                html += '<td class="add-row">Game</td>';
            }else{
                html += '<td class="input add-row"><input value=""/></td>';
            }

        }
        html += '</tr>';
        $('#roleTable').append(html);
    });

    $('#roleTable').on('dblclick','td',function(){
        var _this = $(this),//保存td
            tdText = _this.text();
        var  cellIndex =  _this[0].cellIndex;
        if(cellIndex != 4) {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['memo'] = $(td[1]).html();
            tempData['item'] = $(td[2]).html();
            tempData['title'] = $(td[3]).html();
            tempData['content'] = $(td[4]).html();
            arr.push(tempData)
        }

        $.ajaxSetup({
            async: true
        });


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

