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





{% load tj_filters %}
  
    <style>      
       

    </style>

    {% block js %}
        {{ block.super }}
        
        /*
        $('#div_label,#subdiv_label,.add-div,.add-subdiv').attr("disabled",true);
        */



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



        //2022-11-30      
        //2;"";"2022-11-29 11:24:41.014811+08";320;"宴会申请审批通过"
        //3;"";"2022-11-30 11:39:34.227765+08";293;"下级经销商收货确认"        
        $('#sel-triggers').select2({ data:{{triggers|safe}}, multiple:true });         
        //select2 数据回显
        var arr_triggers=[];  
        {% for t in form.instance.triggers %}
            arr_triggers.push('{{t.id}}');
            {% ifequal t.id '2' %}
                $('#pkg_banquet').val({{t.details.package}});
                $('#val_banquet').val({{t.details.value}});
            {% endifequal%}
            {% ifequal t.id '3' %}
                $('#pkg_receipt').val({{t.details.package}});
                $('#val_receipt_lv1').val({{t.details.value_level1}});
                $('#val_receipt_lv2').val({{t.details.value_level2}});
            {% endifequal%}
        {% endfor %}          
        //显示隐藏相关DIV
        $("#sel-triggers").change(function() {
            var bReceipt=false,bBanquet=false;
            if($("#sel-triggers").is(':visible')){
                var ts=$('#sel-triggers').select2('data');
                for(var k in ts){
                    if(ts[k].id==2){bBanquet=true};
                    if(ts[k].id==3){bReceipt=true};
                };
            };
            if(bReceipt){
                $('#div_receipt').show();
            }else{
                $('#div_receipt').hide();
            };
            if(bBanquet){
                $('#div_banquet').show();
            }else{
                $('#div_banquet').hide();
            };
        });
        $('#sel-triggers').val(arr_triggers);  
        $('#sel-triggers').trigger("change");  



        {% ifnotequal user.company.level_id 1%}
            {% if not user.perms.company.sys_admin%}
                $('#data-content input,#data-content .btn,#data-content select').attr("disabled",true);
            {% endif %}
        {% endifnotequal %}
        

        {% if form.instance.company %}
            $('#co').val('{{form.instance.company.id}}');
            $('#id_channelrebate_promotion-__prefix__-company').val('{{form.instance.company}}');
        {% else %}
            $('#co').val('');
            $('#product_label,#agent_label,#div_label,#subdiv_label').attr("disabled","disabled");
        {% endif %}

        $('#co').change(function(){
            $('#id_company').val($(this).val());
            var b,bLblShow;
            if($('#co').val()==''){
                b=true;
            }else{
                b=false;
            };            
            $('#product_label,#agent_label,#div_label,#subdiv_label').attr("disabled",b);                 
        });           
       

        $('.datepicker').datepicker({dateFormat: 'yy-mm-dd'});
        //$('.datepicker').datepicker('setDate',new Date());
        setDatePickerZh();



        $.datetimepicker.setLocale('zh');
        $('.datetimepicker').datetimepicker({
            i18n:{
                zh:{
                    months:['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月',],
                    dayOfWeek:["一", "二", "三", "四","五", "六", "日",]
                }
            },
            //timepicker:true,
            format:'Y-m-d H:i:s',        
        });


        $('#product_label').autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/product/',
                    dataType: "json",
                    data: {
                        term:   $("#product_label").val() ,//搜索栏里的内容
                        company_id:  $('#co').val() ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $("#product_id").val(ui.item.value);
                $("#product_label").val(ui.item.label);
                return false;
            }
        });

        $('#agent_label').autocomplete({   
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/agent/',
                    dataType: "json",
                    data: {
                        term:   $("#agent_label").val() ,//搜索栏里的内容
                        company_id:  $('#co').val() ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },            
            select: function (event, ui) {
                $("#agent_id").val(ui.item.value);
                $("#agent_label").val(ui.item.label);
                return false;
            }
        });
        $('#div_label').autocomplete({
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/saldiv/',
                    dataType: "json",
                    data: {
                        term:   $("#div_label").val() ,//搜索栏里的内容
                        company_id:  $('#co').val() ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $("#div_id").val(ui.item.value);
                $("#div_label").val(ui.item.label);
                
                //2020-10-19
                get_subdivs(ui.item.value);    
                return false;
            }
        });

        //$('#subdiv_label').autocomplete({
        //    source: function(request, response) {
        //        $.ajax({
        //            url: '/obj/ajax/autocomplete/subdiv/',
        //            dataType: "json",
        //            data: {
        //                term:   $("#subdiv_label").val() ,//搜索栏里的内容
        //                company_id:  $('#co').val() ,//额外参数
        //            },
        //            success: function(data) {
        //                response(data);
        //            }
        //        });
        //    },  
        //    select: function (event, ui) {
        //        $("#subdiv_id").val(ui.item.value);
        //        $("#subdiv_label").val(ui.item.label);
        //        return false;
        //    }
        //});

        $('#div_id').change(function(){
            var saldiv_id=$(this).val();           
            
            if (saldiv_id==''){
                $('#subdiv_id').empty();
                $('#subdiv_id').append('<option value="">所有片区</option>')
            }else{
                get_subdivs($(this).val());
            }
        });

        function get_subdivs(saldiv_id){
            var me=$('#subdiv_id');
            $.ajax({
                url: "/obj/ajax/subsaldiv/"+saldiv_id+"/",
                success: function(data) {
                    $(me).empty();
                    $.each(data,function(i,d){$(me).append("<option value='"+d[0]+"'>"+d[1]+"</option>")});                   
                },
                dataType: 'json'               
            });  
        };


        $('.add-product').click(function(){
            var id=$('#product_id').val();
            var label=$('#product_label').val();
            if(id==''){
                toastr.error("请先选择产品！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            }else{
                var e=false;
                $("#sel-product 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-product').append("<option value='"+id+"'>"+label+"</option>");
                }
            };
        });
        $('.add-agent').click(function(){
            var id=$('#agent_id').val();
            var label=$('#agent_label').val();
            if(id==''){
                toastr.error("请先选择经销商！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            }else{
                var e=false;
                $("#sel-agent 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-agent').append("<option value='"+id+"'>"+label+"</option>");
                }
            };
        });
        $('.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>");
                }
            };
        });

        $('.add-subdiv').click(function(){
            var id=$('#subdiv_id').val();
            {% ifequal user.id 1 %}
                var label=$('#subdiv_label').val();
            {% else %}
                var label=$('#subdiv_id').find("option:selected").text();
            {% endifequal %}
            if(id==''){
                toastr.error("请先选择分区！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            }else{
                var e=false;
                $("#sel-subdiv 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-subdiv').append("<option value='"+id+"'>"+label+"</option>");
                }
            };
        });


        $('.add-rebate').click(function(){           

            var prefix='#id_channelrebate_promotion-';
            var count = $('#formset').find('.rebate').length;
            var tmplMarkup = $('#empty-form').html();
            var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);     
            $('#formset').append(compiledTmpl);
            $(prefix+'TOTAL_FORMS').attr('value', (count+1));   
        });        
        

        $('#obj_form').on('click','.add-pval',function(){
            var div=$(this).closest('.rebate');
            var sel=$(div).find('.sel-plv');
            var id=sel.val();
            var label=sel.find("option:selected").text();
            var e=false;
            var val=$(div).find('.pval').val();

           

            if($.isEmpty(id)){
                toastr.error("营销层级不能为空", "操作错误：",
                   {timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            };

            if(val==''){
                toastr.error("未输入层级返利金额", "操作错误：",
                   {timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            };
            $(div).find(".sel-plvs option").each(function(){
                if($(this).val() == id){
                    e=true;
                    return false;
                }
            });
            if(e){
                toastr.error(label+"已在列表中！", "操作错误：",
                   {timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            }else{
                $(div).find('.sel-plvs').append("<option value='"+id+"' o-vl='"+val+"' o-nm='"+label+"' o-rc='"+
                $(sel).find("option:selected").attr("o-rec")+"'>"+label+":"+val+"</option>");
            };

            //2020-11-03
            $('#formset .select2_multiple').on('dblclick','option',function(){
                $(this).remove();
            });

        });

        $('#obj_form').on('change','.sel-lv',function(){
            var me=$(this);
            $.ajax({
                url: '/company/ajax/parent/level/'+$(this).val()+'/',
                dataType: "json",
                success: function(data) {
                    var div=$(me).closest('.rebate');
                    var html='';
                    var lv,b;
                    for(i in data.parents){
                        lv=data.parents[i];
                        html=html+"<option value='"+lv.id+"' o-rec='"+lv.rec+"'>"+lv.name+"</option>";
                    };
                    $(div).find('.sel-plv').html(html);
                    if(html==''){
                        b=true;
                    }else{
                        b=false
                    }
                    $(div).find('.add-pval').attr('disabled',b);
                }
            });
        });

        $('.btn-save').click(function(){      
            if($.isEmpty($('#id_company').val()) || $.isEmpty($('#id_tm_start').val()) || $.isEmpty($('#id_tm_end').val()) || $.isEmpty($('#id_name').val())){
                toastr.error("公司名称,活动名称,开始时间,结束时间 不能为空", "操作错误：",
                        {timeOut: 1800, positionClass: "toast-center-center"}
                );
                return;
            };


            //2022-11-30            
            var ts=$('#sel-triggers').select2('data');
            var a=[],d,details={};
            for(var k in ts ){
                switch (ts[k].id) {
                    case "2":       //宴会申请
                        details={
                            "package":$('#pkg_banquet').val(),
                            "value":($.isEmpty($('#val_banquet').val())?0:$('#val_banquet').val())
                        };
                        break;
                    case "3":       //商收货确认  
                        details={
                            "package":$('#pkg_receipt').val(),
                            "value_level1":($.isEmpty($('#val_receipt_lv1').val())?0:$('#val_receipt_lv1').val()),
                            "value_level2":($.isEmpty($('#val_receipt_lv2').val())?0:$('#val_receipt_lv2').val()),
                        };
                        break;
                };
                a.push({'id':ts[k].id, 'text':ts[k].text, 'details':details});

            };    
            $('#id_triggers').val(JSON.stringify(a));
            



            //2021-06-23
            if($('#id_scan_type').val()==2){
                var a=[];
                var sa=[];
                var customer_rebates='';
                var lv_id;
                $('#customer-rebates table tbody tr').each(function(){
                    if($.isEmpty($(this).find('.lv_val').val())){
                        $(this).find(".lv_del").attr("checked",true);
                    }else{
                        if($(this).find('.lv_del').attr('checked')!=true){                    
                            lv_id=$(this).find('.lv_lv').val();
                            if(a.indexOf(lv_id)==-1){
                                sa.push('{'+
                                    '"id":'+lv_id+','+
                                    '"name":"'+$(this).find('.lv_lv').find("option:selected").text()+'",'+
                                    '"value":'+$(this).find('.lv_val').val()+
                                '}') 
                            };
                        }
                    }
                }); 
                $('#id_rebates').val('['+sa.join(',')+']');                
            }else{
                var a=[];
                var s='';
                $("#formset .rebate").each(function (){
                    a=[];
                    var me=$(this);
                    $(me).find('.sel-plvs option').each(function(){
                        s=$(this).val()+'|'+$(this).attr('o-rc')+'|'+$(this).attr('o-vl')+'|'+$(this).attr('o-nm');
                        a.push(s);
                    });
                    if(a.length>0){
                        $(me).find('.pvals').val(a.join('&'));
                    };
                });
                a=[];
                $("#sel-product option").each(function (){
                    a.push($(this).val());
                });  
                if(a.length>0){
                    $('#id_products').val(a.join(','));
                }else{
                    $('#id_products').val('');
                };
                a=[];
                $("#sel-agent option").each(function (){
                    a.push($(this).val());
                });  
                if(a.length>0){
                    $('#id_agents').val(a.join(','));
                }else{
                    $('#id_agents').val('');
                };
                a=[];
                $("#sel-div option").each(function (){
                    a.push($(this).val());
                });  
                if(a.length>0){
                    $('#id_div_ids').val(a.join(','));
                }else{
                    $('#id_div_ids').val('');
                };
                a=[];
                $("#sel-subdiv option").each(function (){
                    a.push($(this).val());
                });  
                if(a.length>0){
                    $('#id_subdiv_ids').val(a.join(','));
                }else{
                    $('#id_subdiv_ids').val('');
                };
            };
            $.SubmitForm('#obj_form form','{{request.path}}');
        });
        
        {% for f in formset %}
                var prefix='#id_channelrebate_promotion-';
                var count = $('#formset').find('.rebate').length;
                var tmplMarkup = $('#empty-form').html();
                var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);
                $('#formset').append(compiledTmpl);
                $(prefix+count+'-id').val('{{f.instance.id}}');
                $(prefix+count+'-level').val('{{f.instance.level.id}}');
                $(prefix+count+'-vtype_id').val('{{f.instance.vtype_id}}');
                $(prefix+count+'-val').val('{{f.instance.val}}');
                $(prefix+count+'-distance').val('{{f.instance.distance}}');
                if("{{f.instance.pvals|default_if_none:''}}".length>0){
                    var html='';
                    var aa='{{f.instance.pvals}}'.split('&');
                    for(i=0;i<aa.length;i++){
                        a=aa[0].split('|');
                        html=html+"<option value='"+a[0]+"' o-rc='"+a[1]+"' o-vl='"+a[2]+"' o-nm='"+a[3]+"'>"+a[3]+':'+a[2]+"</option>";
                    };
                    $(prefix+count).find('.sel-plvs').html(html);
                };
                $(prefix+'TOTAL_FORMS').attr('value', (count+1));
        {% endfor %}








        
        //2020-11-06
        $('#data-content [data-toggle="tooltip"]').tooltip();


              
        $('#id_scan_type').change(function(){

            // 2022-12-01
            var bLblShow;
            
            if($(this).val()==1){  
                $('#formset').show();
                $('#formset-add').show();
                $('#div-package').show(); 
                $('#customer-rebates').hide();   
                $('#customer-rebate-add').hide();
                $('#div-terminal-qualified').hide();
                $('#div-end-level').hide();  
                
                // 2022-12-01
                bLblShow=true;
            }else{
                $('#formset').hide();
                $('#div-package').hide();   
                $('#formset-add').hide();
                $('#customer-rebates').show();  
                $('#customer-rebate-add').show();
                $('#div-terminal-qualified').show();
                $('#div-end-level').show(); 
                
                // 2022-12-011
                if($(this).val()==3){
                    bLblShow=false;
                }else{
                    bLblShow=true;
                };               
            };

            //2022-12-01
            if(bLblShow){
                $('#div_lblscan,#customer-rebates').show();
                $('#div_triggers,#div_banquet,#div_receipt').hide();
            }else{
                $('#div_lblscan,#customer-rebates').hide();
                $('#div_triggers').show();                
            };  

            $('#sel-triggers').trigger("change");  


        });
        
        //2022-12-01
        $('#id_scan_type').change(); 


        $('.customer-rebate-add').click(function(){
            var html="<tr><td><input type='checkbox' class='form-control lv_del' /></td>"+
                "<td><select class='lv_lv form-control'>"+
                    "<option value='2'>一级经销商</option>"+ 
                    "<option value='4'>二级经销商</option>"+  
                    "<option value='5'>三级经销商</option>"+  
                    "<option value='6'>四级经销商</option>"+  
                    "<option value='7'>五级经销商</option>"+  
                "</select></td>"+
                "<td><input class='form-control lv_val' /></td>"+
                "</tr>";
            $("#tb-customer-rebates").append(html);
        });




        //2023-05-04
        var cnt_acc=0;
        $('.aformset-show').click(function(){
            $('#adlg').modal('show');        
        });  
        {% for f in aformset %}                
                var prefix='#id_channelpromotionaccumulatins_promotion-';
                var count = {{forloop.counter0}};  
                $(prefix+count+'-id').val('{{f.instance.id|default_if_none:""}}');
                $(prefix+count+'-company').val('{{request.user.company_id}}');
                $(prefix+count+'-cycler').val('{{f.instance.cycler}}');
                $(prefix+count+'-label_pkg').val('{{f.instance.label_pkg}}');
                $(prefix+count+'-amount').val('{{f.instance.amount}}');
                $(prefix+count+'-priority').val('{{f.instance.priority}}');                 
                $(prefix+'TOTAL_FORMS').attr('value', (count+1));
                cnt_acc=cnt_acc+1;
        {% endfor %}
        //不足3行添加空行
        while(cnt_acc<3){
            var prefix='#id_channelpromotionaccumulatins_promotion-';
            var count=cnt_acc;     
            var tmplMarkup = $('#atb_empty').html();
            var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);
            $('#atb').append(compiledTmpl);                
            $(prefix+count+'-id').val('');
            $(prefix+count+'-company').val('{{request.user.company_id}}');
            $(prefix+count+'-label_pkg').val('1');
            $(prefix+count+'-amount').val('');
            $(prefix+count+'-priority').val('');                 
            $(prefix+'TOTAL_FORMS').attr('value', (count+1));   
            cnt_acc++;
        };     


    {% 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 %}
    {{ block.super }}     


    <div class="form-group">
        
        <label class="col-sm-1 control-label ">{% trans '触发类型' %} </label>        
        <div class="col-sm-2">
            {{form.scan_type}}       
        </div>


        <label class="col-sm-1 control-label ">{% get_verbose_field_name form.instance 'name' %} </label>
        <div class="col-sm-2">{{ form.name }}</div>
        <label class="col-sm-1 control-label ">{% get_verbose_field_name form.instance 'awardcategory' %} </label>
        <div class="col-sm-2">{{ form.awardcategory }}</div>

        <label class="col-sm-1 control-label ">{% get_verbose_field_name form.instance 'priority' %} </label>
        <div class="col-sm-2">{{ form.priority }}</div>
    </div>

   
    <div class="form-group " style="margin-bottom:0px;padding-bottom:0px;">
        <label class="col-sm-1 control-label">{% get_verbose_field_name form.instance 'products' %}</label>
        <div class='col-sm-2'>
            <div class="input-group">
                <input type="text" class="form-control" id="product_label" placeholder="所有产品">
                <input type="hidden" id="product_id" />
                <span class="input-group-btn"><button type="button" class="btn btn-success add-product">
                <span class='glyphicon glyphicon-arrow-down'></span></button></span>
            </div>        
        </div>  

        <label class="col-sm-1 control-label">{% get_verbose_field_name form.instance 'agents' %}</label>
        <div class='col-sm-2'>
            <div class="input-group">            
                <input  class=' form-control' id="agent_label" placeholder='所有经销商'/>
                <input type="hidden" id="agent_id" />
                <span class="input-group-btn"><button type="button" class="btn btn-success add-agent">
                    <span class='glyphicon glyphicon-arrow-down'></span></button></span>
            </div>
        </div> 

        <label class="col-sm-1 control-label ">{% get_verbose_field_name form.instance 'div_ids' %} </label>
        <div class='col-sm-2'>
            <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 saldivision %}
                            <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>

        <label class="col-sm-1 control-label ">{% get_verbose_field_name form.instance 'subdiv_ids' %} </label>
        <div class='col-sm-2'>
            <div class="input-group">
                <!--
                <input  type="text" class=' form-control' id="subdiv_label" placeholder='所有分区'/>
                <input type="hidden" id="subdiv_id" />
                -->
                <select  id="subdiv_id"><option>所有分区</option></select>
                <span class="input-group-btn"><button type="button" class="btn btn-success add-subdiv">
                    <span class='glyphicon glyphicon-arrow-down'></span></button></span>
            </div>
        </div>
       
    </div>

    <div class="form-group" style='padding-top:0px;margin-top:0px;'>
        <div class='col-sm-2 col-md-offset-1' >
            <select class="select2_multiple form-control" id="sel-product" multiple="multiple">
                {% for o in products %}
                    <option value='{{o.0}}'>{{o.1}}</option>
                {% endfor %}                                     
            </select>        
        </div>
        <div class='col-sm-2 col-md-offset-1'>
            <select class="select2_multiple form-control" id='sel-agent' multiple="multiple">
                {% for o in agents %}
                    <option value='{{o.0}}'>{{o.1}}</option>
                {% endfor %}   
                                  
            </select>        
        </div>
        <div class='col-sm-2 col-md-offset-1'>
            <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 class='col-sm-2 col-md-offset-1'>
            <select class="select2_multiple form-control" id="sel-subdiv" multiple="multiple">
                {% for o in subdiv_ids %}
                    <option value='{{o.0}}'>{{o.1}}</option>
                {% endfor %}        
            </select>        
        </div>
    </div>    
    



     <div class="form-group ">
        <label class="col-sm-1 control-label">
            <span title="经销商扫码" data-placement="top" data-toggle="tooltip">活动开始时间</span>
        </label>
        <div class='col-sm-2'> {{ form.tm_start }}</div>
        <label class="col-sm-1 control-label">
            <span title="经销商扫码" data-placement="top" data-toggle="tooltip">活动结束时间</span>
        </label>
        <div class='col-sm-2'> {{ form.tm_end }}</div>
        <label class="col-sm-1 control-label">
            <span title="公司发货" data-placement="top" data-toggle="tooltip">最早发货日期</span>
        </label>
        <div class='col-sm-2'> {{ form.dt_delivery }}</div>
        
        <label class="col-sm-1 control-label">{% get_verbose_field_name form.instance 'status' %}</label>
        <div class='col-sm-2'> {{ form.status }}</div>        
    </div>



    <div class="form-group ">
        <label class="col-sm-1 control-label">
            <span title="1积分=多少元" data-placement="top" data-toggle="tooltip">积分兑换比率</span>
        </label>
        <div class='col-sm-2'> {{ form.point_ratio }}</div>    
        <label class="col-sm-1 control-label">{% get_verbose_field_name form.instance 'digest' %}</label>
        <div class='col-sm-8'> {{ form.digest }}</div>              
    </div>





   


{% endblock %}

{% block extra %}
    <div class="form-group " style="padding:15px;">
        <div class='col-sm-11' id='div_lblscan'>
            <div id='div-package'  {% ifequal form.instance.scan_type 2 %} style="display:none;"{% endifequal%} >
                <label class="col-sm-1 control-label">{% trans '核算基本单位' %}</label>
                <div class='col-sm-2'> {{ form.package_unit }}</div>
            </div>
            <div id='div-end-level' {% ifequal form.instance.scan_type 1 %} style="display:none;"{% endifequal%} >            
                <label class='col-sm-1 control-label'>渠道终端层级</label>
                <div class='col-sm-1'>
                    {{form.end_level}}
                </div>
            </div>
        
            <div id='div-terminal-qualified' {% ifequal form.instance.scan_type 1%} style="display:none;"{% endifequal%} >            
                <label class='col-sm-2 control-label'>必经终端店扫码</label>
                <div class='col-sm-1'>
                    {{form.terminal_qualified}}
                </div>
            </div>
            
            
            
            

            <span id='formset-add' {% ifequal form.instance.scan_type 2 %} style="display:none;" {% endifequal %}>
                <a type="button" class='btn btn-success add-rebate'>
                    <span class='glyphicon glyphicon-plus' style='padding-right:5px;'></span>{% trans '添加扫码场景' %}
            </a></span>
                      



            <span id='customer-rebate-add' {% ifequal form.instance.scan_type 1 %} style="display:none;" {% endifequal %}>
                <a type="button" class='btn btn-success customer-rebate-add'>
                    <span class='glyphicon glyphicon-plus' style='padding-right:5px;'></span>{% trans '添加层级奖励' %}
            </a></span>



            <!--2023-05-04-->                              
            <span class='pull-right'>
                <a type="button" class='btn btn-success aformset-show'>
                    {% if form.instance.channelpromotionaccumulatins_promotion.all %}                        
                        {{ '修改累计扫码' }}
                    {% else %}
                        {{ '设置累计扫码返利' }}
                    {% endif %}
                </a>            
            </span>

            
        </div>
        <div class='col-sm-11' id='div_triggers' >
            <label class="col-sm-1 control-label">
                <span title="系统触发事件" data-placement="top" data-toggle="tooltip">系统触发事件</span>
            </label>
            <div class='col-sm-10'>     
                <input type='hidden' id='id_triggers' name='triggers'/>
                <select class="form-control js-example-basic-multiple" id='sel-triggers' multiple="multiple"></select>
            </div>      
        </div>
        <div class='col-sm-1'>
            <a class="btn btn-primary btn-save pull-right" type="button" >
                {% trans '保存活动设置' %}
            </a>
        </div>    
    </div>
    <div id='div_receipt' class='form-group'>
            <label class="col-sm-1 control-label">{% trans '收货确认单位' %}</label>
            <div class='col-sm-2'><select class='form-control' id='pkg_receipt'> 
                <option value='3'>箱</option>
                <option value='1'>瓶</option>
            </select></div>
            <label class="col-sm-1 control-label">{% trans '一级商奖励' %}</label>
            <div class='col-sm-2'><input class='form-control' id='val_receipt_lv1' type="number" /> </div>
            <label class="col-sm-1 control-label">{% trans '二级商奖励' %}</label>
            <div class='col-sm-2'><input class='form-control' id='val_receipt_lv2' type="number" /> </div>                
    </div>
    <div id='div_banquet' class="form-group ">
        <label class="col-sm-1 control-label">{% trans '宴会申请单位' %}</label>
        <div class='col-sm-2'><select class='form-control' id='pkg_banquet'> 
                <option value='3'>箱</option>
                <option value='1'>瓶</option>
        </select></div>        
        <label class="col-sm-1 control-label">{% trans '经销商奖励' %}</label>
        <div class='col-sm-2'><input class='form-control' id='val_banquet' type="number" /> </div> 
        <label class="col-sm-1 control-label">{% trans '触发动作' %}</label>
        <div class="col-sm-2" style='padding-top:8px;'>{% trans '审批通过' %}</div>
    </div>
{% endblock %}

{% block fmset %}
<div id="empty-form" style="display:none;">
    <div id="id_channelrebate_promotion-__prefix__" class="row x_panel rebate" style="margin-left:2px;">
        <div class="col-sm-6 ">
            <div class="form-group ">
                <label class="col-sm-2 control-label">{% trans '扫码层级' %}</label>
                <div class='col-sm-10'> {{ formset.empty_form.level }}</div>
            </div>
            <div class="form-group ">
                {{formset.empty_form.id}}{{formset.empty_form.promotion}}{{formset.empty_form.pvals}}
                <label class="col-sm-2 control-label">{% trans '扫码类型' %}</label>
                <div class='col-sm-10'> {{ formset.empty_form.vtype_id }}</div>
            </div>

            <div class="form-group ">
                <label class="col-sm-2 control-label">{% trans '返利金额' %}</label>
                <div class='col-sm-2'> {{ formset.empty_form.val }}</div>

                <label class="col-sm-2 control-label">{% trans '距离限定' %}</label>
                <div class='col-sm-2'> {{ formset.empty_form.distance }}</div>

                <div class="col-sm-4">
                    <label class="control-label col-md-offset-2">{% trans '勾选删除当前扫码场景' %}</label>
                    <div class="pull-right"> {{ formset.empty_form.DELETE }}</div>
                </div>
            </div>
        </div>
        <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-sm-2 control-label">{% trans '经销商层级返利' %}</label>
                    <div class="col-sm-4"><select class="sel-plv"></select></div>
                    <div class="col-sm-5"><input placeholder="返利金额(点值)" class="pval"></div>
                    <span class="input-group-btn"><button type="button" class="btn btn-success add-pval  pull-right">
                        <span class='glyphicon glyphicon-arrow-down'></span></button></span>
                </div>
                <div class="form-group">
                    <select class="select2_multiple form-control sel-plvs"  multiple="multiple"></select>
                </div>
        </div>
    </div>
</div>





<div id="formset">
    {{ formset.management_form }}
</div>

<div id='customer-rebates' class='x_panel' style="margin-top:-20px;{% ifequal form.instance.scan_type 1 %} display:none;{% endifequal %}" >
    <table id='tb-customer-rebates' class="table table-striped table-bordered dt-responsive" width="100%">
        <thead><tr>
            <th>删除</th><th>经销商层级</th><th>返利金额(点值)</th>
        </tr></thead>
        <tbody>        
        {% for r in form.instance.rebates %}<tr>
            <td><input type="checkbox" class="form-control lv_del" /></td>
            <td><select class='lv_lv form-control'>
                <option value='2' {% ifequal r.id 2%}selected="selected"{% endifequal %}>一级经销商</option> 
                <option value='4' {% ifequal r.id 4%}selected="selected"{% endifequal %}>二级经销商</option>  
                <option value='5' {% ifequal r.id 5%}selected="selected"{% endifequal %}>三级经销商</option>  
                <option value='6' {% ifequal r.id 5%}selected="selected"{% endifequal %}>四级经销商</option> 
                <option value='7' {% ifequal r.id 5%}selected="selected"{% endifequal %}>五级经销商</option> 
            </select></td>
            <td><input class='form-control lv_val' value='{{r.value}}' /></td>
        </tr>{% endfor %}        
        </tbody>
    </table>
</div>



<!--2023-05-05-->
<div id='adlg' class='modal'>    
    <div class="modal-dialog modal-lg">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
            <h4 class="modal-title">累计返利设置</h4>
        </div>
        <div class="modal-body">           
            <table id="atb_empty" style="display:none;">
            <tr id="id_channelpromotionaccumulatins_promotion-__prefix__" >                
                {{aformset.empty_form.id}}
                {{aformset.empty_form.company}}   
                <td>{{ aformset.empty_form.DELETE }}</td>  
                <td>{{aformset.empty_form.awardcategory}}</td>  
                <td>{{aformset.empty_form.cycler}}</td>                
                <td>{{aformset.empty_form.label_pkg}}</td> 
                <td>{{aformset.empty_form.amount}}</td>
                <td>{{aformset.empty_form.priority}}</td>
                <td>{{aformset.empty_form.target}}</td>

            </tr>
            </table>

            <table id='atb' class="table table-condensed table-bordered table-hover">
            <thead><tr>
                <th>{% trans '删除' %}</th>
                <th>{% trans '奖励方式' %}</th>     
                <th>{% trans '扫码基数' %}</th>
                <th>{% trans '计数单位' %}</th>
                <th>{% trans '返利' %}</th>               
                <th>{% trans '优先级' %}</th>   
                <th>{% trans '反利对象' %}</th>              
            </tr></thead>
            <tbody>
                {% for f in aformset %}
                <tr id="id_channelpromotionaccumulatins_promotion-__prefix__{{forloop.counter0}}">
                    {{f.id}}{{f.company}}                  
                    <td>{{f.DELETE}}</td>      
                    <td>{{ f.awardcategory }}</td>
                    <td>{{ f.cycler }}</td>
                    <td>{{ f.label_pkg }}</td>
                    <td>{{ f.amount}}</td>    
                    <td>{{ f.priority}}</td>  
                    <td>{{ f.target }}</td>                  
                </tr>
                {% endfor %}
            </tbody>
            {{ aformset.management_form }}                
        </table>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>                
        </div>
        </div>
    </div>
    </div>
    </div>
</div>



{% endblock %}













