<div class="content_wrapper">

    <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">
                                    <div class="btn-group" style="margin-bottom:20px;">
                                        <button style="margin-right: 5px;" type="button" class="btn btn-success" data-toggle="modal" data-target="#actionCode">新增版本</button>
                                    </div>
                                    <div class="table-responsive">
                                        <table id="example" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                                            <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>设备</th>
                                                <th>版本</th>
                                                <th>是否强制更新</th>
                                                <th>创建时间</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php foreach($list as $k => $v) { ?>
                                            <tr>
                                                <td><?php echo $v['id'];?></td>
                                                <td><?php echo $v['gu_device'];?></td>
                                                <td><?php echo $v['gu_version'];?></td>
                                                <td><?php echo $v['gu_force'] == 1 ? '强制更新' : '不强制更新';?></td>
                                                <td><?php echo date('Y-m-d H:i:s', $v['gu_addtime']);?></td>
                                                <td>
                                                    <a href="<?php echo base_url('/#gameupdate/edit/'.$v['id']);?>" class="btn btn-primary active" role="button">编辑</a>
                                                    <a href="<?php echo base_url('/#gameupdate/addHotFix/'.$v['id'].'/'.$v['gu_device'].'/'.$v['gu_version']);?>" class="btn btn-primary active" role="button">添加热更新版本</a>
                                                    <button id="<?php echo $v['id'];?>" type="button" class="btn btn-danger actiondel">删除</button>
                                                    <button numid="<?php echo $v['id'];?>" type="button" class="btn btn-info tcbtn">查看标题及内容</button>
                                                </td>
                                            </tr>
                                            <tr class="hide titlecontent_<?php echo $v['id']?>">
                                                <td colspan="6">
                                                        <?php 
                                                            $titleList = json_decode(unserialize($v['gu_langtitle']), 1);
                                                            $contentList = json_decode(unserialize($v['gu_langcontent']), 1);
                                                            foreach ($titleList as $k1 => $v1) {
                                                        ?>
                                                        <div class="col-sm-3">
                                                            <input type="text" class="form-control" placeholder=".col-xs-3" value="<?php echo $v1;?>" readonly>
                                                            <br />
                                                            <textarea rows="10" class="form-control" readonly><?php echo $contentList[$k1];?></textarea>
                                                        </div>
                                                        <?php }?>
                                                </td>
                                            </tr>
                                            <?php } ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
    </div>

</div>

