<link rel="stylesheet" href="<?php echo base_url();?>public/js/date/datedropper.min.css">
<!--<link rel="stylesheet" href="--><?php //echo base_url();?><!--public/js/time/timedropper.css">-->
<script src="<?php echo base_url();?>public/js/date/datedropper.min.js"></script>
<!--<script src="--><?php //echo base_url();?><!--public/js/time/timedropper.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 type="button" class="btn btn-success addBtn" >新增公告</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>
                                                <th>开始时间</th>
                                                <th>结束时间</th>
                                                <th>操作</th>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php foreach($list as $v) { ?>
                                            <tr>
                                                <td><?php echo $v['id'];?></td>
                                                <td><?php echo $v['title'];?></td>
                                                <td><?php if($v['device'] == 0){echo '全部';}else if($v['device'] == 1){echo '安卓';}else if($v['device'] == 2){echo 'IOS';} ?></td>
                                                <td><?php if($v['usertype'] == 0){echo '全部';}else if($v['usertype'] == 1){echo '游客';}else if($v['usertype'] == 2){echo 'FB';} ?></td>
<!--                                                <td>--><?php //if($v['type'] == 0){echo '否';}else if($v['type'] == 1){echo '是';}?><!--</td>-->
                                                <td><span class="btn btn-success content" tip='<?php echo base64_encode($v['content']);?>'>详情</span></td>
                                                <td><?php echo date('Y-m-d H:i:s',$v['stime']);?></td>
                                                <td><?php echo date('Y-m-d H:i:s',$v['etime']);?></td>
                                                <td><button type="button" class="btn btn-success exitinfo" tip='<?php echo base64_encode(json_encode($v));?>'>编辑</button><button type="button" class="btn btn-danger" onclick="deleteTask(<?php echo $v['id'];?>)">删除</button></td>
                                            </tr>
                                            <?php } ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    <nav>
                                        <ul class="pagination">
                                            <li class="disabled"><a href="#">共<span class="pagetotal"><?php echo $total;?></span>条</a></li>
                                        </ul>
                                    </nav>
                                </div>
                            </div>
                        </div>
                    </section>
                </aside>
            </div>
        </div>
    </div>

</div>

