<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>
    .fixed-table-toolbar {
        display: none;
    }

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

    .fileinput-button input{
        position:absolute;
        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>
                    <header><i class="fa fa-fw fa-file"></i>语言键与语言关系</header>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                    <?php foreach ($langList as $lang=>$row){ ?>
                                        <span style="margin:5px;"><?php echo $row;?>(<?php echo $lang;?>)</span>
                                    <?php } ?>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>

                <aside>
                    <header><i class="fa fa-fw fa-file"></i>分享公共配置</header>
                    <section>
                        <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data" id="baseForm" action="#" style="margin-top: 20px">
                            <div class="container-fluid">
                              <button type="button" style="margin-bottom: 10px;width: 80px;" class="btn btn-danger" id="commitBtn1">提交</button><span style="color:red;">&nbsp;&nbsp;注:此处提交为单独提交分享公共奖励配置提交</span>
                            <div class="row">
                            <div class="col-sm-12">
                                <div class="table-responsive">
                                    <table id="roleTable" class="table table-bordered">
                                        <thead>
                                        <tr>
                                            <th>奖励次数</th>
                                            <th>间隔时间</th>
                                            <th>游戏内展示</th>
                                            <th>奖励体力</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <td><input type="text" class="form-control" name="base[maxTimes]" value="<?=$base['maxTimes']?>"></td>
                                        <td><input type="text" class="form-control" name="base[intervalTime]" value="<?=$base['intervalTime']?>"></td>

                                        <td><input class='mySwitch' type="checkbox" name="base[show]" value="<?=$base['show']?>" <?=($base['show'] == 1)?'checked':''?>></td>

                                        <td><input type="text" class="form-control" name="base[reward]" value="<?=$base['reward']?>"></td>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            </div>
                        </form>
                    </section>
                </aside>

                <aside>
                    <header><i class="fa fa-fw fa-file"></i>分享配置</header>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                    <a style="margin:5px;" class="btn btn-primary " href="#config/shareConfig" role="button">分享配置</a>
                                    <a style="margin:5px;" class="btn btn-primary " href="#config/shareConfig/img" role="button">分享图配置</a>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
                <aside>
                    <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data"  id="goodForm" action="#" style="margin-top: 20px">
                    <section>
                        <div class="container-fluid">
                            <button type="button" style="margin-bottom: 10px;width: 80px;" class="btn btn-danger" id="commitBtn">提交</button>
                            <div class="btn-group" style="margin-bottom:5px;">
                                <a type="button" class="btn btn-success" href="/Export/shareTranslate2" style="margin-left: 10px;border-radius: 5px;">导出翻译文件</a>
                                <input type="button" style="display: none;margin-bottom: 10px;width: 80px;margin-left: 10px;border-radius: 5px;" class="btn btn-info" value="上传" id="btn"/>
                                <span id="filespan" class="btn btn-info fileinput-button" style="margin-top: 5px;margin-left: 10px;border-radius: 5px;">
                                                <span id="filespan2">上传翻译文件</span>
                                                <input type="file" id="upfile">
                                             </span>
                                <span style="color:red;">&nbsp;&nbsp;注:此处提交为单独提交分享翻译配置提交</span>

                            </div>
                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="table-responsive">
                                        <table id="roleTable" class="table table-bordered">
                                            <thead>
                                            <tr>
                                                <th style="width: 10%;">分享点</th>
                                                <th style="width: 10%;">类型</th>
                                                <th style="width: 8%;">分享模式</th>
                                                <th>翻译</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                            <tbody id="shareList">
                                            <?php
                                            if(is_array($list)  && !empty($list)){
                                                foreach ($list as $key => $vl) { ?>
                                                    <tr>
                                                        <td><input type="text" class="form-control" title="<?php echo $vl['name'];?>" readonly name="conf[<?php echo $key;?>][name]" value="<?php echo $vl['name'];?>"></td>
                                                        <td><input type="text" class="form-control" title="<?php echo $vl['key'];?>" readonly name="conf[<?php echo $key;?>][key]" value="<?php echo $vl['key'];?>"></td>
                                                        <td>
                                                            <div class="col-sm-12">
                                                                <input type="radio" <?php if($vl['shareType'] == 0){ echo 'checked';}?> value="0" name="conf[<?php echo $key;?>][shareType]"/>fb
                                                            </div>
                                                            <div class="col-sm-12">
                                                                <input type="radio" disabled <?php if($vl['shareType'] == 1){ echo 'checked';}?> value="1" name="conf[<?php echo $key;?>][shareType]"/>messager
                                                            </div>
                                                        </td>
                                                        <td><textarea readonly="readonly" class="form-control show" name="conf[<?php echo $key;?>][content]" id="<?php echo $vl['key'];?>" lang_key="<?php echo $vl['key']; ?>" rows="5"><?php echo $vl['content'];?></textarea></td>
                                                        <td><button type="button" class="btn btn-danger delBtn" data-key="<?=$key?>" data-name="<?=$vl['name']?>">删除</button></td>
                                                    </tr>
                                                <?php } } else{?>
                                                <tr>
                                                    <td colspan="14">暂无数据</td>
                                                </tr>
                                            <?php } ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                                <button type="button" onclick="addShareItem()">添加</button>
                            </div>
                        </div>
                    </section>
                    </form>
                </aside>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="actionShow" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">内容</h4>
            </div>
            <form id="goodForm2" class="form-horizontal" role="form" method="post" action="" data-callBack="message">
                <div class="modal-body">
                    <div id="langDivShow" >
                        <div class="form-group">
                            <label class="col-sm-3 control-label">分享点</label>
                            <div class="col-sm-9">
                                <input readonly type="text" name="lang_key" class="form-control lang_key" />
                            </div>
                        </div>
                        <?php foreach ($langList as $k=>$v) { ?>
                            <div class="form-group">
                                <label class="col-sm-3 control-label"><?php echo $v;?></label>
                                <div class="col-sm-9">
                                    <input type="text" class="form-control <?php echo $k.'_title';?>" name="<?php echo $k;?>-title" value="" />
                                    <textarea  rows="5" cols="50" class="form-control <?php echo $k.'_data';?>" name="<?php echo $k;?>-desc"></textarea>
                                </div>
                            </div>
                        <?php }?>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" id="submit2">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    var defaultContent = <?php echo $vl['content'];?>;
    if(typeof defaultContent != 'string'){
        defaultContent = JSON.stringify(defaultContent);
    }
    var addCnt = 0
    function addShareItem() {
        addCnt++;
        var html = " <tr>\n" +
            " <td><input type=\"text\" class=\"form-control  \" title=\"\"  name=\"conf["+ addCnt +"][name]=\"\"  value='" + addCnt + "'></td>\n" +
            " <td><input type=\"text\" class=\"form-control addShareItem \" title=\"\"  name=\"conf["+ addCnt +"][key]=\"\" value='" + addCnt + "'></td>\n" +
            "\n" +
            "  <td>\n" +
            "     <div class=\"col-sm-12\">\n" +
            "            <input type=\"radio\" checked value=\"0\" name=\"conf["+ addCnt +"][shareType]\"/>fb\n" +
            "      </div>\n" +
            "      <div class=\"col-sm-12\">\n" +
            "            <input type=\"radio\"  value=\"1\" name=\"conf["+ addCnt +"][shareType]\"/>messager\n" +
            "      </div>\n" +
            "  </td>\n" +
            "  <td><textarea readonly=\"readonly\" onclick='showLangContent(this)' class=\"form-control show\" name=\"conf["+ addCnt +"][content]\" id=\""+addCnt+"\" lang_key=\""+addCnt+"\" rows=\"5\">"+defaultContent+"</textarea></td>\n" +
            "  <td><button type=\"button\" class=\"btn btn-danger \" onclick='delTr(this)'>删除</button></td>\n" +
            "</tr>";
        $('#shareList').append(html)
    }

    function showLangContent(obj) {
        var data = $(obj).val();
        var lang_key = $(obj).attr("lang_key");
        data = JSON.parse(data);
        for (key in data){
            $("#langDivShow ."+key+"_data").val(data[key].desc);
            $("#langDivShow ."+key+"_title").val(data[key].title);
        }
        $("#langDivShow .lang_key").val(lang_key);
        $('#actionShow').modal('show')
    }

    $('body').on("blur",'.addShareItem',function(){
        console.log(1)
    });
    $('body').on("click",'.delBtn',function(){
        var _this = $(this)
        var name = _this.data('name')
        var key  = _this.data('key')
        if(confirm("是否确定删除分享点："+ name )){
            showLoading()
            $.ajax({
                url: rootPath + 'config/delShareConfig/'+key,
                type: 'POST',
                data: {},
                dataType: 'json',
                contentType: false, //必须
                processData: false, //必须
                success: function (data) {
                    hideLoading()
                    alert(data.msg);
                    if(data.ret == 0){
                        window.location.reload();
                    }
                },
                error: function (data) {
                    alert(data.msg);
                }
            });
        }
    });
    function delTr(obj) {
        $(obj).parents('tr').remove();
    }
</script>
<script>
    var idData = JSON.parse('<?php echo json_encode($idData); ?>');

    $('.addOrDelControl').on("click",'.deleteParent',function(){
        $(this).parent().remove();
    });

    $('.addParent').on("click",function(){
        var gid = $(this).attr('gid');
        var k = $(this).attr('k');

        var str = "";
        str += '<div class="form-group"><label class="col-sm-2 control-label">道具*: </label><div class="col-sm-3"><select class="form-control" name="conf['+gid+'][reward]['+k+'][propsId]">';
        for(var x in idData) {
            str += '<option value="'+x+'">'+idData[x]['msg']+'</option>';
        }
        str += '</select></div><label class="col-sm-2 control-label">数量*: </label><div class="col-sm-2"><input type="text" class="form-control"  name="conf['+gid+'][reward]['+k+'][num]" value="1">'+
            '</div><button type="button" class="btn btn-danger deleteParent" >删除</button></div>';
        $(this).before(str);

        k++;
        $(this).attr("k",k);
    });

    
    //提交表格数据
    $('#commitBtn1').click(function () {
        var data = new FormData(document.getElementById("baseForm"));
        $.ajax({
            url: rootPath + 'config/actShareConfig',
            type: 'POST',
            data: data,
            dataType: 'json',
            contentType: false, //必须
            processData: false, //必须
            success: function (data) {
                alert(data.msg);
                window.location.reload();
            },
            error: function (msg) {
                alert(data.msg);
            }
        });
    });

    //提交表格数据
    $('#commitBtn').click(function () {
        var data = new FormData(document.getElementById("goodForm"));
        showLoading()
        $.ajax({
            url: rootPath + 'config/actShareConfig',
            type: 'POST',
            data: data,
            dataType: 'json',
            contentType: false, //必须
            processData: false, //必须
            success: function (data) {
                hideLoading()
                alert(data.msg);
                if(data.ret == 0){
                    window.location.reload();
                }
            },
            error: function (msg) {
                alert(data.msg);
            }
        });
    });

    $(".mySwitch").bootstrapSwitch({
        onSwitchChange:function(event, state){
            if(state){
                var state = 1;
            }else{
                var state = 0;
            }
            $(this).val(state);
        }
    });

    $('.show').click(function () {
        var data = $(this).val();
        var lang_key = $(this).attr("lang_key");
        data = JSON.parse(data);
        for (key in data){
            $("#langDivShow ."+key+"_data").val(data[key].desc);
            $("#langDivShow ."+key+"_title").val(data[key].title);
        }
        $("#langDivShow .lang_key").val(lang_key);
        $('#actionShow').modal('show')
    })


    $("#btn").click(function(){
        var formData = new FormData();
        formData.append("file",document.getElementById("upfile").files[0]);
        $.ajax({
            type: "POST", // 数据提交类型
            url: "Import/shareTranslate2", // 发送地址
            data: formData, //发送数据
            async: true, // 是否异步
            processData: false, //processData 默认为false，当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
            contentType: false, //
            xhr: function() {        //ajax进度条，直接拿过去就可以用
                var xhr = $.ajaxSettings.xhr();
                if (xhr.upload) {
                    xhr.upload.addEventListener("progress", function (e) {
                        var loaded = e.loaded; //已经上传大小情况
                        var tot = e.total; //附件总大小
                        var per = Math.floor(100 * loaded / tot); //已经上传的百分比
                        console.log(per)
                        $('#progress-bar').css('width',per+'%');
                    }, false);
                    return xhr;
                }
            },
            success: function(data) { // 成功回调
                data = JSON.parse(data);
                if(data.ret == 0){
                    alert(data.msg)
                    window.location.reload();
                }else {
                    alert(data.msg)
                }
            }
        });
    });

    $("#filespan").on("change", "input[type='file']", function() {
        var filePath = $(this).val();
        if (filePath) {
            $('#filespan2').html(filePath);
            $('#btn').val("导入");
            $('#btn').show();
        } else {
            $('#filespan2').html("上传资源文件");
            $('#btn').hide();
        }
    });

    $("#submit2").click(function(){
        var formData = new FormData(document.getElementById("goodForm2"));
        var objData = {};
        var langKey = "";
        for (var entry of formData.entries()){
            if(entry[0] == 'lang_key'){
                langKey = entry[1];
            }else{
                var key = entry[0].split("-")
                if(objData[key[0]] != null){
                    objData[key[0]][key[1]] = entry[1];
                }else{
                    objData[key[0]] = {}
                    objData[key[0]][key[1]] = entry[1];
                }
            }
        }
        $("#"+langKey).val(JSON.stringify(objData));
        $('#actionShow').modal('hide');
    });

</script>

