<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">
            <div class="col-sm-12">
                <aside>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                    <!--                                    <button style="margin:5px;" class="btn btn-primary" onclick="refreshCardMd5()" role="button">刷新卡片MD5</button>-->
                                    <span style="color: red;font-size: 20px;">请先导入卡片资源，再导入卡片配置，最后导入卡组配置；限时卡组ID从11110开始，限时卡组卡片ID从100000开始</span>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <aside>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                    <a style="margin:5px;" class="btn btn-primary" href="#card/group/0" role="button">常规卡组</a>
                                    <a style="margin:5px;" class="btn btn-primary " href="#card/group/1" role="button">限时卡组</a>
                                </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><?php echo $name ?>配置修改</header>
                    <section>
                        <div class="container-fluid">
                            <div style="margin-bottom: 10px;">
                                <button type="button" style="width: 80px;" class="btn btn-danger" disabled id="commitBtn">提交</button>

                                <span id="filespan" class="btn btn-success fileinput-button" style="margin-left: 50px;top: 0;">
                                        <label style="margin-bottom: 0" for="upfile">导入<?php echo $name ?></label>
                                        <input type="file" id="upfile">
                                    </span>
                                <input type="button" style="display: none;margin: 5px;top: 0;width: 80px;" class="btn btn-success" value="上传" id="btn"/>
                                <a type="button" href="/Export/cardgroup/<?php echo $type; ?>" style="margin: 5px;"
                                   class="btn btn-success">导出<?php echo $name ?></a>
                                <span id="filespan2" class="btn btn-success fileinput-button" style="margin-left: 50px;top: 0;">
                                        <label style="margin-bottom: 0" for="upfile2">导入<?php echo $name ?>卡片</label>
                                        <input type="file" id="upfile2">
                                    </span>
                                <input type="button" style="display: none;margin: 5px;top: 0;width: 80px;" class="btn btn-success" value="上传" id="btn2"/>
                                <a type="button" href="/Export/card/<?php echo $type; ?>" style="width: 150px;margin: 5px;"
                                   class="btn btn-success">导出<?php echo $name ?>卡片</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 title="<?= $val ?>"><?php echo $val; ?></th>
                                                    <?php }
                                                } ?>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php
                                            if (is_array($list) && !empty($list)) {
                                                foreach ($list as $key => $vl) { ?>
                                                    <tr>
                                                        <td alt="<? $vl['cgid'] ?>"><?= $vl['cgid'] ?></td>
                                                        <?php foreach ($_langList as $langk => $langv) { ?>
                                                            <td alt="<?= $vl[$langk] ?? $vl['cg_name'] ?>"><?= $vl[$langk] ?? $vl['cg_name'] ?></td>
                                                        <?php } ?>
                                                        <td alt="<? $vl['cg_coin_reward'] ?>"><?= $vl['cg_coin_reward'] ?></td>
                                                        <td alt="<? $vl['cg_spin_reward'] ?>"><?= $vl['cg_spin_reward'] ?></td>
                                                        <td alt="<? $vl['cg_lock_rank'] ?>"><?= $vl['cg_lock_rank'] ?></td>
                                                        <td alt="<? $vl['cg_petFeed_reward'] ?>"><?= $vl['cg_petFeed_reward'] ?></td>
                                                        <td alt="<? $vl['cg_petExp_reward'] ?>"><?= $vl['cg_petExp_reward'] ?></td>
                                                        <td alt="<? $vl['cg_title_img_id'] ?>"><?= $vl['cg_title_img_id'] ?></td>
                                                        <td><a type="button" class="btn btn-primary" href="#card/card/<?php echo $vl['cgid']; ?>">卡组</a></td>
                                                    </tr>
                                                <?php }
                                            } else { ?>
                                                <tr>
                                                    <td colspan="14">暂无数据</td>
                                                </tr>
                                            <?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>
    var type = <?php echo $type;?>;

    $("#btn").click(function () {
        var formData = new FormData();
        formData.append("file", document.getElementById("upfile").files[0]);
        $.ajax({
            type: "POST", // 数据提交类型
            url: "Import/cardgroup/" + type, // 发送地址
            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(data.msg);
                }
            }
        });
    });

    $("#btn2").click(function () {
        var formData = new FormData();
        formData.append("file", document.getElementById("upfile2").files[0]);
        $.ajax({
            type: "POST", // 数据提交类型
            url: "Import/card/" + type, // 发送地址
            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(data.msg);
                }
            }
        });
    });

    function refreshCardMd5() {
        if (confirm('确认刷新卡片配图Md5配置吗?')) {
            $.post('/card/refreshCardMd5', {
                id: 1
            }, function (data) {
                alert("刷新成功")
            });
        }
    }


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

    $("#filespan2").on("change", "input[type='file']", function () {
        var filePath = $(this).val();
        //如果仅上传图片 if(filePath.indexOf("jpg") != -1 || filePath.indexOf("png") != -1) {
        if (filePath) {
            $('#filespan2').hide();
            $('#btn2').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', 'td', 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);
    });


    tableColumnsMapping = '<?=json_encode($tableColumnMapping)?>';
    if(typeof  tableColumnsMapping == 'string')tableColumnsMapping = JSON.parse(tableColumnsMapping)
    //提交表格数据
    $('#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++) {
                if(!tableColumnsMapping[j]){
                    console.log(tableColumnsMapping, j)
                    continue;
                }
                switch (j) {
                    case 0:
                        tempData[tableColumnsMapping[j]] = $(td[j]).html();
                        break;
                    default:
                        arrayTemp[tableColumnsMapping[j]] = $(td[j]).html();
                        break;
                }
            }
            tempData['data'] = arrayTemp;
            arr.push(tempData)
        }

        $.ajaxSetup({
            async: true
        });


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


</script>

