<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改活动</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="<?php echo base_url();?>public/js/bootstrap/bootstrap.min.css" >
    <link rel="stylesheet" href="<?php echo base_url();?>public/js/vendor/bootstrap-datepicker/css/bootstrap-datepicker.standalone.min.css">
    <link rel="stylesheet" href="<?php echo base_url();?>public/css/jquery.timepicker.min.css">
    <!-- Select2 -->
    <link rel="stylesheet" href="<?php echo base_url();?>public/js/vendor/select2/select2.min.css">

    <style type="text/css">
        .thumbnail {
            margin:10px;
        }

        .lang-name {
            margin-left:42px;
        }

        .upimg {
            cursor: pointer;
        }

    </style>
<link rel="stylesheet" href="<?php echo base_url();?>public/js/vendor/smalot_bootstrap-datetimepicker/css/bootstrap-datetimepicker.min.css">
<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">
<script src="<?php echo base_url();?>public/js/vendor/smalot_bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js"></script>
<script src="<?php echo base_url();?>public/js/vendor/smalot_bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
</head>
<body>
<div id="msg-tip">

</div>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12">
            <aside>
                <header><i class="fa fa-fw fa-file"></i>修改邮件</header>
                <hr/>
                <section>
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-sm-12">
                                <form  role="form" class="form-horizontal" method="post"  enctype="multipart/form-data">
                                    <div class="form-group">
                                        <?php if(!empty($id)){ ?>
                                        <input type="hidden" name="id" value="<?php echo $id; ?>">
                                        <?php } ?>
                                        <label class="col-sm-2 control-label">在线时间段:</label>
                                        <div class="col-sm-4">
                                            <input type="text" class="form-control datetimepicker" value="<?php if(!empty($data['online_start'])) echo date('Y-m-d H:i:s', $data['online_start']);?>" name="online_start" />
                                        </div>
                                        <div class="col-sm-4">
                                            <input type="text" class="form-control datetimepicker" value="<?php if(!empty($data['online_end'])) echo date('Y-m-d H:i:s', $data['online_end']);?>" name="online_end" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">显示天数:</label>
                                        <div class="col-sm-4">
                                            <input type="text" class="form-control" value="<?php if(!empty($data['days'])) echo $data['days'];?>" name="days" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">玩家uid列表:</label>
                                        <div class="col-sm-6">
                                            <input type="text" class="form-control" value="<?=!empty($data['uids']) ? $data['uids'] : '';?>" name="uids" placeholder="英文逗号间隔的uid" />
                                        </div>
                                        <label class="col-sm-2 control-label" style="color:red;">全服发送:</label>
                                        <div class="col-sm-2">
                                            <select name="allUser" class="form-control">
                                                <option value="1">否</option>
                                                <option value="2">是</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">等级区间:</label>
                                        <div class="col-sm-2">
                                            <input type="text" class="form-control" value="<?=!empty($data['level_start']) ? $data['level_start'] : 0;?>" name="level_start" />
                                        </div>
                                        <div class="col-sm-2">
                                            <input type="text" class="form-control" value="<?=!empty($data['level_end']) ? $data['level_end'] : 0;?>" name="level_end" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">积分区间:</label>
                                        <div class="col-sm-2">
                                            <input type="text" class="form-control" value="<?=!empty($data['score_start']) ? $data['score_start'] : 0;?>" name="score_start" />
                                        </div>
                                        <div class="col-sm-2">
                                            <input type="text" class="form-control" value="<?=!empty($data['score_end']) ? $data['score_end'] : 0;?>" name="score_end" />
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">国家或地区:</label>
                                        <div class="col-sm-10">
                                            <input type="checkbox" id="langs-checkall"> 【全选】

                                            <?php foreach ($langs as $k => $v) { ?>
                                                <label>
                                                    <input type="checkbox" class="lang-check" name="langs[]" value="<?php echo $k;?>" <?php if(!empty($data['langs']) && in_array($k, $data['langs'])) {echo 'checked'; }?> > <?php echo $v;?>
                                                </label>
                                            <?php }?>

                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">平台:</label>
                                        <div class="col-sm-4">
                                            <?php foreach ($sids as $k => $v) { ?>
                                            <label class="radio-inline">
                                            <input type="radio" name="sid" value="<?=$k?>" <?php if((!empty($data['sid']) && $k == $data['sid']) || (empty($data['sid']) && empty($k))) echo 'checked' ?> /> <?=$v?>
                                            </label>
                                            <?php }?>
                                        </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="versionMin" style="border-radius: 4px;">
                                                <option value="0">全部</option>
                                                <?php foreach ($versions as $k => $v) { ?>
                                                    <option <?php echo $data['version'][0] == $v ? "selected" : ""; ?> 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 ($versions as $k => $v) { ?>
                                                    <option <?php echo $data['version'][1] == $v ? "selected" : ""; ?> value="<?php echo $v?>"><?php echo $v?></option>
                                                <?php }?>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="form-group propsList" id="rewardData">
                                    </div>


                                    <div class="form-group">
                                        <label class="col-sm-2 control-label">邮件(国家)标题/内容:</label>
                                        <button type="button" class="btn btn-info col-cm-1" onclick="$('#titleContentFile').click()">导入Excel内容</button>
                                        <input type="file" id="titleContentFile" onchange="exportTitleContent(this)", style="display: none">
                                        <a type="button" href="/static/systemMail/template-1.xlsx" target="_blank" class="btn btn-info col-cm-1">Excel 模板下载</a>
                                    </div>
                                    <?php foreach ($langs as $k => $v) { ?>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label"><?=$v?>标题</label>
                                        <div class="col-sm-10">
                                            <label class="radio-inline">
                                            <textarea id="title_container_<?=$k?>" style="border: 1px solid #CCC;margin: 20px 15px;padding-top: 10px;width:1000px;height:80px" name="title[<?=$k?>]"><?=isset($data['title'][$k]) ? $data['title'][$k] : ''; ?></textarea>
                                            </label>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-sm-2 control-label"><?=$v?>内容</label>
                                        <div class="col-sm-10">
                                            <label class="radio-inline">
                                            <textarea id="body_container_<?=$k?>" style="border: 1px solid #CCC;margin: 20px 15px;padding-top: 10px;width:1000px;height:80px" name="body[<?=$k?>]"><?=isset($data['body'][$k]) ? $data['body'][$k] : ''; ?></textarea>
                                            </label>
                                        </div>
                                    </div>

                                    <?php }?>
                                    
                                    <div class="form-group">
                                        <div class="col-sm-offset-2 col-sm-3">
                                            <button type="button" class="btn btn-info" onclick="window.location.href='/#SystemMail/index'">返回</button>
                                            <button type="button" class="btn btn-primary" id="submitBtn">提交</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </section>
            </aside>
        </div>
    </div>
</div>

<!--
<script type="text/javascript" src="<?php echo base_url();?>public/js/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="<?php echo base_url();?>public/js/ueditor/ueditor.all.js"></script>
-->

<script src="<?php echo base_url();?>static/common/js/props.js?time=<?=time()?>"></script>
<script src="<?php echo base_url();?>static/common/js/xlsx.lib.js"></script>
<script src="<?php echo base_url();?>static/common/js/xlsx.js?time=<?=time()?>"></script>
<script>
    // propsObj= Object.create(propsClass);
    //var propsConfig  = JSON.parse('<?php //echo json_encode($propsConfig);?>//');
    var propsData = JSON.parse('<?php echo json_encode($rewardData);?>')
    propsClass.Init(propsData,'rewardData','activity');
</script>
<script>
    //window.UEDITOR_HOME_URL = "/public/js/ueditor/";
    //<?php foreach($countries as $k => $v){?>
    //    var title_<?=$k?> = UE.getEditor('title_container_<?=$k?>', {
    //        autoHeight: true
    //    });
    //    var body_<?=$k?> = UE.getEditor('body_container_<?=$k?>', {
    //        autoHeight: true
    //    });
    //<?php }?>

    $("#submitBtn").click(function () {
        var startTime = $('input[name="online_start"]').val(), endTime = $('input[name="online_end"]').val();
        if(startTime > endTime){
            alert("在线开始时间不能大于结束时间");
            return;
        }
        var days = $('input[name="days"]').val();
        if(days <= 0){
            alert("展示天数错误");
            return;
        }
        var level_start = parseInt($('input[name="level_start"]').val()), level_end = parseInt($('input[name="level_end"]').val());
        if(level_start < 0 || level_end < 0 || level_start > level_end){
            alert("等级区间错误");
            return;
        }
        var score_start = parseInt($('input[name="score_start"]').val()), score_end = parseInt($('input[name="score_end"]').val());
        if(score_start < 0 || score_end < 0 || score_start > score_end){
            alert("积分区间错误");
            return;
        }
        var allUser = $('select[name=allUser]').val()
        var uids = $('input[name=uids]').val()
        if(allUser == 1 && (!uids)){
            alert("非全服发送，请输入用户uid");
            return;
        }
        if(allUser == 2){
            if(!confirm('确认该邮件是全服邮件')){
                return;
            }
            $('input[name=uids]').val('')
        }
        if(score_start < 0 || score_end < 0 || score_start > score_end){
            alert("积分区间错误");
            return;
        }

        if( confirm('确定修改吗?') ){
            var data = $('form').serialize();
            $.ajax({
                type: 'POST',
                url: rootPath + 'SystemMail/actionEdit',
                dataType: 'json',
                data: data,
                success: function (data) {
                    if(data.ret == 0){
                        alert("修改成功");
                        $(location).attr('href', '#SystemMail/index');
                    }
	        }
            })
	}
    });

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

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

    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 >= 500){
                alert("不允许发放大于500的体力值");
                $(obj).val(1);
            }
        }
        if($(obj).parent().prev().children().val() == 14){
            if(num >= 100000000){
                alert("不允许发放大于100M的金币值");
                $(obj).val(1);
            }
        }
    }

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

    var langsMap = '<?php echo json_encode(array_flip($langs));?>';
    langsMap = JSON.parse(langsMap);
    console.log(langsMap)
    function exportTitleContent(obj) {
        showLoading()
        parseXlsx(obj, function(data){
            console.log(1)
            hideLoading()
            $.each(data, function(i, o ){
                if(langsMap[o.lang]){
                    if($('textarea[name="title['+ langsMap[o.lang]+']"]').length > 0)$('textarea[name="title['+ langsMap[o.lang]+']"]').val(o.title)
                    if($('textarea[name="body['+ langsMap[o.lang]+']"]').length > 0)$('textarea[name="body['+ langsMap[o.lang]+']"]').val(o.content)
                }
            })
        })
    }
</script>
</body>

