<!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;
        }
        .emailInfos{
            display: none;
        }
    </style>
</head>
<body>
<form  role="form" method="post"  enctype="multipart/form-data" id="emailFrom">
<div id="msg-tip">
    <div class="form-group ">
        <label class="col-sm-2 control-label m-b">模板名称：</label>
        <div class="col-sm-4">
            <input type="text" name="title">
        </div>
    </div>
</div>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-12">
            <aside>
                <ul class="nav nav-tabs nav-pills nav-justified">

                    <?php
                    foreach ($langs as $k=>$v){
//                echo '<li role="presentation" class="active">'.$v.'</li>';
                        $sel = $k=='zh'?'"active"':null;
                        echo '<li role="presentation" class="langname '.$sel.'" value="'.$k.'" id="langname_'.$k.'"><a href="javascript:void(0);">'.$v.'</a></li>';
                    }
                    ?>
                </ul>
                <hr/>
                <section>
                    <div class="container-fluid">
                        <div class="row">
                            <div class="col-sm-12">

                                    <div class="form-group" id="emailInfo">

                                    </div>
                                    <div class="form-group">
                                        <div class="col-sm-offset-2 col-sm-3">
                                            <button type="button" class="btn btn-info" onclick="window.location.href='/#email/eamilTask'">返回</button>
                                            <button type="button" class="btn btn-primary" id="submitBtn">提交</button>
                                        </div>
                                    </div>

                            </div>
                        </div>
                    </div>
                </section>
            </aside>
        </div>
    </div>
</div>
</form>
<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>
    window.UEDITOR_HOME_URL = "/public/js/ueditor/";
    // var ue = UE.getEditor('container', {
    //     autoHeight: true
    // });
    var have = [];
    var lang_json =<?php echo json_encode($langs)?>;
    var selLang = '<?php echo $selLang;?>';
    var list_json = '<?php echo $list;?>';
    var list=[];
    var ueList=[];
    // var list = eval('(' + list_json + ')');
    // console.log(list);
    // $('#langname-zh').trigger("click");
    // document.getElementsByClassName('langname')[0].click();
    // console.log(document.getElementsByClassName('langname')[0]);

    $(document).ready(function() {
        var id=0;
        // console.log(list_json);
        if(list_json!=null&&list_json!=''){
            list = JSON.parse(list_json);
            // console.log(list);
        }else{
            $.each(lang_json,function (k,v) {
                id++;
                list.push({"id":id,"lang":k,"content":""});
            });
        }
        $.each(list,function (k,v) {
            // console.log(v);
            $('#emailInfo').append(emailInfoHtml(v));
            var ue = UE.getEditor('container'+v['lang'], {autoHeight: true});
            ueList[v['lang']] = ue;
        });
        $("#langname_zh").trigger("click");
    });

    $(document).on('change', '#selcid', function(e) {
        var cid = e.currentTarget.value;
        var url ='/#email/showEmailClass/'+cid;
        console.log(url);
        $('.selcid').attr("href",url);
    });



    $(document).on('click', '.langname', function(){
        $('.langname').removeClass('active');
        $(this).addClass('active');
        var thislang = $(this).attr('value');
        $(".emailInfos").hide();
        $("#lang_"+thislang).show();
    });


    $("#submitBtn").click(function () {
        // if( confirm('确定保存吗?') ){
            var datas = $('#emailFrom').serializeArray();
            console.log(datas);
            $.ajax({
                url: rootPath + 'email/saveEmailClass',
                type: 'POST',
                data: datas,
                dataType:'json',
                success: function (data) {
                    alert(data.msg);
                    console.log(data);
                },
                error: function (data) {
                    alert(data.msg);
                    console.log(data);
                }
            });
        // }
    });
    function emailInfoHtml(data) {
        var html='';
        content = unescape(data['content']);
        html +='<div class="emailInfos" id="lang_'+data['lang']+'">';
        html +='<input type="hidden" name="conf['+data['id']+'][id]" id="emailId'+data['lang']+'" value="'+data['id']+'">';
        html +='<input type="hidden" name="conf['+data['id']+'][lang]" id="emailId'+data['lang']+'" value="'+data['lang']+'">';
        html +='<div class="form-group"><label class="col-sm-2 control-label">标题:</label>';
        html +='<input type="text" name="conf['+data['id']+'][title]" value="'+data['title']+'"></div>';
        html +='<div class="form-group"><label class="col-sm-2 control-label">详细说明:</label>';
        html +='<div class="col-sm-10"><textarea id="container'+data['lang']+'" style="width:800px;height: 500px" name="conf['+data['id']+'][content]">'+content+'</textarea></div>';
        html +="</div></div>";
        return html;
    }

</script>
</body>