<div class="modal" id="actionCode" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width: 60%">
        <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 class="form-horizontal J_ajaxForm" data-validate ="ver_remark" role="form" method="post" action="gameupdate/actionAdd">
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">更新设备:</label>
                        <div class="col-sm-9">
                            <label class="checkbox-inline">
                                <input type="checkbox" name="gu_device[]" value="1" checked="checked">Android
                            </label>
                            <label class="radio-inline">
                                <input type="checkbox" name="gu_device[]" value="2"> IOS
                            </label>
                            <label class="radio-inline">
                              <input type="checkbox" name="gu_device[]" value="3"> VIVO
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">版本:</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" placeholder="版本" name="gu_version">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">导入提示语:</label>
                        <div class="col-sm-9">
                            <button type="button" class="btn btn-primary" onclick="importData()">导入</button>
                        </div>
                    </div>

                    <div class="langContent2 form-group">
                        <label class="col-sm-3 control-label">更新内容:</label>
                        <div class="col-sm-9">
                            <textarea cols="50"  rows="7" class="form-control" placeholder="更新提示语言:th,zh,en,tw &#10;格式：&#10;<th></th>&#10;<zh-CN></zh-CN>&#10; <en-US></en-US>&#10;<zh-TW></zh-TW>&#10;<hi></hi>" name="langContentPri" id="langContentPri"></textarea>
                        </div>
                    </div>
                    <div id="langDiv" >
                        <?php foreach ($langList as $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 $v.'_title';?>" value="" name="title_<?php echo $v;?>"/>
                                    <textarea  rows="7" cols="50" class="form-control <?php echo $v.'_content';?>" name="content_<?php echo $v;?>"></textarea>
                                </div>
                            </div>
                        <?php }?>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">是否显示提示:</label>
                        <div class="col-sm-9">
                            <label class="radio-inline">
                              <input type="radio" name="gu_tips" value="1" checked="checked"> 是
                            </label>
                            <label class="radio-inline">
                              <input type="radio" name="gu_tips" value="0"> 否
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">是否强制更新:</label>
                        <div class="col-sm-9">
                            <label class="radio-inline">
                              <input type="radio" name="gu_force" value="1"> 是
                            </label>
                            <label class="radio-inline">
                              <input type="radio" name="gu_force" value="0" checked="checked"> 否
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">是否消息中心:</label>
                        <div class="col-sm-9">
                            <label class="radio-inline">
                                <input type="radio" name="gh_news" value="1"> 是
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="gh_news" value="0" checked="checked"> 否
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">是否奖励更新:</label>
                        <div class="col-sm-9">
                            <label class="radio-inline">
                                <input type="radio" name="gu_isReward" value="1"> 是
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="gu_isReward" value="0" checked="checked"> 否
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">更新奖励:</label>
                        <div class="col-sm-9">
                            <div class="col-sm-6">
                                <label class="col-sm-3 control-label">体力:</label>
                                <div class="col-sm-9">
                                    <input type="number" class="form-control" value="0" name="gu_reward[spins]">
                                </div>
                            </div>
                            <div class="col-sm-6">
                                <label class="col-sm-3 control-label">金币:</label>
                                <div class="col-sm-9">
                                    <input type="number" class="form-control" value="0" name="gu_reward[money]">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">更新备注:</label>
                        <div class="col-sm-9">
                            <textarea class="form-control gucontent remark" rows="5" placeholder="更新备注,必填" name="remark"></textarea>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary J_ajaxSubmitBtn">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="<?php echo base_url();?>public/js/common.js"></script>