<div class="modal fade" id="action" 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="Announcement/save" data-callBack="message">
                <div class="modal-body">
                    <style>
                        .time{
                            height: 34px;
                            padding: 6px 12px;
                            font-size: 14px;
                            line-height: 1.42857143;
                            color: #555;
                            background-color: #fff;
                            background-image: none;
                            border: 1px solid #ccc;
                            border-radius: 4px;
                        }

                    </style>
                    <input type="hidden" name="id" value=""/>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">开始时间</label>
                        <div class="col-sm-9">
                            <input type="text" class="time" value="<?php echo date('Y-m-d');?>" name="sdate"/>
                            <input type="text" class="time" name="stime" value="00:00" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">结束时间</label>
                        <div class="col-sm-9">
                            <input type="text" class="time" value="<?php echo date('Y-m-d');?>" name="edate"/>
                            <input type="text" class="time" name="etime" value="00:00" />
                        </div>
                    </div>
                    <div class="langContent2 form-group">
                        <label class="col-sm-3 control-label">uid白名单:</label>
                        <div class="col-sm-6" id="info-wlist">
                            <textarea cols="50"  rows="7" class="form-control" placeholder="uid白名单（停服用户可以登录 用,间隔）" name="wlist"></textarea>
                        </div>
                    </div>
                    <div class="langContent2 form-group">
                        <label class="col-sm-3 control-label">uid黑名单(测试):</label>
                        <div class="col-sm-6" id="info-blist">
                            <textarea cols="50"  rows="7" class="form-control" placeholder="uid黑名单（停服用户可以登录 用,间隔）" name="blist"></textarea>
                        </div>
                    </div>
                    <div class="form-group" style="display: none">
                        <label class="col-sm-3 control-label">是否停服:</label>
                        <div class="col-sm-9">
                            <label class="radio-inline">
                                <input type="radio" name="type" value="0" checked="checked"> 否
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="type" value="1"> 是
                            </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="usertype" value="0" checked="checked"> 全服
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="usertype" value="1"> FB
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="usertype" value="2"> 游客
                            </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="device" value="0" checked="checked"> 全部
                            </label>
                            <label class="radio-inline">
                              <input type="radio" name="device" value="1"> 安卓
                            </label>
                            <label class="radio-inline">
                              <input type="radio" name="device" value="2"> 苹果
                            </label>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">消息标题:</label>
                        <div class="col-sm-9" id="info-title">
                            <textarea class="form-control" rows="5" placeholder="消息标题" name="title"></textarea>
                        </div>
                    </div>
                    <div id="info-content">
                    <?php foreach ($lang as $langKey => $row){ ?>
                        <div class="form-group info-content">
                            <label class="col-sm-3 control-label">消息内容(<?php echo $row;?>):</label>
                            <div class="col-sm-9">
                                <textarea class="form-control" rows="5" placeholder="" name="content[<?php echo $langKey;?>]"></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 J_ajaxSubmitBtn">提交</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="actionContent" 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" >公告详情</h4>
            </div>
            <form class="form-horizontal J_ajaxForm" role="form" >
                <div class="modal-body" id="modelBody">

                </div>
            </form>
        </div>
    </div>
</div>



<script type="text/javascript">
    var rootPath = '<?php echo site_url();?>';
    $(document).ready(function() {
        $("#pickdate").dateDropper({
            animate: true,
            format: 'Y-m-d',
            maxYear: '2025',
            minYear: '2019',
            lang:'zh'
        });
    });

    function message(returnData, $from) {
        if(returnData.ret != '0'){
            alert(returnData.msg);
        }else{
            window.location.reload();
        }
    }

    function deleteTask(id) {
        if(confirm('确定要删除吗')==true){
            datas = {id:id};
            $.ajax({
                url: rootPath + 'announcement/del',
                type: 'POST',
                data: datas,
                dataType:'json',
                success: function (data) {
                    alert(data.msg);window.location.reload()
                },
                error: function (data) {
                    alert(data.msg);
                }
            });
            // $.post('/announcement/del',{
            //     id:id
            // },function (data) {
            //     console.log(data);
            //     // window.location.reload()
            // });
        }
    }


    $('.content').click(function () {
        var str2 =atob($(this)[0].getAttribute('tip'))
        var content = JSON.parse(str2);
        var str = ''
        for (const key in content) {
            str += '<div class="form-group"><label class="col-sm-3 control-label">消息内容('+key+'):</label>\<div class="col-sm-9"><textarea class="form-control" rows="5" placeholder="" name="">'+content[key]+'</textarea></div></div>';
        }
        $("#modelBody").html(str);
        $('#actionContent').modal('show');
    });
    $('.addBtn').click(function () {
        var info = <?php echo $defaultInfo;?>
        // var info = JSON.parse();
        console.log(info);
        $("input[name='id']").val(info['id']);
        var sdate = getdate(info['stime'],1);
        var stime = getdate(info['stime'],2);
        var edate = getdate(info['etime'],1);
        var etime = getdate(info['etime'],2);
        $("input[name='sdate']").val(sdate);
        $("input[name='stime']").val(stime);
        $("input[name='edate']").val(edate);
        $("input[name='etime']").val(etime);

        $("#info-wlist").children().empty();
        var html = '<textarea class="form-control" rows="5" placeholder="uid白名单（停服用户可以登录 用,间隔）" name="wlist">'+info['wlist']+'</textarea>';
        $("#info-wlist").html(html);

        $("#info-blist").children().empty();
        var html = '<textarea class="form-control" rows="5" placeholder="uid黑名单（停服用户可以登录 用,间隔）" name="blist">'+info['blist']+'</textarea>';
        $("#info-blist").html(html);

        $(":radio[name='type'][value='" + parseInt(info['type']) + "']").prop("checked", "checked");
        $(":radio[name='usertype'][value='" + parseInt(info['usertype']) + "']").prop("checked", "checked");
        $(":radio[name='device'][value='" + parseInt(info['device']) + "']").prop("checked", "checked");

        $("#info-title").children().empty();
        var html = '<textarea class="form-control" rows="5" placeholder="消息标题" name="title">'+info['title']+'</textarea>';
        $("#info-title").html(html);

        $("#info-content").children(".info-content").remove();
        var str='';
        var content = $.parseJSON( info['content']);
        console.log(content);
        for (const key in content) {
            str += '<div class="form-group info-content"><label class="col-sm-3 control-label">消息内容('+key+'):</label>\<div class="col-sm-9"><textarea class="form-control" rows="5" placeholder="" name="content['+key+']">'+content[key]+'</textarea></div></div>';
        }
        $("#info-content").html(str);

        // $("#action").addClass("in");
        $("#action").modal('show');

    });
    $('.exitinfo').click(function () {
        var str2 =atob($(this)[0].getAttribute('tip'))
        var info = JSON.parse(str2);
        console.log(info);
        $("input[name='id']").val(info['id']);
        var sdate = getdate(info['stime'],1);
        var stime = getdate(info['stime'],2);
        var edate = getdate(info['etime'],1);
        var etime = getdate(info['etime'],2);
        $("input[name='sdate']").val(sdate);
        $("input[name='stime']").val(stime);
        $("input[name='edate']").val(edate);
        $("input[name='etime']").val(etime);

        $("#info-wlist").children().empty();
        var html = '<textarea class="form-control" rows="5" placeholder="uid白名单（停服用户可以登录 用,间隔）" name="wlist">'+info['wlist']+'</textarea>';
        $("#info-wlist").html(html);

        $("#info-blist").children().empty();
        var html = '<textarea class="form-control" rows="5" placeholder="uid黑名单（停服用户可以登录 用,间隔）" name="blist">'+info['blist']+'</textarea>';
        $("#info-blist").html(html);

        $(":radio[name='type'][value='" + parseInt(info['type']) + "']").prop("checked", "checked");
        $(":radio[name='usertype'][value='" + parseInt(info['usertype']) + "']").prop("checked", "checked");
        $(":radio[name='device'][value='" + parseInt(info['device']) + "']").prop("checked", "checked");

        $("#info-title").children().empty();
        var html = '<textarea class="form-control" rows="5" placeholder="消息标题" name="title">'+info['title']+'</textarea>';
        $("#info-title").html(html);

        $("#info-content").children(".info-content").remove();
        var str='';
        var content = $.parseJSON( info['content']);console.log(content);
        for (const key in content) {
            str += '<div class="form-group info-content"><label class="col-sm-3 control-label">消息内容('+key+'):</label>\<div class="col-sm-9"><textarea class="form-control" rows="5" placeholder="" name="content['+key+']">'+content[key]+'</textarea></div></div>';
        }
        $("#info-content").html(str);

        // $("#action").addClass("in");
        $("#action").modal('show');

    });
    // $('.close').click(function () {
    //     $("#action").hide();
    // });
    function getdate(str,type){
        var time  = new Date(parseInt(str) * 1000);
        var y = time.getFullYear();
        var m = time.getMonth()+1;
        var d = time.getDate();
        var h = time.getHours();
        var mm = time.getMinutes();
        var s = time.getSeconds();
        if(type==1){
            return y+'-'+add0(m)+'-'+add0(d);
        }else if(type==2){
            return add0(h)+':'+add0(mm);
        }
    }
    function add0(m){return m<10?'0'+m:m }
</script>
