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




{% block js %}
    {{ block.super }}     

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

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


        $.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:  $('#id_company').val() ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },  
            select: function (event, ui) {
                $("#product_id").val(ui.item.value);
                $("#product_label").val(ui.item.label);
                return false;
            }
        });

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

        

        $('.add-product').click(function(){            
            var b=false;
            var id=$('#product_id').val();
            var name=$('#product_label').val();
            var unit=$('#product_unit').val();
            var qty=$('#product_qty').val();

            $('#scheme-product tbody tr').each(function(idx,val){
                if($(this).find('.pid').val()==$('#product_id').val()){
                  b=true;
                  return;
                }
            });

            if($.isEmpty(id) || $.isEmpty(name) || $.isEmpty(unit) || $.isEmpty(qty)){
                toastr.error("产品/单位/数量不能为空！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );                
            }else{
                if (b){
                    toastr.error("此产品名称已在列表中！", "操作错误：",
                        {timeOut: 800, positionClass: "toast-center-center"}
                    );                
                }else{     
                    var prefix='#id_schemeproducts_set-';   
                    $(prefix+'__prefix__').find('.product').text(name); 
                    var count = $('#scheme-product tbody tr').length; 
                    var tmplMarkup = '<tr class="p">'+$(prefix+'__prefix__').html()+'</tr>';    
                    var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);     
                    $('#scheme-product tbody').append(compiledTmpl); 
                    $(prefix+count+'-product').val(id);   
                    $(prefix+count+'-qty').val(qty);                   
                    $(prefix+count+'-package_id').val(unit);   
                    $(prefix+'TOTAL_FORMS').attr('value', (count+1));   
                }
            }
        });
        
        $('.add-material').click(function(){
            var id=$('#material_id').val();
            var name=$('#material_label').val();           
            var qty=$('#material_qty').val();
            var b=false;         
            $('#scheme-material tbody tr').each(function(idx,val){
                if($(this).find('.mid').val()==id){
                  b=true;
                  return;
                }
            });
            if($.isEmpty(id) || $.isEmpty(name) || $.isEmpty(qty)){
                toastr.error("物料名称/数量不能为空！", "操作错误：",
                    {timeOut: 800, positionClass: "toast-center-center"}
                );                
            }else{
                if (b){
                    toastr.error("此物料名称已在列表中！", "操作错误：",
                        {timeOut: 800, positionClass: "toast-center-center"}
                    );                
                }else{                   
                    var prefix='#id_schemematerials_set-';   
                    $(prefix+'__prefix__').find('.material').text(name); 
                    var count = $('#scheme-material tbody tr').length; 
                    var tmplMarkup = '<tr class="p">'+$(prefix+'__prefix__').html()+'</tr>';    
                    var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);     
                    $('#scheme-material tbody').append(compiledTmpl); 
                    $(prefix+count+'-material').val(id);   
                    $(prefix+count+'-qty').val(qty);   
                    $(prefix+'TOTAL_FORMS').attr('value', (count+1));   
                }
            }
        });   
    $('#id_means').change(function(){
        if($(this).val()==1){
            $('#id_tm_app_latest').val('').attr("disabled",true); 
        }else{
            $('#id_tm_app_latest').attr("disabled",false);
        }
    });

    $('.btn-save').click(function(){
        a=[];
        $("#sel-div option").each(function (){
                a.push($(this).val());
        });  
        if(a.length>0){
                $('#id_div_ids').val(a.join(','));
        };
        a=[];
        $("#sel-subdiv option").each(function (){
                a.push($(this).val());
        });  
        if(a.length>0){
                $('#id_subdiv_ids').val(a.join(','));
        };


        $.SubmitForm('#obj_form form','{{request.path}}');
    });  


    //2020-11-20
    $('#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-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>");
                }
            };
    });

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