<script type="text/javascript">

    var lastStr = '<?php echo addslashes(json_encode($last));?>';

    $("input[name='gu_device']").click(function(){
        var device = $(this).val(),
            androidPackage = $('.android-package'),
            iosPackage = $('.ios-package');

        if ( device == 1 ) {
            iosPackage.addClass('hide');
            androidPackage.removeClass('hide');
        }else{
            androidPackage.addClass('hide');
            iosPackage.removeClass('hide');
        }
    });

    $('.actiondel').click(function(){
        if(confirm('确定删除吗?')){
            var id = $(this).attr('id');
            $.ajax({
                type: 'POST',
                url: "gameupdate/actionDel",
                dataType: 'json',
                data: "id="+id,
                success: function(e){
                    if( e.code >= 1){
                        alert('删除成功');
                        window.location.reload();
                    }else{
                        alert('删除失败');
                    }
                }
            });
        }
    });

    //添加新的语言包
    var langContentNum = 1;
    $('.addUpdateConbtn').click(function(){
        $('.langContent:first').clone().insertAfter('.langContent:last');
        langContentNum++;
        if (langContentNum > 1) {
            $('.delUpdateConbtn').removeAttr('disabled');
        }
        console.log(langContentNum);
    });

    $('.delUpdateConbtn').click(function(){
        if (langContentNum <= 1) {
            $('.delUpdateConbtn').attr('disabled',"true");
            return false;
        }
        langContentNum--;
        $('.langContent:last').remove();
        if (langContentNum == 1) {
            $('.delUpdateConbtn').attr('disabled',"true");
        }
    });

    //弹出多语言的标题和内容, 不会用toggle
    $('.tcbtn').click(function(){
        var id = $(this).attr('numid');
        var targetTr = '.titlecontent_'+id;
        var hasClass = $(targetTr).hasClass('hide');
        if (hasClass) {
            $(targetTr).removeClass('hide');
        }else{
            $(targetTr).addClass('hide');
        }
    });

    //记录
    function ver_remark($from) {
        var title = $from.find('textarea[name="remark"]');
        if ($.trim(title.val()) == '') {
            return '备注不能为空';
        }

        return true;
    }

    //添加更新内容
    $('#langContentPri').change(function () {
        var langContent, contentMatch;
        langContent = $('#langContentPri').val();

        //英文更新
        contentMatch = langContent.match(/<en-US>([\s\S]*?)<\/en-US>/);
        showContent(contentMatch, $('#langDiv .en_title'), $('#langDiv  .en_content'));

        //德語更新
        contentMatch = langContent.match(/<de-DE>([\s\S]*?)<\/de-DE>/);
        showContent(contentMatch, $('#langDiv .de_title'), $('#langDiv  .de_content'));

        //西班牙更新
        contentMatch = langContent.match(/<es-ES>([\s\S]*?)<\/es-ES>/);
        showContent(contentMatch, $('#langDiv .es_title'), $('#langDiv  .es_content'));

        //法語更新
        contentMatch = langContent.match(/<fr-FR>([\s\S]*?)<\/fr-FR>/);
        showContent(contentMatch, $('#langDiv .fr_title'), $('#langDiv  .fr_content'));

        //法語更新
        contentMatch = langContent.match(/<pt-PT>([\s\S]*?)<\/pt-PT>/);
        showContent(contentMatch, $('#langDiv .pt_title'), $('#langDiv  .pt_content'));

        //泰文更新
        contentMatch = langContent.match(/<th>([\s\S]*?)<\/th>/);
        showContent(contentMatch, $('#langDiv .th_title'), $('#langDiv  .th_content'));

        //中文更新
        contentMatch = langContent.match(/<zh-CN>([\s\S]*?)<\/zh-CN>/);
        showContent(contentMatch, $('#langDiv .zh_title'), $('#langDiv  .zh_content'));

        //台湾更新
        contentMatch = langContent.match(/<zh-TW>([\s\S]*?)<\/zh-TW>/);
        showContent(contentMatch, $('#langDiv .tw_title'), $('#langDiv  .tw_content'));

        //印地语更新
        contentMatch = langContent.match(/<hi>([\s\S]*?)<\/hi>/);
        showContent(contentMatch, $('#langDiv .hi_title'), $('#langDiv  .hi_content'));

        //俄语更新
        contentMatch = langContent.match(/<ru>([\s\S]*?)<\/ru>/);
        showContent(contentMatch, $('#langDiv .ru_title'), $('#langDiv  .ru_content'));

    });

    //全选/全不选
    $('#android-checkall').change(function () {
        if($(this).is(':checked')) {//已经是全选的状态
            $('.android-check').prop('checked', true);
        }else {
            $('.android-check').prop('checked', false);
        }
    });
    //全选/全不选
    $('#ios-checkall').change(function () {
        if($(this).is(':checked')) {//已经是全选的状态
            $('.ios-check').prop('checked', true);
        }else {
            $('.ios-check').prop('checked', false);
        }
    });

    /**
     * 更加输入内容截取显示标题和内容
     * @param contentMatch
     * @param titleElement
     * @param contentElement
     */
    function showContent(contentMatch, titleElement, contentElement) {
        var head = '', content = '', contentArr = [];
        if(!isEmpty(contentMatch)) {
            content = contentMatch[1];
            contentArr = content.trim().split(/[\n]/);
            if(contentArr.length > 0) {//如果截取的数组不为空，则取第一个为标题，其他的为内容
                head = contentArr[0];
                contentArr.splice(0, 1);
                content = contentArr.join('\n');
            }
        }
        titleElement.val(head);
        contentElement.val(content);
    }

    function importData() {
        var last = JSON.parse(lastStr)
        var content = last['content']
        var title = last['title']
        for (let i in title){
            $('#langDiv .'+i+'_title').val(title[i])
        }
        for (let i in content){
            $('#langDiv .'+i+'_content').val(content[i])
        }
    }
    
</script>