{% extends 'obj_form.html' %}
{% load i18n %}





{% load tj_filters %}
  
   
    {% block js %}
        {{ block.super }}    
        {% if not form.instance.id %}
            $('#id_content').attr('placeholder','请在此输入/编辑通知信息正文');
        {% endif %}
        $('#tb tbody td.attachment').find('br').remove(); 
        $('#tb tbody td.attachment input[type="file"]').removeClass('form-control');

        var fmst_id='#id_bulletinboardattach_set-';
        var prefix=fmst_id+'__prefix__';      
       
        $('#div_label').autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/saldiv/',
                    dataType: "json",
                    data: {
                        term:   $("#div_label").val() ,//搜索栏里的内容
                        company_id:{{request.user.company.id}}  ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $("#div_id").val(ui.item.value);
                $("#div_label").val(ui.item.label);     
                return false;
            }
        });      
       

        
        $('.add-div').click(function(){
            var id=$('#div_id').val();
            {% ifequal user.id 1 %}
                var label=$('#div_label').val();
            {% else %}
                var label=$('#div_id').find("option:selected").text();
            {% endifequal %}            
            if(id==''){
                toastr.error("请先选择战区！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            }else{
                var e=false;
                $("#sel-div option").each(function(){
                    if($(this).val() == id){
                        e=true;
                        return false;          
                    }
                });
                if(e){
                    toastr.error(label+"已在列表中！", "操作错误：",
                        {timeOut: 800, positionClass: "toast-center-center"}
                    );
                    return;
                }else{
                    $('#sel-div').append("<option value='"+id+"'>"+label+"</option>");
                }
            };
        });

        

        $('#data-content .btn-add').click(function(){  
            var count = $('#tb tr').length;
            $(prefix+' .sn').text(count);
            var tmplMarkup = $(prefix).html();
            var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);     
            $('#tb').append('<tr>'+compiledTmpl+'</tr>');
            $(fmst_id+'TOTAL_FORMS').attr('value', (count+1));   

            
        });        
        
        $('#data-content .select2_multiple').on('dblclick','option',function(){
            $(this).remove();
        });


        $('.btn-save').click(function(){   
            var a=[];
            var s='';            
            
            $("#sel-div option").each(function (){
                a.push($(this).val());
            });  
            if(a.length>0){
                $('#id_salesdivisions').val('['+a.join(',')+']');
            }else{
                $('#id_salesdivisions').val('[]');
            };                
            
            tinyMCE.triggerSave();
            $.SubmitForm('#obj_form form','{{request.path}}');
        });
        
        
        tinymce.init({ 
            //selector:'id_content',
            //mode:'textareas', 
            selector: "textarea",
            convert_urls: false,
            height: 400,
            inline:false,            
            images_upload_url:'/company/ajax/uploadfile/',
            plugins: "table,link, uploadimage",           

            menubar:false,
            statusbar: false,
            relative_urls: false,
            fontsize_formats: "10pt 12pt 13pt 14pt 18pt 24pt 36pt",

            toolbar:"undo redo | formatselect | bold italic strikethrough forecolor backcolor fontsizeselect | table link uploadimage  | alignleft aligncenter alignright alignjustify  | numlist bullist outdent indent",
  
            language:'zh_CN',                
        });
        

        
        






    {% endblock %}


    {% block subtitle-1 %}
        {% trans "公司管理" %}
    {% endblock %}
    {% block subtitle-2 %}
        {% trans "公告信息" %}
    {% endblock %}

    <span style="margin-left:10px; color:green; ">
        {% block subtitle-3 %}
            {% if form.instance.id %}
                {% trans "修改" %}
            {% else %}
                {% trans "新建" %}
            {% endif %}
        {% endblock %}
    </span>



