<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;
    }

    .fileinput-button {
        position: relative;
        top: -5px;
    }

    .fileinput-button input{
        position:absolute;
        right: 0px;
        top: 0px;
        opacity: 0;
        -ms-filter: 'alpha(opacity=0)';
    }
</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" style="padding-left: 15px;">
                                <span id="filespanpro" class="btn btn-success fileinput-button">
                                    <span>导入概率</span>
                                    <input type="file" id="upfilepro">
                                </span>
                                <input type="button" style="display: none;margin-bottom: 10px;width: 80px;" class="btn btn-success" value="上传" id="btnpro"/>
                                <a type="button" href="/Export/slotWeight" style="margin-bottom: 10px;width: 80px;" class="btn btn-success">导出概率</a>
                                <span id="filespancoin" class="btn btn-success fileinput-button">
                                    <span>导入金币</span>
                                    <input type="file" id="upfilecoin">
                                </span>
                                <input type="button" style="display: none;margin-bottom: 10px;width: 80px;" class="btn btn-success" value="上传" id="btncoin"/>
                                <a type="button" href="/Export/slotcoin2" style="margin-bottom: 10px;width: 80px;" class="btn btn-success">导出金币</a>
                            </div>
                            <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)){
                                                    foreach ($title as $val){ ?>
                                                        <th><?php echo $val;?></th>
                                                    <?php }

                                                }?>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php
                                            if(is_array($list)  && !empty($list)){
                                                foreach ($list as $key => $vl) { ?>
                                                    <tr>
                                                        <td><?php echo '<div>'.$key.'</div>';?></td>
                                                        <?php foreach ($vl as $k=>$v){ ?>
                                                            <td><?php if($k<4){ echo '<div>'.$v['v'].'</div>';}else if($k>=10){ echo '<div>'.$v['v'].'</div>';}else{ echo '<div>'.$v['v'].'</div><div>'.$v['prize'].'</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="24">暂无数据</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>
    $("#btnpro").click(function(){
        var formData = new FormData();
        formData.append("file",document.getElementById("upfilepro").files[0]);
        $.ajax({
            type: "POST", // 数据提交类型
            url: "Import/slotWeight", // 发送地址
            data: formData, //发送数据
            async: true, // 是否异步
            processData: false, //processData 默认为false，当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
            contentType: false, //
            success: function(data) { // 成功回调
                data = JSON.parse(data);
                if(data.ret == 0){
                    window.location.reload();
                }else {
                    msgTip = $('#msg-tip');
                    msgTip.messager().warn('上传失败');
                }
            }
        });
    });

    $("#filespanpro").on("change", "input[type='file']", function() {
        var filePath = $(this).val();
        //如果仅上传图片 if(filePath.indexOf("jpg") != -1 || filePath.indexOf("png") != -1) {
        if (filePath) {
            $('#filespanpro').hide();
            $('#btnpro').show();
        } else {
            return false
        }
    });

    $("#btncoin").click(function(){
        var formData = new FormData();
        formData.append("file",document.getElementById("upfilecoin").files[0]);
        $.ajax({
            type: "POST", // 数据提交类型
            url: "Import/slotcoin2", // 发送地址
            data: formData, //发送数据
            async: true, // 是否异步
            processData: false, //processData 默认为false，当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
            contentType: false, //
            success: function(data) { // 成功回调
                data = JSON.parse(data);
                if(data.ret == 0){
                    window.location.reload();
                }else {
                    msgTip = $('#msg-tip');
                    msgTip.messager().warn('上传失败');
                }
            }
        });
    });

    $("#filespancoin").on("change", "input[type='file']", function() {
        var filePath = $(this).val();
        //如果仅上传图片 if(filePath.indexOf("jpg") != -1 || filePath.indexOf("png") != -1) {
        if (filePath) {
            $('#filespancoin').hide();
            $('#btncoin').show();
        } else {
            return false
        }
    });



    $('#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 editUserList = '<?php echo json_encode($editUserList) ?>';
        var username = '<?php echo get_cookie('username');?>';
        var msgTip = $('#msg-tip');

        editUserList = JSON.parse(editUserList);
        if($.inArray(username, editUserList) < 0){
            //msgTip.messager().warn('没有编辑权限');
            //return;
        }

        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);
                }
            });
        }

    });


    //新增一行
    $('#addBtn').click(function () {
        var editUserList = '<?php echo json_encode($editUserList) ?>';
        var username = '<?php echo get_cookie('username');?>';
        var msgTip = $('#msg-tip');

        editUserList = JSON.parse(editUserList);
        if($.inArray(username, editUserList) < 0){
            //msgTip.messager().warn('没有编辑权限');
            //return;
        }

        if($('#commitBtn').prop('disabled')){
            $('#commitBtn').attr('disabled', false);
        }
        var html = '<tr>';
        for(var i=0; i<22; i++){
            if(i<5){
                html += '<td class="input add-row"><div><input value=""/></div></td>';
            }else if(i<11){
                html += '<td class="input add-row"><div><input value=""/></div><div><input value=""/></div></td>';
            }else{
                html += '<td class="input add-row"><div><input value=""/></div></td>';
            }
        }

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

    //删除一行
    $('#roleTable').on('click','.delete-row',function () {
        if(confirm('是否确认删除该行')){
            if($('#commitBtn').prop('disabled')){
                $('#commitBtn').attr('disabled', false);
            }
            $(this).parent().parent().remove();
        }

    });


    $('#roleTable').on('blur', '.add-row', function () {
        var _that = $(this);
        var thisval = _that.find('input');
        var div = $(thisval[0]).parent();
        if($(thisval[0]).val() == ""){
            div.html(0);
        }else{
            div.html($(thisval[0]).val());
        }

        div.removeClass('input');
        div.parent().removeClass('input');
    });


    //提交表格数据
    $('#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 vNum = 0;
            var tempData = {};
            var arrayTemp =[];
            td = $(tr[i]).find('td');
            for (var j = 0; j < td.length; j++) {
                var temp = {};
                div = $(td[j]).find('div');
                temp['id'] = j;
                if(j==0){
                    tempData['id'] = $(div[0]).html();
                }else{
                    switch (j) {
                        case 1:
                            temp['type'] = 4;
                            temp['prize'] = '攻击';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 2:
                            temp['type'] = 3;
                            temp['prize'] = '偷袭';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 3:
                            temp['type'] = 2;
                            temp['prize'] = '体力';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 4:
                            temp['type'] = 5;
                            temp['prize'] = '护盾';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 11:
                            temp['type'] = 0;
                            temp['prize'] = '没有奖励';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 12:
                            temp['type'] = 6;
                            temp['prize'] = '活动物品1';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 13:
                            temp['type'] = 6;
                            temp['prize'] = '活动物品2';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 14:
                            temp['type'] = 6;
                            temp['prize'] = '活动物品3';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 15:
                            temp['type'] = 7;
                            temp['prize'] = '小玩法物品1';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 16:
                            temp['type'] = 7;
                            temp['prize'] = '小玩法物品2';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 17:
                            temp['type'] = 7;
                            temp['prize'] = '小玩法物品3';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 18:
                            temp['type'] = 8;
                            temp['prize'] = '一个偷';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 19:
                            temp['type'] = 8;
                            temp['prize'] = '两个偷';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 20:
                            temp['type'] = 9;
                            temp['prize'] = '一个打';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 21:
                            temp['type'] = 9;
                            temp['prize'] = '两个打';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 22:
                            temp['type'] = 10;
                            temp['prize'] = '1个体力瓶';
                            temp['v'] = $(div[0]).html();
                            break;
                        case 23:
                            temp['type'] = 10;
                            temp['prize'] = '2个体力瓶';
                            temp['v'] = $(div[0]).html();
                            break;
                        default:
                            temp['type'] = 1;
                            temp['prize'] = $(div[1]).html();
                            temp['rewardId'] = j-4;
                            temp['v'] = $(div[0]).html();
                    }
                    vNum += parseFloat(temp['v']);
                    arrayTemp[j-1] = temp;
                }
            }
            // if(vNum > 100 || vNum != 100){
            //     msgTip.messager().warn('第'+i+'行概率不等于100');
            //     return;
            // }
            tempData['data'] = arrayTemp;
            arr.push(tempData)
        }


        $.ajaxSetup({
            async: true
        });


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



</script>

