<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-classes="table"
                                               data-height="700">
                                            <thead>
                                            <tr>
                                                <?php if(!empty($title)){
                                                    foreach ($title as $val){ ?>
                                                        <th><?php echo $val;?></th>
                                                    <?php }
                                                }?>
                                                <?php
                                                    for($i=0;$i<$max-1;$i++){
                                                           echo "<th>".$title[count($title)-1]."</th>";
                                                    }
                                                ?>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php
                                            if(is_array($list)  && !empty($list)){
                                                foreach ($list as $key => $vl) { ?>
                                                    <tr>
                                                        <td><div><?php echo $vl['aid'];?></div></td>
                                                        <td><div><?php echo $vl['designation'];?></div></td>
                                                        <td><div><?php echo $vl['designation_en']; ?></div></td>
                                                        <td><div><?php echo $vl['designation_tw']; ?></div></td>
                                                        <td><div><?php echo $vl['designation_fr']; ?></div></td>
                                                        <td><div><?php echo $vl['designation_de']; ?></div></td>
                                                        <td><div><?php echo $vl['designation_es']; ?></div></td>
                                                        <td><div><?php echo $vl['designation_pt']; ?></div></td>
                                                        <td><div><?php echo $vl['designation_th']; ?></div></td>
                                                        <td><div><?php echo $vl['content'];?></div></td>
                                                        <td><div><?php echo $vl['content_en']; ?></div></td>
                                                        <td><div><?php echo $vl['content_tw']; ?></div></td>
                                                        <td><div><?php echo $vl['content_fr']; ?></div></td>
                                                        <td><div><?php echo $vl['content_de']; ?></div></td>
                                                        <td><div><?php echo $vl['content_es']; ?></div></td>
                                                        <td><div><?php echo $vl['content_pt']; ?></div></td>
                                                        <td><div><?php echo $vl['content_th']; ?></div></td>
                                                        <td>
                                                            <?php foreach ($vl['stage'] as $y){ ?>
                                                                <div><?php echo $y;?></div>
                                                            <?php } ?>
                                                        </td>
                                                        <?php foreach ($vl['reward'] as $y){ ?>
                                                            <td>
                                                                <div><?php echo $y->spin;?></div>
                                                                <div><?php echo $y->coin;?></div>
                                                            </td>
                                                        <?php } ?>
                                                        <!--                                                        <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="--><?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','div',function(){
        var _this = $(this),//保存td
            tdText = _this.text();
        var  cellIndex =  _this[0].cellIndex;

        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 = {};
            var arrayTemp =[];
            td = $(tr[i]).find('td');
            for (var j = 0; j < td.length; j++) {
                switch (j) {
                    case 0:
                        div = $(td[j]).find('div');
                        tempData['aid'] = $(div[0]).html();
                    break;
                    case 1:
                        div = $(td[j]).find('div');
                        tempData['designation'] = $(div[0]).html();
                        break;
                    case 2:
                        div = $(td[j]).find('div');
                        tempData['designation_en'] = $(div[0]).html();
                        break;
                    case 3:
                        div = $(td[j]).find('div');
                        tempData['designation_tw'] = $(div[0]).html();
                        break;
                    case 4:
                        div = $(td[j]).find('div');
                        tempData['designation_fr'] = $(div[0]).html();
                        break;
                    case 5:
                        div = $(td[j]).find('div');
                        tempData['designation_de'] = $(div[0]).html();
                        break;
                    case 6:
                        div = $(td[j]).find('div');
                        tempData['designation_es'] = $(div[0]).html();
                        break;
                    case 7:
                        div = $(td[j]).find('div');
                        tempData['designation_pt'] = $(div[0]).html();
                        break;
                    case 8:
                        div = $(td[j]).find('div');
                        tempData['designation_th'] = $(div[0]).html();
                        break;
                    case 9:
                        div = $(td[j]).find('div');
                        tempData['content'] = $(div[0]).html();
                        break;
                    case 10:
                        div = $(td[j]).find('div');
                        tempData['content_en'] = $(div[0]).html();
                        break;
                    case 11:
                        div = $(td[j]).find('div');
                        tempData['content_tw'] = $(div[0]).html();
                        break;
                     case 12:
                        div = $(td[j]).find('div');
                        tempData['content_fr'] = $(div[0]).html();
                        break;
                     case 13:
                        div = $(td[j]).find('div');
                        tempData['content_de'] = $(div[0]).html();
                        break;
                     case 14:
                        div = $(td[j]).find('div');
                        tempData['content_es'] = $(div[0]).html();
                        break;
                     case 15:
                        div = $(td[j]).find('div');
                        tempData['content_pt'] = $(div[0]).html();
                        break;
                     case 16:
                        div = $(td[j]).find('div');
                        tempData['content_th'] = $(div[0]).html();
                        break;
                    case 17:
                        div = $(td[j]).find('div');
                        var temp = [];
                        for (var x=0;x<div.length;x++){
                            temp.push($(div[x]).html());
                        }
                        tempData['stage'] =  temp.join(',');
                        break;
                    default:
                        var temp ={};
                        div = $(td[j]).find('div');
                        temp['spin'] = $(div[0]).html();
                        temp['coin'] = $(div[1]).html();
                        arrayTemp.push(temp);
                        break;
                }
            }
            tempData['reward'] = arrayTemp;
            arr.push(tempData)
        }

        $.ajaxSetup({
            async: true
        });


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



</script>

