<link rel="stylesheet" href="<?php echo base_url();?>public/js/vendor/bootstrap-datepicker/css/bootstrap-datepicker.standalone.min.css">
<script src="<?php echo base_url();?>public/js/vendor/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script>
<script src="<?php echo base_url();?>public/js/vendor/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js"></script>
<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>
                                        <!--                                        <a style="margin-right: 5px;" href="#gameupdate/remarkList" class="btn btn-info" role="button">查看备注</a>-->
                                    </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 date('Y-m-d H:i',$v['endTime']);?></td>
                                                    <td><textarea readonly="readonly" class="form-control show" rows="5"><?php echo $v['remark'];?></textarea></td>
                                                    <td><?php echo date('Y-m-d H:i',$v['addTime']);?></td>
                                                    <td><?=!empty($v['levelId']) ? $v['levelId'] : '-';?></td>
                                                    <td>
                                                        <a href="<?php echo base_url('/#systemCompensation/edit/'.$v['id']);?>" class="btn btn-primary active" role="button">编辑</a>
                                                        <button id="<?php echo $v['id'];?>" type="button" class="btn btn-danger actiondel">删除</button>
                                                    </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="systemCompensation/actionAdd">
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">开始时间:</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="startTime" value="<?php echo date("Y-m-d H:i:s") ?>" placeholder="开始时间" name="startTime" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">结束时间:</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="endTime" value="<?php echo date("Y-m-d 23:59:59") ?>" placeholder="结束时间" name="endTime" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">登录时间:</label>
                        <div class="col-sm-1">
                            <input class='mySwitch' type="checkbox" name="loginTimeSwitch" aid="1" value="0">
                        </div>
                        <div class="col-sm-5">
                            <input type="text" class="form-control" id="loginTime" value="<?php echo date("Y-m-d H:i:s") ?> - <?php echo date("Y-m-d 23:59:59") ?>" placeholder="结束时间" name="loginTime" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">补偿等级(等级范围):</label>
                        <div class="col-sm-1">
                            <input class='mySwitch' type="checkbox" name="levelIdSwitch" aid="1" value="0">
                        </div>
                        <div class="col-sm-2">
                                <input type="number" class="form-control" value="0" min="1" name="levelIdMin">
                        </div>
                        <div class="col-sm-3">
                                <input type="number" class="form-control" value="0" min="1" name="levelIdMax">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">充值金额(金额范围):</label>
                        <div class="col-sm-1">
                            <input class='mySwitch' type="checkbox" name="paySwitch" aid="1" value="0">
                        </div>
                        <div class="col-sm-2">
                                <input type="number" class="form-control" value="0" min="1" name="payMin">
                        </div>
                        <div class="col-sm-3">
                                <input type="number" class="form-control" value="0" min="1" name="payMax">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">版本(版本范围):</label>
                        <div class="col-sm-2">
                            <select class="form-control m-b" name="versionDevice" style="border-radius: 4px;">
                                <option value="0">全部</option>
                                <?php foreach ($device as $k => $v) { ?>
                                    <option value="<?php echo $k?>"><?php echo $v?></option>
                                <?php }?>
                            </select>
                        </div>
                        <div class="col-sm-2">
                            <select class="form-control m-b" name="versionMin" style="border-radius: 4px;">
                                <option value="0">全部</option>
                                <?php foreach ($version as $k => $v) { ?>
                                    <option value="<?php echo $v?>"><?php echo $v?></option>
                                <?php }?>
                            </select>
                        </div>
                        <div class="col-sm-2">
                            <select class="form-control m-b" name="versionMax" style="border-radius: 4px;">
                                <option value="0">全部</option>
                                <?php foreach ($version as $k => $v) { ?>
                                    <option value="<?php echo $v?>"><?php echo $v?></option>
                                <?php }?>
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">地区:</label>
                        <div class="col-sm-1">
                            <input class='mySwitch' type="checkbox" name="countrySwitch" aid="1" value="0">
                        </div>
                        <div class="col-sm-6">
                            <?php $i=0; foreach ($country as $k => $v) { ?>
                                <label class="checkbox-inline" <?php if($i == 0){ echo 'style="margin-left: 10px"';} $k;?> >
                                    <input type="checkbox" name="country[]" value="<?php echo $k;?>"><?php echo $v;?>
                                </label>
                            <?php $i++; }?>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">补偿奖励:</label>
                        <div class="col-sm-9">
                            <label class="col-sm-2 control-label">体力:</label>
                            <div class="col-sm-4">
                                <input type="number" class="form-control" value="0" name="reward[spins]">
                            </div>
                            <label class="col-sm-2 control-label">金币:</label>
                            <div class="col-sm-4">
                                <input type="number" class="form-control" value="0" name="reward[money]">
                            </div>
                        </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">
                                    <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">
                            <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">
    laydate.render({
        elem: '#startTime',
        type: 'datetime'
    });
    laydate.render({
        elem: '#endTime',
        type: 'datetime'
    });
    laydate.render({
        elem: '#loginTime',
        type: 'datetime',
        range: true
    });
    $(".mySwitch").bootstrapSwitch({
        onSwitchChange:function(event, state){
            var aid = $(this).attr('aid');
            state = state ? 1 : 0;
            $(this).val(state)
        }
    });

    //添加新的语言包
    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'));

    });

    /**
     * 更加输入内容截取显示标题和内容
     * @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);
    }

    //选择日期
    $('.datetimepicker').datepicker({
        language:'zh-CN',
        format:'yyyy-mm-dd',
        zIndexOffset:906  //控制z-index
    });

    //删除礼物
    $('.actiondel').click(function(){
        if( confirm('确认删除吗?') ){
            var id = $(this).attr('id');
        }
        $.ajax({
            type: "POST",
            url : 'systemCompensation/actionDel',
            dataType: "json",
            data: "id="+id,
            success: function(e){
                alert('删除成功');
                window.location.reload();
            }
        });
    });
</script>
