<!--<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>-->

<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 type="button" class="btn btn-success ann_add" data-toggle="modal" data-target="#action">新增公告</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>
                                            </tr>
                                            </thead>
                                            <tbody>
                                            <?php foreach($list as $v) { ?>
                                            <tr>
                                                <td><?=$v['id'];?></td>
                                                <td><?=$v['createtime'] ? date('Y-m-d H:i:s', $v['createtime']) : '-'?></td>
                                                <td><?=$v['topic']?></td>
                                                <td><?=$v['admin_user']?></td>
                                                <td><?php if(time() > $v['stime'] && time() < $v['etime']){echo '展示中';}else if($time < $v['stime']){echo '未开始';}else{echo '已结束';}?></td>
                                                <td><?php if($v['device'] == 0){echo '全部';}else if($v['device'] == 1){echo '安卓';}else if($v['device'] == 2){echo 'IOS';} ?></td>
                                                <td><?=$v['version']?implode(' | ', json_decode($v['version'], true)) : ''?></td>
                                                <td><button type="button" class="btn btn-success exitinfo" tip='<?=json_encode($v,256)?>'>详情</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" style="width:900px;">
        <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/saveNew" 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>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">公告主题:</label>
                        <div class="col-sm-6">
                            <input id="info-topic" type="text" class="form-control" placeholder="公告主题" name="topic" value=""/>
                        </div><span style="color:red;">主要为信息备注用</span>
                    </div>
                    <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" placeholder="年-月-日" class="time day datetimepicker" value="<?=date('Y-m-d')?>" name="sdate" />
                            <input type="text" placeholder="时:分:秒" class="time dtime" name="stime" value="00:00:00" />
                            <span style="color:red">精确到时分秒</span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">结束时间</label>
                        <div class="col-sm-9">
                            <input type="text" placeholder="年-月-日" class="time day" value="<?=date('Y-m-d')?>" name="edate" />
                            <input type="text" placeholder="时:分:秒" class="time dtime" name="etime" value="00:00:00" />
                            <span style="color:red">精确到时分秒</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">生效版本:</label>
                        <div class="col-sm-9" id="version">
                            <?php foreach ($versions as $v){ ?>
                            <input class="form-check-input" type="checkbox" name="version[]" value="<?=$v?>" > <?=$v?>
                            <?php } ?>
                            <span style="color:red">不勾选默认大于1.6.3版本</span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">更新到版本:</label>
                        <div class="col-sm-3">
                        <select class="col-sm-3 form-control" id="newVersion" name="newVersion">
                            <option value="">请选择</option>
                            <?php foreach ($versions as $v){ ?>
                            <option value="<?=$v?>"><?=$v?></option>
                            <?php } ?>
                        </select>
                        </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>
                            <label class="radio-inline">
                              <input type="radio" name="device" value="3" > VIVO
                            </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="is_force" checked="checked" value="0" > 否
                        </label>
                        <label class="radio-inline">
                          <input type="radio" name="is_force" 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="plat" checked="checked" value="0" > 全部
                        </label>
                        <label class="radio-inline">
                          <input type="radio" name="plat" value="1" > spin
                        </label>
                        <label class="radio-inline">
                          <input type="radio" name="plat" value="2" > fox
                        </label>
                    </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"  name="img" value="">
                    </div>
                    <div class="col-sm-3">
                        <a target="_blank" href="<?php echo $href;?>" class="btn btn-success">上传图片</a>
                    </div>
                    </div>

                    <div class="form-group">
                        <label class="col-sm-3 control-label">更新奖励:</label>
                        <div class="col-sm-9">
                            <div class="container-fluid addOrDelControl" style="border: 1px solid #CCC;margin: 20px 15px;padding-top: 10px;">
                                <button type="button" class="btn btn-success addParent" style="margin-left: 12px;margin-bottom: 10px;">增加</button>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">跳转粉丝页:</label>
                        <div class="col-sm-9" id="info-topic">
                            <input type="text" class="form-control" placeholder="跳转粉丝页链接" name="fansUrl" value="" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">配置示例:</label>
                        <div class="col-sm-9" id="info-topic">
                            <img width="100%" src="<?= base_url()?>/public/images/announcement.png"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">导入:</label>
                        <div class="col-sm-3">
                            <a style="display: none;margin-bottom: 10px;width: 80px;" class="btn btn-success" id="btn">上传</a>
                            <span id="filespan" class="btn btn-success fileinput-button">
                                <input type="file" id="upfile">
                            </span>
                        </div>

                        <div class="col-sm-5 pull-right exprt"><a style="padding-top:10px;" href="" class="btn btn-success">导出</a></div>
                    </div>
                    <div id="info-content">
                    <?php foreach ($lang as $row){ ?>
                        <div class="form-group info-title">
                            <label class="col-sm-3 control-label">(<?=$row?>)起头:</label>
                            <div class="col-sm-9">
                                <input type="text"  class="form-control" placeholder="起头" id="title_<?=$row?>" name="title[<?=$row?>]" value="" />
                            </div>
                        </div>
                        <div class="form-group info-content">
                            <label class="col-sm-3 control-label">(<?=$row?>)正文:</label>
                            <div class="col-sm-9">
                                <textarea class="form-control" rows="5" placeholder="" id="content_<?=$row?>" name="content[<?=$row?>]" ></textarea>
                            </div>
                        </div>
                        <div class="form-group info-time_desc">
                            <label class="col-sm-3 control-label">(<?=$row?>)更新时间:</label>
                            <div class="col-sm-9">
                                <input type="text"  class="form-control" id="time_desc_<?=$row?>" placeholder="更新时间" name="time_desc[<?=$row?>]" value="" />
                            </div>
                        </div>
                        <div class="form-group info-range_desc">
                            <label class="col-sm-3 control-label">(<?=$row?>)更新范围:</label>
                            <div class="col-sm-9">
                                <input type="text"  class="form-control" id="range_desc_<?=$row?>" placeholder="更新范围" name="range_desc[<?=$row?>]" value=""/>
                            </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>

<script type="text/javascript">
    var idData = JSON.parse('<?php echo json_encode($idData); ?>');
    var langs = JSON.parse('<?php echo json_encode($lang); ?>');
    var chestSkin = JSON.parse('<?php echo json_encode($chestSkin); ?>');

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

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

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

    $("#btn").click(function(){
        var formData = new FormData();
        formData.append("file",document.getElementById("upfile").files[0]);
        $.ajax({
            type: "POST", // 数据提交类型
            url: "Import/announceNews", // 发送地址
            data: formData, //发送数据
            async: true, // 是否异步
            processData: false, //processData 默认为false，当设置为true的时候,jquery ajax 提交的时候不会序列化 data，而是直接使用data
            contentType: false, //
            success: function(data) { // 成功回调
                data = JSON.parse(data);
                if(data.length <= 0){
                    alert('导入格式错误');
                    return;
                }
                for(var x in data) {
                    if($("#range_desc_"+x).length){
                        $("#range_desc_"+x).val(data[x].range_desc);
                    }
                    if($("#time_desc_"+x).length){
                        $("#time_desc_"+x).val(data[x].time_desc);
                    }
                    if($("#title_"+x).length){
                        $("#title_"+x).val(data[x].title);
                    }
                    if($("#content_"+x).length){
                        $("#content_"+x).val(data[x].content);
                    }
                }
            }
        });
    });

    $("#filespan").on("change", "input[type='file']", function() {
        var filePath = $(this).val();
        //如果仅上传图片 if(filePath.indexOf("jpg") != -1 || filePath.indexOf("png") != -1) {
        if (filePath) {
            $('#btn').show();
        } else {
            return false
        }
    });

    $('.addParent').on("click",function(){
        var obj = $(this).parent('div').find('.form-group');
        var str = "";
        str += '<div class="form-group"><label class="col-sm-1 control-label">道具*: </label><div class="col-sm-3"><select class="form-control propsSelect" name="reward[props][propsId][]" data-index="'+obj.length+'">';
        for(var x in idData) {
            str += '<option value="'+x+'">'+idData[x]['msg']+'</option>';
        }
        str += '</select></div><label class="col-sm-1 control-label">数量*: </label><div class="col-sm-2"><input type="text" class="form-control"  name="reward[props][num][]" value="1"></div>';
        str += '<div class="skinSelect"><input type="hidden" name="reward[props][skin][]" value=""></div>';
        str += '<button type="button" class="btn btn-danger deleteParent" >删除</button></div>';
        $(this).before(str);
    });

    $(document).on("change",".propsSelect",function(){
        var value = $(this).val();
        var index = $(this).attr("data-index");
        var str = '';
        if(value == 19){
            str = '<label class="col-sm-1 control-label">皮肤*: </label>';
            str += '<div class="col-sm-2">';
            str += '<select class="col-sm-3 form-control" name="reward[props][skin]['+index+']">';
            for(var x in chestSkin) {
                str += '<option value="'+x+'">'+chestSkin[x]+'</option>';
            }
            str += '</select></div>';
        }else{
            str = '<input type="hidden" name="name" value="">';
        }
        $(this).parent("div").parent("div").find(".skinSelect").html(str);
    });

    $('.exitinfo').click(function () {
        var info = JSON.parse($(this)[0].getAttribute('tip'));
        $("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);

        $('.exprt>a').attr('href', "/Export/announceNews/"+info['id']);
        $('.exprt').show();

        $('.addOrDelControl>.form-group').each(function(){
            $(this).remove();
        });

        //公告主题
        $('#info-topic').val(info['topic']);
        //粉丝页
        $("input[name='fansUrl']").val(info.fansUrl);
        //更新奖励

        var reward = info['reward'] ? JSON.parse(info['reward']) : {};
        if(reward['props']){
            var str = "";
            for(k in reward['props']){
                str += '<div class="form-group"><label class="col-sm-1 control-label">道具*: </label><div class="col-sm-3"><select class="form-control propsSelect" name="reward[props][propsId][]">';
                for(var x in idData) {
                    if(reward['props'][k]['propsId'] == parseInt(x)){
                        str += '<option value="'+x+'" selected>'+idData[x]['msg']+'</option>';
                    } else {
                        str += '<option value="'+x+'">'+idData[x]['msg']+'</option>';
                    }
                }
                str += '</select></div><label class="col-sm-1 control-label">数量*: </label><div class="col-sm-2"><input type="text" class="form-control" name="reward[props][num][]" value="'+reward['props'][k]['num']+'"></div>';
                if(reward['props'][k]['propsId'] == 19){
                    str += '<label class="col-sm-1 control-label">皮肤*: </label>';

                    str += '<div class="col-sm-2">';
                    str +='<select class="form-control col-sm-3" name="reward[props][skin][]">';
                    for(var x in chestSkin) {
                        if(reward['props'][k]['skin'] == parseInt(x)){
                            str += '<option value="'+x+'" selected >'+chestSkin[x]+'</option>';
                        } else {
                            str += '<option value="'+x+'" >'+chestSkin[x]+'</option>';
                        }
                    }
                    str+='</select></div>';

                }else{
                    str += '<div class="skinSelect"><input type="hidden" name="reward[props][skin][]" value=""></div>';
                }
                str += '<button type="button" class="btn btn-danger deleteParent">删除</button></div>';
            }
            $('.addParent').before(str);
        }

        //生效版本
        var versions = $.parseJSON(info.version);
        $('#version input[type="checkbox"]').each(function(){
            var value = $(this).val();
            if($.inArray(value, versions) != -1){
                $(this).attr('checked', true);
            }
        });

        $("#newVersion").find("option").each(function(){
            $(this).attr('selected', false);
        });
        if(info.newVersion == ""){
            $("#newVersion").find("option").eq(0).attr("selected", true);
        }else{
            $("#newVersion option[value='"+info.newVersion+"']").attr("selected",true);
        }

        //生效平台
        $(":radio[name='device'][value='" + parseInt(info.device) + "']").prop("checked", "checked");
        //是否强更
        $(":radio[name='is_force'][value='" + parseInt(info.is_force) + "']").prop("checked", "checked");
        //生效平台 
        $(":radio[name='plat'][value='" + parseInt(info.plat) + "']").prop("checked", "checked");

        //背景资源
        $("input[name='img']").val(info.img);

        //$("#info-content").children(".info-content").remove();
        var content    = $.parseJSON( info.content);
        var ititle     = $.parseJSON( info.title);
        var time_desc  = $.parseJSON( info.time_desc);
        var range_desc = $.parseJSON( info.range_desc);
        for (key in langs) {
            var k = langs[key];
            if(content == null || typeof content[k] == 'undefined'){
                $("#content_"+k).val('');
            }else{
                $("#content_"+k).val(content[k]);
            }
            if(ititle == null || typeof ititle[k] == 'undefined'){
                $("#title_"+k).val('');
            }else{
                $("#title_"+k).val(ititle[k]);
            }
            if(time_desc == null || typeof time_desc[k] == 'undefined'){
                $("#time_desc_"+k).val('');
            }else{
                $("#time_desc_"+k).val(time_desc[k]);
            }
            if(range_desc == null || typeof range_desc[k] == 'undefined'){
                $("#range_desc_"+k).val('');
            }else{
                $("#range_desc_"+k).val(range_desc[k]);
            }
        }
        //$('.addParent').hide();

        $("#action").modal('show');
    });
    
    $('.ann_add').click(function(){
        $('#btn').hide();
        $('input[type="text"]').each(function(){
            $(this).val("");
        });
        $('.exprt').hide();
        $('.dtime').val('00:00:00');

        $("#newVersion").find("option").each(function(){
            $(this).attr('selected', false);
        });
        $("#newVersion").find("option").first().attr("selected", true);

        $('.addOrDelControl>.form-group').each(function(){
            $(this).remove();
        });

        for (key in langs) {
            $("#content_"+langs[key]).val('');
            $("#time_desc_"+langs[key]).val('');
            $("#title_"+langs[key]).val('');
            $("#range_desc_"+langs[key]).val('');
        }

        $('input[type="checkbox"]').each(function(){
            $(this).removeAttr('checked');
        });
        $('input[type="radio"]').each(function(){
        });
        $('.addParent').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)+':'+add0(s);
        }
    }
    function add0(m){return m<10?'0'+m:m }
</script>