{% 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-5">{{ form.code }}</div>
    
        <label class="col-sm-1 control-label ">{% trans '方案名称' %} </label>
        <div class="col-sm-5">{{ form.name }}</div>
    </div>   


    <!--
    <div class="form-group ">
        <label class="col-sm-1 control-label ">{% trans '有效范围' %} </label>
        <div class="col-sm-5">{{form.scope}}</div>
    
        <label class="col-sm-1 control-label ">{% trans '地区' %} </label>
        <div class="col-sm-5">{{form.region}}</div>
    </div>   
    -->

    
    <div class="form-group " style="margin-bottom:0px;padding-bottom:0px;">
        <label class="col-sm-1 control-label ">{% trans '指定战区' %} </label>
        <div class="col-sm-5"> 
            <div class="input-group">  
            <select id='div_id'>
                    <option value=''>所有战区</option>
                    {% for o in saldivs %}
                        <option value='{{o.0}}'>{{o.1}}</option>
                    {% endfor %}                        
            </select>                
            <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 ">{% trans '指定分区' %} </label>
        <div class="col-sm-5">
            <div class="input-group">                 
                <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-5 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-5 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 ">{% trans '客户类型' %} </label>
        <div class="col-sm-5">{{form.agent_type}}</div>
    
        <label class="col-sm-1 control-label ">{% trans '发放条件' %} </label>
        <div class="col-sm-5">{{form.condition}}</div>
    </div>   



    <div class="form-group ">
        <label class="col-sm-1 control-label ">{% trans '发放方式' %} </label>
        <div class="col-sm-2">{{ form.means }}</div>
        <label class="col-sm-1 control-label ">{% trans '最晚申请' %} </label>
        <div class="col-sm-2">{{ form.tm_app_latest }}</div>
        <label class="col-sm-1 control-label ">{% trans '备注说明' %} </label>
        <div class="col-sm-5">{{ form.remark }}</div>
    </div>    
   



    

   

{% endblock %}


 {% block fmset %}  

        <table  style="display:none;">
            <tr id="id_schemeproducts_set-__prefix__" >  
                <td>{{pset.empty_form.DELETE}}{{pset.empty_form.id}}{{pset.empty_form.status}}</td>      
                <td>{{pset.empty_form.product}}<span class='product'></span></td> 
                <td>{{pset.empty_form.qty}}</td>
                <td>{{pset.empty_form.package_id}}</td>
                <td>{{pset.empty_form.remark}}</td>
            </tr>
        </table>
        {{ pset.management_form }}       

        <table  style="display:none;">
            <tr id="id_schemematerials_set-__prefix__" > 
                <td>{{mset.empty_form.DELETE }}{{mset.empty_form.id}}{{mset.empty_form.status}}</td>      
                <td>{{mset.empty_form.material}}<span class='material'></span></td> 
                <td>{{mset.empty_form.qty}}</td>       
                <td>{{mset.empty_form.remark}}</td>  
            </tr>
        </table>
        {{ mset.management_form }}        


        <div class="x_panel" id='scheme-product'>
            <div class="x_title">
                <h2 style='font-size:15px;'><i class="fa fa-gift"></i> 成品酒/品鉴酒  </h2>
                <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li>
                      <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                          <li><a href="#">Settings 1</a>
                          </li>
                          <li><a href="#">Settings 2</a>
                          </li>
                        </ul>
                      </li>
                      <li><a class="close-link"><i class="fa fa-close"></i></a>
                      </li>
                </ul>
                <div class="clearfix"></div>
            </div>
            <div class="x_content">                
                <div class="form-group ">
                    <div class='col-sm-6'><input id='product_label' placeholder='产品名称'><input type='hidden' id='product_id'></div>
                    <div class='col-sm-1'><input id='product_qty' placeholder='数量'></div>
                    <div class='col-sm-1'><select id='product_unit'>
                        <option value='3'>箱</option>
                        <option value='1'>瓶</option>
                    </select></div>
                    <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 class='table-responsive' style='padding:10px;'>
                    <table class="table table-condensed table-bordered table-hover">
                        <thead>
                            <th style='width:5%;'>删除</th>
                            <th style='width:35%;'>产品名称</th>
                            <th style='width:10%;'>数量</th>
                            <th style='width:10%;'>单位</th>
                            <th style='width:45%;'>备注</th>
                        </thead>
                        <tbody>
                            {% for f in pset %}
                                <tr>        
                                    <td>{{f.DELETE}}{{f.id}}{{f.status}}</td>                                    
                                    <td>{{f.product}}<span class='product'>{{f.instance.product.name}}</span></td> 
                                    <td>{{f.qty}}</td> 
                                    <td>{{f.package_id}}</td>    
                                    <td>{{f.remark}}</td>
                                </tr>
                            {% endfor %}                        
                        </tbody>
                    </table>
                </div>
            </div>
        </div>   



        <div class="x_panel" id='scheme-material'>
            <div class="x_title">
                <h2 style='font-size:15px;'><i class="fa fa-gift"></i> 促销物料  </h2>
                <ul class="nav navbar-right panel_toolbox">
                      <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                      </li>
                      <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                          <li><a href="#">Settings 1</a>
                          </li>
                          <li><a href="#">Settings 2</a>
                          </li>
                        </ul>
                      </li>
                      <li><a class="close-link"><i class="fa fa-close"></i></a>
                      </li>
                </ul>
                <div class="clearfix"></div>
            </div>
            <div class="x_content">
                <div class="form-group ">
                    <div class='col-sm-6'><input id='material_label' placeholder='物料名称'><input type='hidden' id='material_id'></div>
                    <div class='col-sm-2'><input id='material_qty' placeholder='数量'></div>                    
                    <span class="input-group-btn"><button type="button" class="btn btn-success add-material">
                    <span class='glyphicon glyphicon-arrow-down'></span></button></span>                
                </div>
                 
                <div class='table-responsive' style='padding:10px;'>
                    <table class="table table-condensed table-bordered table-hover">
                        <thead>
                            <th style='width:5%;'>删除</th>
                            <th style='width:45%;'>物料名称</th>
                            <th style='width:10%;'>数量</th>
                            <th style='width:45%;'>备注</th>
                        </thead>
                        <tbody>
                            {% for f in mset %}
                                <tr>        
                                    <td>{{f.DELETE}}{{f.id}}{{f.status}}</td>                                    
                                    <td>{{f.material}}<span  class='material'>{{f.instance.material.name}}</span></td> 
                                    <td>{{f.qty}}</td> 
                                    <td>{{f.remark}}</td> 
                                </tr>
                            {% endfor %}             
                        </tbody>
                </table></div>                 
            </div>
        </div>  

        <div> <a class="btn btn-primary btn-save" type="submit" style="float:right;">{% trans '保存' %}</a></div>

    {% endblock %}
   

