<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>
<!--                            <span id="filespan" class="btn btn-success fileinput-button">-->
<!--                                <span>导入</span>-->
<!--                                <input type="file" id="upfile">-->
<!--                            </span>-->
<!--                            <input type="button" style="display: none;margin-bottom: 10px;width: 80px;" class="btn btn-success" value="上传" id="btn"/>-->
<!--                            <a type="button" href="/Export/card/--><?php //echo $cgid ?><!--" style="margin-bottom: 10px;width: 80px;" class="btn btn-success">导出</a>-->
                            <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 $vl['ciid'];?></td>

                                                        <?php foreach ($_langList as $langk => $langv){?>
                                                            <td alt="<?=$vl['ci_name_'.$langk]??$vl['ci_name']?>"><?=$vl['ci_name_'.$langk]??$vl['ci_name']?></td>
                                                        <?php }?>
                                                        <td><?php echo $vl['cgid']; ?></td>
                                                        <td><?php echo $vl['cgposition'];?></td>
                                                        <td><?php echo $vl['ci_star_rank']; ?></td>
                                                        <td><?php echo $vl['ci_lockRank']; ?></td>
                                                        <td><?php echo $vl['ci_picture']; ?></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>-->
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="top: 20%;width: 30%;">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">上传卡片</h4>
            </div>
            <div class="modal-body" id="uploadImg" style="text-align: center;">
                <img src="<?php echo '/images/default_img.png';?>" id="preview" class="upimg default-img" style="width: 55%;"  />
                <input type="file" name="imgfile" id="imgfile" class="imgfile hide"/>
                <span class="lang-name"></span>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="submit">提交</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>


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

<script>

    var obj;
    var levelId = 0;
    function ImgUpload(id){
        if(levelId!=id){
            document.getElementById('preview').setAttribute('src','/images/default_img.png');
            levelId = id;
            $("#imgfile").val("");
        }
        $('#myModal').modal('show');
    }

    $('#uploadImg').on('click','.upimg',function () {
        obj = $(this);
        obj.siblings(".imgfile").click();
    });

    //预览图片
    $('#uploadImg').on('change','.imgfile', function () {
        const file = document.getElementById('imgfile');
        const fileObj = file.files[0];
        const windowURL = window.URL || window.webkitURL;
        const img = document.getElementById('preview');
        if(file && fileObj) {
            const dataURl = windowURL.createObjectURL(fileObj);
            img.setAttribute('src',dataURl);
        }
    });

    $("#submit").click(function(){
        var formData = new FormData();
        var file = document.getElementById("imgfile").files[0];
        if(file == "" || file == undefined){
            $('#myModal').modal('hide');
            msgTip = $('#msg-tip');
            msgTip.messager().warn('请选择图片');
            return false;
        }
        formData.append("file",file);
        formData.append("ciid",levelId);
        $.ajax({
            type: "POST",
            url: "Upload/cardUpload",
            data: formData,
            async: true,
            processData: false, //
            contentType: false, //
            success: function(data) {
                data = JSON.parse(data);
                if(data.ret == 0){
                    $('#myModal').modal('hide');
                    msgTip = $('#msg-tip');
                    msgTip.messager().success('上传成功');
                    setTimeout(function () {
                        window.location.reload();
                    },2000)
                }else {
                    msgTip = $('#msg-tip');
                    msgTip.messager().warn('上传失败');
                }
            }
        });
    });

</script>

<script>
    $("#btn").click(function(){
        var formData = new FormData();
        formData.append("file",document.getElementById("upfile").files[0]);
        $.ajax({
            type: "POST", // 数据提交类型
            url: "Import/card/<?php echo $cgid ?>", // 发送地址
            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('上传失败');
                }
            }
        });
    });

    $("#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
        }
    });

    $('#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);
    });


    //新增一行
    $('#addBtn').click(function () {

        if($('#commitBtn').prop('disabled')){
            $('#commitBtn').attr('disabled', false);
        }
        var html = '<tr>';
        for(var i=0; i<7; i++){
            if(i==0){
                html += '<td class="add-row">自动生成</td>';
            }else if(i==2){
                html += '<td class="add-row"><?php echo $cgid ?></td>';
            }else{
                html += '<td class="input add-row"><input value=""/></td>';
            }

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


    //提交表格数据
    $('#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:
                        tempData['ciid'] = $(td[j]).html();
                        break;
                    case 1:
                        arrayTemp['ci_name'] = $(td[j]).html();
                        break;
                    case 2:
                        arrayTemp['ci_name_zh'] = $(td[j]).html();
                        break;
                    case 3:
                        arrayTemp['ci_name_tw'] = $(td[j]).html();
                        break;
                    case 4:
                        arrayTemp['ci_name_fr'] = $(td[j]).html();
                        break;
                    case 5:
                        arrayTemp['ci_name_de'] = $(td[j]).html();
                        break;
                    case 6:
                        arrayTemp['ci_name_es'] = $(td[j]).html();
                        break;
                    case 7:
                        arrayTemp['ci_name_pt'] = $(td[j]).html();
                        break;
                    case 8:
                        arrayTemp['ci_name_th'] = $(td[j]).html();
                        break;
                    case 9:
                        arrayTemp['ci_name_vn'] = $(td[j]).html();
                        break;
                    case 10:
                        arrayTemp['ci_name_no'] = $(td[j]).html();
                        break;
                    case 11:
                        arrayTemp['ci_name_id'] = $(td[j]).html();
                        break;
                    case 12:
                        arrayTemp['ci_name_rou'] = $(td[j]).html();
                        break;
                    case 13:
                        arrayTemp['ci_name_my'] = $(td[j]).html();
                        break;
                    case 14:
                        arrayTemp['ci_name_it'] = $(td[j]).html();
                        break;
                    case 15:
                        arrayTemp['cgid'] = $(td[j]).html();
                        break;
                    case 16:
                        arrayTemp['cgposition'] = $(td[j]).html();
                        break;
                    case 17:
                        arrayTemp['ci_star_rank'] = $(td[j]).html();
                        break;
                    case 18:
                        arrayTemp['ci_lockRank'] = $(td[j]).html();
                        break;
                    case 19:
                        arrayTemp['ci_picture'] = $(td[j]).html();
                        break;
                    case 20:
                        arrayTemp['grade'] = $(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/actionExclusiveCard',
            success: function(data){
                if(data.ret == 0){
                    window.location.reload()
                    $('#commitBtn').attr('disabled', true);
                }else{
                    msgTip.messager().error(data.msg);
                }
            }
        });
    });



</script>