{% block form_content %}
    <style>
        #obj_form .x_panel{        
            margin-bottom:2px;
        }
        #tb {
            background:white;
        }
        #tb .attachment input[type="file"]{
            display:inline-block;
        }
    </style>



    {{ block.super }}   
    
    <div class='col-sm-8'>    
        <label class="col-sm-2 control-label ">{% trans '信息类别' %}</label>
        <div class="col-sm-4" >
            {{form.category}}
        </div> 

        <label class="col-sm-2 control-label ">{% trans '发布范围' %}</label>
        <div class="col-sm-4" style='margin-bottom:10px;'>
            {{form.scope}}
        </div>      
    </div>
    <div class='col-sm-4'>
        <div class='col-sm-12'>
            <div class="input-group">  
                {% ifequal user.id 1 %}
                    <input type="text" class=' form-control' id="div_label" placeholder='所有战区'/>
                    <input type="hidden" id="div_id" />                    
                {% else %}
                    <select id='div_id'>
                        <option value=''>所有战区</option>
                        {% for o in sal_divs %}
                            <option value='{{o.0}}'>{{o.1}}</option>
                        {% endfor %}                        
                    </select>
                {% endifequal %}
                <span class="input-group-btn"><button type="button" class="btn btn-success add-div">
                    <span class='glyphicon glyphicon-arrow-down'></span></button></span>
            
            </div>
        </div>          
    </div>


    <div class="col-sm-8">
        
        <label class="col-sm-2 control-label ">{% trans '标题摘要' %}</label>
        <div class="col-sm-10" style='margin-bottom:10px;'>
            {{form.subject}}
        </div>

        <label class="col-sm-2 control-label ">
            {% trans '备注说明' %}
            <div style="font-size:0.7rem;font-style:oblique;color:#888;">
                不会在前端显示
            </div>
        </label>
       
        <div class="col-sm-9">
            {{form.remark}}
        </div>             
       

    </div>
   
    <div class='col-sm-4'>
        <div class='col-sm-12'>
            <select class="select2_multiple form-control" id="sel-div" multiple="multiple">
                {% for o in div_ids %}
                    <option value='{{o.0}}'>{{o.1}}</option>
                {% endfor %}                       
            </select>     
        </div>
    </div>
   
       

    
    
      
    
   
{% endblock %}



{% block fmset %}

    <div style='margin-top:0px;margin-bottom:10px;'>{{ form.content }}</div>



    <div class="form-group" style="display:flex;align-items: center;">       
        {% trans "附件数量:" %}
        <span class='cnt-attached' style="margin-left:0.5rem;"> {{form.instance.bulletinboardattach_set.all.count}}</span>   
        <a type='button' class="btn btn-primary btn-add" style='margin-left:20px;'>            
            <span class='glyphicon glyphicon-arrow-down' style='padding-right:5px;'></span>
            {% trans "添加附件" %}</a></span>
        <a style="background: #dc2828;border-radius:7px;color:#fff;font-weight:bold;padding:0.4rem 1rem;font-size:0.9rem;margin-left:2rem;" 
            href="https://davis.china315net.com/static/tools/imgCompress-multi.html">图片压缩工具</a>
        <a class="btn btn-primary btn-save pull-right" style="margin-left:auto;" type="submit">{% trans '保存' %}</a>        
    </div>



    
   
        
    <table style='display:none'>
            <tr id="id_bulletinboardattach_set-__prefix__" >                
                {{formset.empty_form.id}}{{formset.empty_form.bulletinboard}}    
                <td class='sn'></td>
                <td>{{formset.empty_form.attachment}}</td>
                <td>{{formset.empty_form.DELETE}}</td>
            </tr>
    </table>
    <div class='table-responsive'>
        <table id='tb' class="table table-condensed table-bordered table-hover">
            <thead><tr>                
                <th>{% trans '序号' %}</th>
                <th>{% trans '附件' %}</th>    
                <th>{% trans '删除' %}</th>                
            </tr></thead>
            <tbody>
                {% for f in formset %}
                <tr  class='p'>
                    {{f.id}}{{f.bulletinboard}}  
                    <td class='sn'>{{forloop.counter}}</td> 
                    <td class='attachment'>{{ f.attachment }}</td>    
                    <td>{{f.DELETE}}</td>
                </tr>
                {% endfor %}
            </tbody>
            {{ formset.management_form }}                
        </table>
    </div>

   
   

</div>
</div>
{% endblock %}













