{% load i18n %}




    <ol class="breadcrumb">
        <li>{% trans "存货管理" %}</li>
        <li>{% trans "发货TXT上传" %}</li>
    </ol>


<script type="text/javascript">
    $(document).ready(function(){

        $('#obj_form form .form-group').css('min-height','35px');


        $('#product_label').autocomplete({
            source: {{product|safe}},
            select: function (event, ui) {
                $("#product_id").val(ui.item.value);
                $("#product_label").val(ui.item.label);
                return false;
            }
        });

        
       
        $('#company_to_label').autocomplete({
            source: {{company_to|safe}},
            select: function (event, ui) {
                $("#company_to_id").val(ui.item.value);
                $("#company_to_label").val(ui.item.label);
                return false;
            }
        });
       
        $('#store_fr_label').autocomplete({
            source: {{store_fr|safe}},
            select: function (event, ui) {
                $("#store_fr_id").val(ui.item.value);
                $("#store_fr_label").val(ui.item.label);
                return false;
            }
        });      






    $('input[name="info"]').change(function(){
        info=$('input[name="info"]:checked').attr('value');  
        if(info=='custom'){        
            $('#fahuo_info').show(400);
        }else{
            $('#fahuo_info').hide(400);
        }
    });
    $('#xFile').change(function(){
        $('#info table').html('');
        $('#info .info').html('');
        $('#errinfo_null_rec').html('');
        $('#errinfo_delivered_rec').html('');
        $('#errinfo_different_rec').html('');
        var xhr= new XMLHttpRequest();
        var file = document.getElementById('xFile').files;
        var formDat; 
        var cnt=0;
        formData = new FormData();
        for(i=0;i<file.length;i++){  
            var fn=file[i].name.substr(0,2).toUpperCase();
            formData.append("files", file[i]);
            cnt=cnt+1;
        }
        if(cnt==0){
            //bootbox.alert("{% trans '没有可上传的文件!' %}", function() {});
            toastr.error(
                "{% trans '没有可上传的文件!' %}",
                "操作错误：",
                {timeOut: 800, positionClass: "toast-center-center"}
            );
            return;
        };

        switch($('#vtype_id').val()){
            case 2:                
                if ($.isEmpty($("#company_to_id").val())||$.isEmpty($("#product_id").val())||$.isEmpty($("#store_fr_id").val())||$.isEmpty($("#column").val())){
                    //bootbox.alert("{% trans '经销商，产品，仓库，标签号码所在列不能为空!' %}", function() {});
                    toastr.error(
                        "经销商，产品，仓库，标签号码所在列不能为空!",
                        "操作错误：",
                        {timeOut: 800, positionClass: "toast-center-center"}
                    );
                    return;
                }
                break;        
            case 4:
                if ($.isEmpty($("#product_id").val())||$.isEmpty($("#store_fr_id").val())||$.isEmpty($("#column").val())){
                    //bootbox.alert("{% trans '经销商，产品，仓库，标签号码所在列不能为空!' %}", function() {});
                    toastr.error(
                        "产品，仓库，标签号码所在列不能为空!",
                        "操作错误：",
                        {timeOut: 800, positionClass: "toast-center-center"}
                    );
                    return;
                }
                break;        
        
        };
        var agt_id=$("#company_to_id").val();
        if (agt_id==''){
            agt_id='0';
        };
        formData.append("code",$("#code").val());
        formData.append("customer_id",agt_id);
        formData.append("product_id",$("#product_id").val());
        formData.append("store_fr_id",$("#store_fr_id").val());
        formData.append("voucher_id",$("#vtype_id").val());
        formData.append("column",$("#column").val());
        formData.append("force",$("#force").val());



        $('#msg').remove();
        $('.btn-upload').addClass('disabled');
        $('#ajax_loading2').show();
        var si='';
        xhr.open('POST','/inventory/upload/inv/txt/');
        xhr.setRequestHeader("X-CSRFToken", '{{ csrf_token }}');
        xhr.send(formData);
        xhr.onreadystatechange=(function(_xhr) {
            return function() {
                if (_xhr.readyState == 4){ 
                    if(_xhr.status == 200) {
                        $('#ajax_loading2').hide();
                        $('.btn-upload').removeClass('disabled');
                        var d = eval('('+_xhr.responseText+')');
                        if(d.e!=''){    
                            //bootbox.alert(d.e, function() {});
                            toastr.error(
                                d.e,
                                "上传失败：",
                                {timeOut: 800, positionClass: "toast-center-center"}
                            );

                            return;
                        };

                        toastr.success(
                            "成功上传发货"+d.cnt+"件。",
                            "上传成功：",
                            {timeOut: 800, positionClass: "toast-center-center"}
                        );




                    }else{                        
                        $('.btn-upload').removeClass('disabled');
                        $('#ajax_loading2').hide();
                        //bootbox.alert("{% trans '通讯错误!' %}", function() {});

                        toastr.error(
                            "通讯错误!",
                            "上传失败：",
                            {timeOut: 800, positionClass: "toast-center-center"}
                        );
                    }
                }
            }
        })(xhr);
        xhr.timeout = 300000;
        xhr.ontimeout = function(event){            　　
            $('#ajax_loading2').hide();
            //bootbox.alert("{% trans '请求超时！' %}", function() {});
            toastr.error(
                    "请求超时",
                    "操作失败：",
                    {timeOut: 800, positionClass: "toast-center-center"}
            );

        }           
    });       

});
</script>


