{% 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: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/product/',
                    dataType: "json",
                    data: {
                        term: $("#product_label").val() ,//搜索栏里的内容
                        company_id:{{request.user.company_id}} //额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $("#product_id").val(ui.item.value);
                $("#product_label").val(ui.item.label);
                return false;
            }
        });        
        $('#company_to_label').autocomplete({            
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/agent/?query_all=1',
                    dataType: "json",
                    data: {
                        term: $("#product_label").val() ,//搜索栏里的内容
                        company_id:{{request.user.company_id}} //额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $("#pcompany_to_id").val(ui.item.value);
                $("#company_to_label").val(ui.item.label);
                return false;
            }
        });

        $('#company_to_label').autocomplete({            
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/agent/?query_all=1',
                    dataType: "json",
                    data: {
                        term: $("#company_to_label").val() ,//搜索栏里的内容
                        company_id:{{request.user.company_id}} //额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            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: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/store/',
                    dataType: "json",
                    data: {
                        term: $("#store_fr_label").val() ,//搜索栏里的内容
                        company_id:{{request.user.company_id}} //额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $("#store_fr_id").val(ui.item.value);
                $("#store_fr_label").val(ui.item.label);
                return false;
            }
        });

        $('#store_to_label').autocomplete({            
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/store/',
                    dataType: "json",
                    data: {
                        term: $("#store_to_label").val() ,//搜索栏里的内容
                        company_id:{{request.user.company_id}} //额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $("#store_to_id").val(ui.item.value);
                $("#store_to_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;     
            case '3':
                if($.isEmpty($("#product_id").val())||$.isEmpty($("#store_fr_id").val())||$.isEmpty($("#store_to_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';
        };
        
        
        var store_fr_id=$('#store_fr_id').val();
        var store_to_id=$('#store_to_id').val();        
        if(store_fr_id==''){store_fr_id='0'};
        if(store_to_id==''){store_to_id='0'};        

        formData.append("code",$("#code").val());
        formData.append("customer_id",agt_id);
        formData.append("product_id",$("#product_id").val());        
        formData.append("voucher_id",$("#vtype_id").val());
        formData.append("column",$("#column").val());
        formData.append("force",$("#force").val());
        formData.append("store_fr_id",store_fr_id);
        formData.append("store_to_id",store_to_id);       


        //2023-08-09
        var remark=$('#remark').val();   
        formData.append("remark",remark);


        $('#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-5">
            <input id='code' name='code' class='form-control'/>
        </div>
        <label class="col-sm-1 control-label ">{% trans '单据类型:' %} </label>
        <div class="col-sm-5">
            <select id="vtype_id" class='form-control'>
                <option value=2 seleted="selected">销售出库</option>
                <option value=4>销售退库</option>   
                <option value=3>成品转库</option>
                <option value=1>成品入库</option>
            </select>
        </div>
    </div>
    

    <div class="form-group ">
        <label class="col-sm-1 control-label ">{% trans '产品名称:' %} </label>
        <div class="col-sm-5">
            <input id='product_id' type='hidden' name='product_id' /><input id='product_label'  class='form-control'/>        
        </div>
        <label class="col-sm-1 control-label ">{% trans '客户名称:' %} </label>
        <div class="col-sm-5">
            <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-5">
            <input id='store_fr_id' type='hidden' name='store_fr_id'/><input id='store_fr_label' class='form-control'/>
        </div>

        <label class="col-sm-1 control-label ">{% trans '目地仓库:' %} </label>
        <div class="col-sm-5"> 
            <input id='store_to_id' type='hidden' name='store_to_id'/><input id='store_to_label' class='form-control'/>
        </div>
    </div> 
    <div class="form-group ">
            <label class="col-sm-1 control-label ">{% trans '重复发货:' %} </label>
            <div class="col-sm-5">
                <select  id='force' name='force' class='form-control'>
                    <option value='1' selected="selected">允许</option>
                    <option value='0'>不允许</option>
                </select>
            </div>
            
            <!--2023-08-09     -->
            <label class="col-sm-1 control-label ">{% trans '备注说明:' %} </label>
            <div class="col-sm-5">
                <input id='remark' name='remark' class='form-control'/>
            </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>

