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




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

        {% if form.instance.id %}
            $('#agent_label').val("{{form.instance.agent.name}}");    
            $('#label_scheme').val("{{form.instance.scheme.name}}"); 
            $('#salesman_label').val("{{form.instance.salesman.username}}");
            $('#saldiv_label').val("{{form.instance.saldiv.name}}");
            $('#subdiv_label').val("{{form.instance.subdiv.name}}");     


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

        {% endif %}



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


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





        $('#saldiv_label').autocomplete({ 
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/saldiv/',
                    dataType: "json",
                    data: {
                        term:   $("#saldiv_label").val() ,//搜索栏里的内容
                        company_id:  $('#id_company').val() ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },  
            select: function (event, ui) {
                $("#id_saldiv").val(ui.item.value);
                $("#saldiv_label").val(ui.item.label);
                return false;
            }
        });
        $('#subdiv_label').autocomplete({ 
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/subdiv/',
                    dataType: "json",
                    data: {
                        term:   $("#subdiv_label").val() ,//搜索栏里的内容
                        company_id:  $('#id_company').val() ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },  
            select: function (event, ui) {
                $("#id_subdiv").val(ui.item.value);
                $("#subdiv_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:  $('#id_company').val() ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },  
            select: function (event, ui) {
                $("#id_agent").val(ui.item.value);
                $("#agent_label").val(ui.item.label);
                return false;
            }
        });

        $('#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;
            }
        });

        $('#salesman_label').autocomplete({ 
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/salesman/',
                    dataType: "json",
                    data: {
                        term:   $("#salesman_label").val() ,//搜索栏里的内容
                        company_id:  $('#id_company').val() ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },  
            select: function (event, ui) {
                $("#id_salesman").val(ui.item.value);
                $("#salesman_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();

            $('#prj-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_requisitionproducts_set-';   
                    $(prefix+'__prefix__').find('.product').text(name); 
                    var count = $('#prj-product tbody tr').length; 
                    var tmplMarkup = '<tr class="p">'+$(prefix+'__prefix__').html()+'</tr>';    
                    var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);     
                    $('#prj-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;         
            $('#prj-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_requisitionmaterials_set-';   
                    $(prefix+'__prefix__').find('.material').text(name); 
                    var count = $('#prj-material tbody tr').length; 
                    var tmplMarkup = '<tr class="p">'+$(prefix+'__prefix__').html()+'</tr>';    
                    var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);     
                    $('#prj-material tbody').append(compiledTmpl); 
                    $(prefix+count+'-material').val(id);   
                    $(prefix+count+'-qty').val(qty);   
                    $(prefix+'TOTAL_FORMS').attr('value', (count+1));   
                }
            }
        });   

    $('.btn-save').click(function(){
        $.SubmitForm('#obj_form form','{{request.path}}');
    });  
{% 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"><input id='label_scheme' placeholder='方案名称'  /></div>
    
        <label class="col-sm-1 control-label ">{% trans '备注说明' %} </label>
        <div class="col-sm-5">{{ form.remark }}</div>
    </div>      
    <div class="form-group ">
        <label class="col-sm-1 control-label ">{% trans '经销商' %} </label>
        <div class="col-sm-5"><input id='agent_label'/></div>
    
        <label class="col-sm-1 control-label ">{% trans '业务员' %} </label>
        <div class="col-sm-5"><input id='salesman_label'/></div>
    </div>     



    

   

{% endblock %}


 {% block fmset %}  

        <table  style="display:none;">
            <tr id="id_requisitionproducts_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_requisitionmaterials_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='prj-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='prj-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 %}
   

