<link rel="stylesheet" href="<?php echo base_url();?>public/plugins/datatables/jquery.dataTables.min.css">
<link rel="stylesheet" href="<?php echo base_url();?>public/plugins/datatables/extensions/Responsive/css/dataTables.responsive.css">
<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">
                                        <button type="button" class="btn btn-success" data-toggle="modal" data-target="#actionCode">创建补偿礼包</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                    <section>
                        <div class="container-fluid">
                            <div class="row">
                                <div class="col-sm-12">
                                    <div style="margin-bottom:20px;">
                                        <form class="form-inline" role="form" method="POST" action="#user/gift" name="stanpform">
                                            <input type="hidden" name="fromuser_gift" value="1">
                                            <div class="form-group">
                                                <label>uid</label>
                                                <input type="text" class="form-control datetimepicker" value="<?php if($uid) echo $uid;?>" placeholder="uid" name="uid" id="uid"/>
                                            </div>
                                            <div class="form-group">
                                                <label>开始时间</label>
                                                <input type="text" class="form-control datetimepicker" value="<?php if($stime) echo $stime;?>" placeholder="开始时间" name="stime" id="stime"/>
                                            </div>
                                            <span class="gap"></span>
                                            <div class="form-group">
                                                <label>结束时间</label>
                                                <input type="text" class="form-control datetimepicker" value="<?php if($etime) echo $etime;?>" placeholder="结束时间" name="etime" id="etime"/>
                                            </div>
                                            <button type="submit" class="btn btn-primary">查询</button>
                                        </form>
                                    </div>
                                    <div class="table-responsive">
                                        <table id="exchangeTable" class="table table-striped table-bordered table-hover" cellspacing="0" width="100%">
                                            <thead>
                                            <tr>
                                                <th>序号</th>
                                                <th>UID</th>
                                                <th width="80">奖励类型</th>
                                                <th>奖励</th>
                                                <th>创建时间</th>
                                                <th>过期时间</th>
                                                <th>领奖时间</th>
                                                <th>备注</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                            <tbody>

                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
    </div>

</div>

<!-- 操作金币弹出框 -->
<div class="modal fade" id="actionCode" 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 class="form-horizontal J_ajaxForm" role="form" method="post" action="User/actionGift" data-callBack="updatecode">
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-sm-3 control-label">奖励对象:</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" placeholder="uid" name="uid">
                        </div>
                    </div>
                    <div class="form-group addOrDelControl" id="vipDiv">
                        <label class="col-sm-3 control-label">奖励道具:</label>
                        <div class="col-sm-9">
                            <div class="col-sm-12" style="margin-top: 1rem;">
                                <button type="button" class="btn btn-success addParent" >新增道具</button>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">过期时间:</label>
                        <div class="col-sm-9">
                            <input type="text" id="test-limit2" class="form-control" placeholder="奖励过期日期" name="expired_at">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">备注：</label>
                        <div class="col-sm-9">
                            <textarea name="remark" class="form-control" rows="4"></textarea>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button style="display: none" type="button" class="btn btn-primary J_ajaxSubmitBtn">提交</button>
                    <button type="button" class="btn btn-primary" onclick="actionConfirm()">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="<?php echo base_url();?>public/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="<?php echo base_url();?>public/plugins/datatables/extensions/Responsive/js/dataTables.responsive.min.js"></script>
<script type="text/javascript">
    laydate.render({
        elem: '#test-limit2',
        type: 'datetime'
    });
</script>
<script type="text/javascript">
    var idData = JSON.parse('<?php echo json_encode($props); ?>');
    $('.addParent').on("click",function(){
        var str = '<div class="col-sm-12" style="margin-top: 1rem;padding-left:0"><div class="col-sm-5"><select class="form-control typeControl" name="props[id][]">';
        for(var x in idData) {
            str += '<option value="'+x+'">'+idData[x]['msg']+'</option>';
        }
        str +='</select></div><div class="col-sm-5"><input type="number" onchange="checkPropsId(this.value,this)" class="form-control" placeholder="道具数量" name="props[num][]" min="1" value="1"></div><div class="col-sm-1"><button type="button" class="btn btn-danger deleteParent" >删除</button></div></div>';
        $(this).parent().before(str);
    });

    function checkPropsId(num,obj){
        if($(obj).parent().prev().children().val() == 15){
            if(num >= 5000){
                alert("不允许发放大于5000的体力值");
                $(obj).val(1);
            }else if(num >= 1000){
                if(confirm("确认发放大于1000的体力值")!=true){
                    $(obj).val(1);
                }
            }
        }
    }

    $('.addOrDelControl').on("click",'.deleteParent',function(){
        $(this).parent().parent().remove();
    });
    function updatecode(data) {
        if(data.ret != 0){
            alert(data.ms)
        }else{
            location.reload();
        }
    }
    $(function () {
        var exchangeTable = $("#exchangeTable").DataTable({
            "paging": true,
            // "select": true,
            // "processing": true,
            "serverSide": true,
            "language": {
                'lengthMenu': '每页显示 _MENU_ 记录',
                'zeroRecords': '没有数据',
                'info': ' 第_PAGE_ 页/ _PAGES_页，共 _TOTAL_ 条',
                'infoEmpty': '没有符合条件的记录',
                'search': 'uid查找',
                'infoFiltered': '(从  _MAX_ 条记录中过滤)',
                'paginate': { "first":  "首页 ", "last": "末页", "next": "下一页","previous": "上一页"}

            },
            "ajax": {
                url: rootPath + "user/gift",
                type:"post",
                //增加参数查询参数
                data:function(data){
                    data.uid = $('#uid').val();
                    data.stime = $('#stime').val();
                    data.etime = $('#etime').val();
                }
            },
            "lengthChange": true,
            // "ordering": false,
            "searching": false,
            "autoWidth": false,
            "columns": [
                {"data": "id"},
                {"data": "uid"},
                {"data": "from"},
                {"data": "reward", render:function (data) {
                    return '<textarea id="textareaId" rows="4" readonly class="form-control" placeholder="" name="userLimit">'+data+'</textarea>';
                }},
                {"data": "created_at"},
                {"data": "expired_at"},
                {"data": "rewarded_at"},
                {"data": "remark", render:function (data) {
                    return '<textarea id="textareaId" rows="4" readonly class="form-control" placeholder="" name="userLimit">'+data+'</textarea>';
                }},
                {"data": "id", render:function (data) {
                    return '<button class="btn btn-danger btn-sm" onclick="del('+data+')">删除</button>';
                }}
            ]
        });

        //当点击search按钮时按条件重新加载数据
        $("#searchBtn").click(function(){
            if($("#exchangeForm").valid()) {
                exchangeTable.ajax.reload();
            }
        });
    });
    function del(id) {
        var msg = "您真的确定要删除吗？\n\n请确认！";
        if (confirm(msg)==true){
            $.post(rootPath + 'User/actionGiftDel',{
                id:id,
            },function (data) {
                window.location.reload();
            });
        }
    }
    function actionConfirm() {
        if (confirm("您真的确定要发放吗？\n\n请确认！")==true){
            $(".J_ajaxSubmitBtn").click();
        }
    }
</script>