{% load i18n %}

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

        $('#dt_fr').val('{{date_from}}');
        $('#dt_to').val('{{date_to}}');

        $('.btn-qry').click(function(){
            var date_from,date_to;           
            date_fr=$('#dt_fr').val();
            date_to=$('#dt_to').val();
            var params=[];          
            if(date_fr!=''){params.push('date_from='+date_fr)};
            if(date_to!=''){params.push('date_to='+date_to)};

            if(params.length==0){               
                toastr.error("{% trans '请先输入查询条件！'%}", "{% trans '操作错误：'%}")
            }else{
                $.RefreshContent('{{request.path}}?'+params.join('&'));
            }
        });      



        $('#tb').on("click","td a.btn-close-order",function(event){  

		
            event.preventDefault();  
            order_id=$(this).attr('order_id');
            order_code=$(this).attr('code');
			$("#modal_permit").modal('show')
			return;
            $.OpenCfmDlg("{% trans '关闭订单' %}","确定订单"+order_code+"已完成，要关闭此订单吗？");            
        });

        $('#btn_save').click(function(){ 
            //$('#confirm-modal').modal('hide'); 
			var state = $("input[name='state']:checked").val();
			var voucher_code = $("#inputSaleOrderNumber").val();
			var remark = $("#inputRemark").val();
			console.log(state,voucher_code,remark);
			if(!state){
				alert('请选择审核状态');
				return;
			}
			//return;
            $.ajax({
                type:'POST',     
                url:'/company/close/agent/order/'+order_id+'/',
				data:{
					state,
					voucher_code,
					remark
				},
                success:function(data){
                    if(data.cnt>0){
						$('#modal_permit').modal('hide'); 	
                        toastr.success("{% trans '订单' %}"+order_code+"{% trans '已关闭' %}", "{% trans '关闭订单完成：' %}",
                            {timeOut: 2000, positionClass: "toast-center-center"} ); 
                        var date_from,date_to;           
                        date_fr=$('#dt_fr').val();
                        date_to=$('#dt_to').val();
                        var params=[];          
                        if(date_fr!=''){params.push('date_from='+date_fr)};
                        if(date_to!=''){params.push('date_to='+date_to)};
                        if(params.length==0){                           
                            toastr.error("{% trans '请先输入查询条件！'%}", "{% trans '操作错误：'%}")
                        }else{
                            $.RefreshContent('{{request.path}}?'+params.join('&'));
                        }    					
                    }else{
                        alert('订单关闭失败');
                       
                    };                   
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert('连接服务器失败');
                    
                }                
            });      
        });


        var table=$('#tb').DataTable({
            dom: 'Bfrtip',
            autoWidth: true,
            buttons: [{extend:'excelHtml5',text: '导出到Excel'}],   
            language:lng,          
            data:{{data|safe}}
        });     
        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();
                }
            });
        });          
       
    });
    
    function viewDetail(id) {
        $('#modal_review').modal('show')
        $.ajax({
            type: 'GET',
            processData: false,
            contentType: 'application/json; charset=utf-8',
            url: "/company/list/agent/order/"+id+"/",
            success: function (html) { 
                $("#review_body").html(html);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert('连接服务器失败');
                return;
            }
        });
    }

</script>



<ol class="breadcrumb">
        <li>{% trans '经销商管理' %}</li>
        <li>{% trans '订单列表' %}</li>     
</ol>




<div class='col-sm-12 form-inline' style='margin-bottom:20px;'>
    <div class="col-sm-3 input-group">
        <span class="input-group-addon">{% trans "开始日期"%}</span>
        <input class="datepicker form-control" id="dt_fr" />
    </div>
    <div class="col-sm-3 input-group">
        <span class="input-group-addon">{% trans "结束日期"%}</span>
        <input class="datepicker form-control" id="dt_to" />
    </div>
    <div style="float:right;">
    <span class='input-group-btn'>
        <a type="button" class='btn btn-primary btn-qry'>
            <span class='glyphicon glyphicon-search' style='padding-right:5px;'></span>{% trans '查找' %}
        </a>
    </span>    
    </div>
</div>





<div class="col-md-12 col-sm-12 col-xs-12">
    <div class="x_panel">
        <div  class="table-responsive">

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



<div class="modal fade" id="modal_review" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content"> 
            
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button> 
            </div>
            <div class="modal-body" id="review_body"> 
				
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> 
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->


<div class="modal fade" id="modal_permit" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content"> 
            
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button> 
            </div>
            <div class="modal-body" id="permit_body"> 
			
				<label class="control-label"><b>审核状态</b></label>
				<div>
					<span class="control-label"><input type="radio" name="state" class="" value="1" /> 通过</span>
					<span class="control-label"><input type="radio" name="state" class="" value="2" /> 驳回</span>
				</div>

                <label for="inputSaleOrderNumber" class="control-label" style="margin-top:5px"><b>销售单号</b></label>
                <input id="inputSaleOrderNumber" name="name" type="text" class="form-control"
                                   placeholder="输入销售单号" maxlength="50"/>
								   
                <label for="inputRemark" class="control-label" style="margin-top:5px"><b>备注</b></label>
                <input id="inputRemark" name="name" type="text" class="form-control"
                                   placeholder="输入备注" maxlength="50"/>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> 
                <button type="button" class="btn btn-primary" id="btn_save">保存</button> 
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