<div id="obj_form"  class=''>   
    <form>

    <div class="form-group ">
        <label class="col-sm-1 control-label ">{% trans '发货单号:' %} </label>
        <div class="col-sm-7">
            <input id='code' name='code' class='form-control'/>
        </div>
        <label class="col-sm-1 control-label ">{% trans '单据类型:' %} </label>
        <div class="col-sm-3">
            <select id="vtype_id" class='form-control'>
                <option value=2 seleted="selected">销售出库</option>
                <option value=4>销售退库</option>            
            </select>
        </div>
    </div>
    </div>

    <div class="form-group ">
            <label class="col-sm-1 control-label ">{% trans '客户名称:' %} </label>
            <div class="col-sm-11">
                <input id='company_to_id' type='hidden' name='company_to_id' /><input id='company_to_label' class='form-control'/>
            </div>
    </div>

    <div class="form-group ">
        <label class="col-sm-1 control-label ">{% trans '产品名称:' %} </label>
        <div class="col-sm-11">
            <input id='product_id' type='hidden' name='product_id' /><input id='product_label'  class='form-control'/>        
        </div>
    </div>    
    
    
    <div class="form-group ">
            <label class="col-sm-1 control-label ">{% trans '仓库名称:' %} </label>
            <div class="col-sm-11">
                <input id='store_fr_id' type='hidden' name='store_fr_id'/><input  id='store_fr_label'  class='form-control'/>
            </div>
    </div> 
    <div class="form-group ">
            <label class="col-sm-1 control-label ">{% trans '重复发货:' %} </label>
            <div class="col-sm-11">
                <select  id='force' name='force' class='form-control'>
                    <option value='1' selected="selected">允许</option>
                    <option value='0'>不允许</option>
                </select>
            </div>
    </div> 

    <div class="form-group">
        <label class="col-sm-1 control-label">{% trans "标签所在列:" %}</label>
        <div class="col-sm-1"><input id="column" value="1"  class="form-control"/></div>
        <div class="col-sm-10">
            <label class="btn btn-primary btn-upload" for="xFile">{% trans "上传文件" %}</label>
        </div>
    </div>
    <input type="file" id="xFile" accept="text/plain"  multiple="multiple" style="position:absolute;clip:rect(0 0 0 0);">

    </form>   

</div>

