{% load i18n %}

<script type="text/javascript">
    $(document).ready(function(){
        $('.datepicker').datepicker({dateFormat: 'yy-mm-dd'});
        //$('.datepicker').datepicker('setDate',new Date());
        setDatePickerZh();

        {% if is_saved %}
                $('.btn').show();
                toastr.success("{% trans '数据保存成功!' %}",
                    "{% trans '消息提示' %}",{timeOut: 800, positionClass: "toast-center-center"}
                );
        {% endif %}


        
        $('#id_recipientdetails_set-__prefix__-company').val('{{request.user.company_id}}');
        $('#id_recipientdetails_set-__prefix__-ancestor_id').val('{{request.user.company.ancestor_id}}');

        
        $('.btn-save').click(function(){
            $('.btn').hide();
            $.SubmitForm('#recipient_form','{{request.path}}');
        });
        $('.btn-add').click(function(){
            var receiver_id=$('#id_receiver').val();
            if($.isEmpty(receiver_id)){
                toastr.error("{% trans '请先输入领用人!' %}",
                    "{% trans '操作错误：' %}",{timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            };
            var code=$('#lbl_code').val();
            if (code.length<11){
                toastr.error("{% trans '请先输入标签号码!' %}",
                    "{% trans '操作错误：' %}",{timeOut: 800, positionClass: "toast-center-center"}
                );
                return;
            };
            var bExists=false;
            $("#tb tbody tr .label_code").each(function(){
                if($(this).text()==code){
                    bExists=true;
                    return false;
                }
            });
            if (bExists){
                toastr.error("{% trans '此标签号码在明细表中已经存在!' %}",
                        "{% trans '操作错误：' %}",{timeOut: 800, positionClass: "toast-center-center"}
                    );
                 return ;
            };

            $.ajax({
                type:'GET',
                url:'/obj/agent/label/?code='+code,
                success: function(data) {
                    $('#id_recipientdetails_set-__prefix__-label_code').val(code);
                    $('#id_recipientdetails_set-__prefix__ .label_code').text(code);
                    $('#id_recipientdetails_set-__prefix__-product').val(data.product_id);
                    $('#id_recipientdetails_set-__prefix__-label_pk_id').val(data.label_pk_id);
                    $('#id_recipientdetails_set-__prefix__-label_id').val(data.label_id);
                    $('#id_recipientdetails_set-__prefix__-label_pkg').val(data.label_pkg_id);
                    $('#id_recipientdetails_set-__prefix__ .label_pkg').text(data.label_pkg_name);
                    $('#id_recipientdetails_set-__prefix__ .product_name').text(data.product_name);
                    var count = $('#tb tbody tr').length;
                    var tmplMarkup = '<tr>'+$('#id_recipientdetails_set-__prefix__').html()+'</tr>';
                    var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);
                    $('#tb tbody').append(compiledTmpl);
                    $('#id_recipientdetails_set-TOTAL_FORMS').attr('value', 1+count);
                }
            });
        });
    });
</script>

<ol class="breadcrumb">
        <li>{% trans '公司管理' %}</li>
        {% if form.instance.id %}
            <li>{% trans '修改订单' %}</li>
        {% else %}
            <li>{% trans '新建订单' %}</li>
        {% endif %}
        {% if is_saved %}
            <a type="button" class='btn btn-obj-add l-a create-btn pull-right'  href="{{request.path}}">
                <span class='glyphicon glyphicon-plus' style='padding-right:5px;'></span>
                {% trans '新建' %}
            </a>
        {% endif %}
</ol>

<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="x_panel">
<br />

<div id="order_form" class="container" >
    <div class="edit_panel"><form role="form" method='post'  class="form-horizontal">
    {% csrf_token %}
        {% if messages %}
            {% for message in messages %}
                <div class="alert {% if message.tags %} alert-{{ message.tags }}{% endif %}">{{ message|safe }}</div>
            {% endfor %}
        {% endif %}
        {% if form.errors %}
            <div class="alert alert-error">
                <h4>{% trans '数据输入有错误,请先改正以下错误:' %}</h4>
                <ul>
                    {% for field in form %}
                        {% if field.errors %}
                            {% for error in field.errors %}
                                <li class="error">{{ field.label}}: {{ error|escape }}</li>
                            {% endfor %}
                        {% endif %}
                    {% endfor %}
                </ul>
                {% if form.non_field_errors %}
                    {{ form.non_field_errors }}
                {% endif %}
            </div>
        {% endif %}


        <div class="form-group ">
            {{form.vendor}}
            <label class="col-sm-2 control-label">{% trans '供货单位:' %}</label>
            <div class="col-sm-10">{{ form.instance.vendor.name }} </div>
        </div>


        <div class="form-group ">
            {{ form.id }}
            {{ form.company }}
            {{ form.ancestor_id }}
            {{ form.user }}
            <label class="col-sm-2 control-label">{% trans '订单编号:' %}</label>
            <div class="col-sm-10">{{ form.code }} </div>
        </div>
        <div class="form-group ">
            <label class="col-sm-2 control-label">{% trans '期望到货日期:' %}</label>            
            <div class="col-sm-10">{{form.dt_expect}} </div>
        </div>
        <div class="form-group ">
            <label class="col-sm-2 control-label">{% trans '备注与说明:' %}</label>
            <div class="col-sm-10">{{ form.remark }} </div>
        </div>



        <table style="display:none;">
             <tr id="id_recipientdetails_set-__prefix__" >
            {{ formset.empty_form.id }}
            <td>{{ formset.empty_form.DELETE }}</td>
            <td>{{ formset.empty_form.label_code }}<span class="label_code"></span></td>
            <td>{{ formset.empty_form.label_pkg }}<span class="label_pkg"></span></td>
            <td>
                {{ formset.empty_form.label_id }}
                {{ formset.empty_form.label_pk_id }}
                {{ formset.empty_form.receiver }}
                {{ formset.empty_form.product }}
                {{ formset.empty_form.company }}
                {{ formset.empty_form.ancestor_id }}
                 <span class="product_name"></span>
            </td>
            </tr>
        </table>


        <div class="col-sm-2">
            <div class="input-group">
                <input type="text" id='lbl_code' class="form-control" placeholder="请输入领用的标签号码">
                <span class="input-group-btn">
                    <button type="button" class="btn btn-success btn-add">添加</button>
                </span>
            </div>
        </div>

        <div class="col-sm-1 col-md-offset-9">
            <span class="pull-right"><button type="button" class="btn btn-primary btn-save">保存</button></span>
        </div>


        <div class="col-md-12 col-sm-12 col-xs-12">
        <div  class='table-responsive container' >
            <table  id='tb' class='table table-bordered table-condensed table-hover'>
            <thead>
                <tr>
                    <th style="width:7px">删除</th>
                    <th style="width:40px">标签号码</th>
                    <th style="width:20px">标签类型</th>
                    <th style="width:20px">产品名称</th>
                </tr>
            </thead>
            <tbody>
                    {% for f in formset %}
                    <tr>{{f.id}}
                        <td> {{f.DELETE}}</td>
                        <td>{{f.label_code}}<span class="label_code"> {{f.label_code.value}} </span></td>
                        <td>{{f.label_pkg}}<span class="label_pkg">
                            {% ifequal f.instance.label_pkg 1 %}
                                瓶标
                            {% endifequal %}
                            {% ifequal f.instance.label_pkg 3 %}
                                箱标
                            {% endifequal %}
                        </span></td>
                        <td>
                            {{ f.label_id }}
                            {{ f.label_pk_id }}
                            {{ f.receiver }}
                            {{ f.product }}
                            {{ f.company }}
                            {{ f.ancestor_id }}
                            <span class="product_name">{{f.instance.product.name}}</span>
                        </td>
                    </tr>
                    {% endfor %}
            </tbody>
            </table>
            {{ formset.management_form }}
        </div>
        </div>

    </form>
    </div>
</div>

</div>
</div>
</div>
