{% load i18n %}

<script src="/static/js/CellEdit-master/js/dataTables.cellEdit_cn.js"></script>


<style>

    .inventory-tt .g{
        color:green;
        margin-right:10px;
    }
    tbody tr.row_selected{
        color:black;
        background: -moz-linear-gradient(center top , #ff8e1f, #ff7217) repeat scroll 0 0 rgba(0, 0, 0, 0);
        background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#ff8e1f), to(#ff7217));
    }
</style>





<script type="text/javascript">    
    $(document).ready(function(){
        var changed_data={};
        var columns={{clmns|safe}};           
        
        var table=$('#tb-dlg').DataTable({
            dom: 'Bfrtip',
            autoWidth: false,
            buttons: [{extend:'excelHtml5',text:'导出到Excel'}],           
            language:lng,            
            destroy:true,
            columns:{{clmns|safe}},
            data:{{data|safe}},
            //columnDefs: [{ "visible":false, "targets": [0] }],
        });      




        /*
        //单击，选择行
        $("#tb-dlg tbody tr").click(function (e) {
            $('#tb-dlg tbody tr').removeClass('row_selected'); 
            $(this).addClass('row_selected');
            $('.btn-details-edit, .btn-details-delete').removeClass('disabled');            
        });
        //编辑 替换明细 
        $(".btn-details-edit").click(function () { 
            var d=table.rows('.row_selected').data()[0];
            $('#row-id').val(d.DT_RowId);
            $('#code0').val(d.code0);
            $('#code').val(d.code);
            $('#remark').val(d.remark); 
            $("#bg-modal").modal('hide');
            $('#dlg-edit').modal("show");
        });
        //删除
        $(".btn-details-delete").click(function () { 
                if(confirm("确定要将这条替换记录删除吗?")){
                    var url='/inventory/label/replace/delete/'+table.rows('.row_selected').data()[0].DT_RowId+'/?obj=details'; 
                    $.ajax({
                        type:'GET',
                        processData: false,               
                        url:url,
                        success:function(data){                    
                            if(data.e==''){
                                toastr.success('删除完成!', '操作成功：',
                                    {timeOut: 800, positionClass: "toast-center-center"}
                                );
                            }else{
                                toastr.error(data.e, "操作错误：",
                                    {timeOut: 800, positionClass: "toast-center-center"}
                                );
                            };
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {                                      
                            $('#dlg-edit').modal("hide");                    
                            alert('连接服务器失败');                        
                        }
                    });   
                    $("#bg-modal").modal('hide');
                };                      
        });
        */     


        //单击，选择行
        $("#tb-dlg tbody")
            .off('click','tr')
            .on('click','tr',function (e) {
                $('#tb-dlg tbody tr').removeClass('row_selected'); 
                $(this).addClass('row_selected');
                $('.btn-details-edit, .btn-details-delete').removeClass('disabled');            
            });
        //编辑 替换明细 
        $(".btn-details-edit")
            .off('click')
            .on('click',function () {                 
                var d=table.rows($('#tb-dlg tbody tr.row_selected')).data()[0];
                $('#row-id').val(d.DT_RowId);
                $('#code0').val(d.code0);
                $('#code').val(d.code);
                $('#remark').val(d.remark); 
                $("#bg-modal").modal('hide');
                $('#dlg-edit').modal("show");
            });
        //删除 
        $(".btn-details-delete")
            .off('click')
            .on('click',function () { 
                if(confirm("确定要将这条替换记录删除吗?")){
                    //var url='/inventory/label/replace/delete/'+table.rows('.row_selected').data()[0].DT_RowId+'/?obj=details'; 
                    var url='/inventory/label/replace/delete/'+table.rows($('#tb-dlg tbody tr.row_selected')).data()[0].DT_RowId+'/?obj=details';
                    $.ajax({
                        type:'GET',
                        processData: false,               
                        url:url,
                        success:function(data){                    
                            if(data.e==''){
                                toastr.success('删除完成!', '操作成功：',
                                    {timeOut: 800, positionClass: "toast-center-center"}
                                );
                            }else{
                                toastr.error(data.e, "操作错误：",
                                    {timeOut: 800, positionClass: "toast-center-center"}
                                );
                            };
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {                                      
                            $('#dlg-edit').modal("hide");                    
                            alert('连接服务器失败');                        
                        }
                    });   
                    $("#bg-modal").modal('hide');
                };                      
            });



        
        table.columns().every( function () {                        
            var that = this;             
            $('input', this.footer() ).on( 'keyup change clear', function () {
                if ( that.search() !== this.value ) {
                    that.search( this.value ).draw();
                }
           });
        });      

        $('.btn-check').click(function(){
                var msg = "您确定要将这些替换记录生效吗？\n\n请确认！";                 
                if (confirm(msg)==true){
                    $.ajax({
                        type:'GET',
                        processData: false,
                        contentType: 'application/json; charset=utf-8',
                        url:'/inventory/label/replace/check/{{batch.id}}/?',
                        success:function(data){
                                if (data.e==''){
                                    var date_from,date_to;           
                                    date_fr=$('#dt_fr').val();
                                    date_to=$('#dt_to').val();
                                    var params=[];
                                    if(date_fr!=''){params.push('dt_fr='+date_fr)};
                                    if(date_to!=''){params.push('dt_to='+date_to)};
                                    $("#bg-modal").modal('hide');                
                                    $.RefreshContent('/inventory/label/replace/list/?'+params.join('&'));   
                                }else{
                                    toastr.error(
                                        data.e,
                                        "错误：",
                                        {timeOut: 800, positionClass: "toast-center-center"}
                                    );                        
                                }      
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                                alert('连接服务器失败');
                                return;
                        }
                    }); 
                
                };        
                         
        });    


    });
</script>

<div class='inventory-tt'>
    <span>替换来源:<span class='g'>{{batch.src}}</span></span>  
    <span>替换记录总数:<span class='g'>{{batch.qty}}</span></span>   
    {% if  batch.checked_by %}
        <span>审核人:<span class='g'>{{batch.checked_by}}</span></span>
        <span>时间:<span class='g'>{{batch.checked_tm}}</span></span>
    {% else %}
        <button type="button" class="btn btn-primary btn-sm btn-check" >审核通过</button>    
    {% endif %}

    <span class='pull-right'>
        <button class='btn btn-primary btn-details-edit disabled'>修改</button> 
        <button class='btn btn-danger btn-details-delete disabled'>删除</button> 
    </span>

</div>

<div class="clearfix"></div>


<div id="inventory-list" class="table-responsive">   

    <table id="tb-dlg" class="table table-striped table-bordered dt-responsive" width="100%">
        <thead>
            {% for c in clmns %}
                <th>{{c}}</th>
            {% endfor %}            
        </thead><tbody></tbody>
        <tfoot>
            {% for c in clmns %}
                <th><input tyext='text' placeholder='{{c.title}}' /></th>
            {% endfor %}            
        </tfoot>
    
    </table>       
</div>
