{% load i18n %}

<style> 

</style>



<script type="text/javascript">
    var status_dict = {
        0: ['冻结','color:red;text-decoration:underline','disabled'],
        1: ['正常','color:#07c160;text-decoration:underline;pointer-events:none','disabled'],
    }
    $(document).ready(function(){    
        var page_length=10;

        var table=$('#tb').DataTable({
            dom: 'Bfrtip',
            autoWidth: true,
            pageLength: page_length,
            buttons: [{extend:'excelHtml5',text: '导出到Excel'}],   
            language:lng,	
			{% if order %}
            order: {{order|safe}},
			{% endif %}
            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();
                }
            });
        });    


        $(".btn-obj-add").click(function () { 
            event.preventDefault();
            $.RefreshContent($(this).attr('href'));        
        })

        $('#tb').on("click","td a",function(event){
            event.preventDefault(); 
            window.click_row = $(this).closest('tr');
			if($(this).hasClass('btn-dlg') || $(this).hasClass('btn-only-dlg') ){
				$.OpenDlg($(this).attr('href'),'信息弹窗',1000,700);
				return;
			}
            if($(this).hasClass('btn-permit') || $(this).hasClass('btn-reject')){
                var id = $(this).data('id')
                var status = $(this).data('status')
                var remark =  $(this).data('remark')
                review(id, status, remark)
            }
        });     

       
        $('#tb tbody').on('click','img',function(){            
            var src=$(this).attr('src');
            var tt=$(this).closest('tr').find('a').text();
            if(tt==''){                
                tt=$(this).attr('tt');
            };
            var i="<div style='text-align:center;'><img src='"+src+"' style='height:400px;'></img></div>";
            $.OpenCfmDlg(tt,i);       
        });

        $('.datepicker').datepicker({dateFormat: 'yy-mm-dd'});
        $('.datepicker').datepicker('setDate',new Date());
        setDatePickerZh();

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

        $('.btn-qry').click(function(){
            var dt_fr,dt_to;
            dt_fr=$('#dt_fr').val();
            dt_to=$('#dt_to').val();
            var params=[];
            if(dt_fr!=''){params.push('dt_fr='+dt_fr)};
            if(dt_to!=''){params.push('dt_to='+dt_to)};
            var checkList = [];  
            $('input[name="status"]:checked').each(function(){  
                checkList.push($(this).val());//向数组中添加元素  
            }); 
            params.push('status='+checkList.join(','))

            if(params.length==0){
                bootbox.alert("{% trans '请先输入查询条件！'%}");
            }else{
                $.RefreshContent('{{request.path}}?'+params.join('&'));
            }
        });

        
        //2020-11-07
        $("[data-toggle='popover']").popover({
            html:true,
            placement:'bottom'
        });

		$('.btn-photo').click(function(){            
            var src=$(this).data('src'); 
            var i="<div style='text-align:center;'><img src='"+src+"' style='height:400px;'></img></div>";
            $.OpenCfmDlg('图片',i);       
        });

        function review(id, status, remark){
            $.ajax({
                url: '/sales/consumer/winner_active/',
                data:{
                    id,
                    status,
                    remark
                },
                method: 'POST',
                success:function(res){
                    if(res.e === ''){
                        $(".alert-success").fadeIn();
                        changeTableData(status)
                        $("#bg-modal").modal('hide');
                    }
                    else{
                        alert(res.e);
                    }
                },
                error:function(){
                    alert('网络超时！请稍后再试！')
                }
            })
        }
    });

    function deleteItem(id){
        var r = confirm('您确定要删除吗？');
        if(r){
            var dt_fr = $('#dt_fr').val();
            var dt_to = $('#dt_to').val();
            var durl = 'sales/delete/consumer_winner_share_code/'+id+'/?dt_fr=' + dt_fr + '&dt_to=' + dt_to
            $.RefreshContent(durl);
        }
    }

    function switchItem(id, status){
        var dt_fr = $('#dt_fr').val();
        var dt_to = $('#dt_to').val();
        var durl = 'sales/switch/consumer_winner_share_code/'+id+'/'+status+'/?dt_fr=' + dt_fr + '&dt_to=' + dt_to
        $.RefreshContent(durl);
    }

    //2023-93-99下载成TXT
    function fileDownload (filename,what) {
        var oReq = new XMLHttpRequest();
        oReq.open("GET", "/obj/list/export/?what="+what+"", true);
        oReq.responseType = "blob";
        oReq.onload = function (oEvent) {
            var content = oReq.response;
            var elink = document.createElement('a');
            elink.download = filename;
            elink.style.display = 'none';
            var blob = new Blob([content]);
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            document.body.removeChild(elink);
        };
        oReq.send();
    };

    function changeTableData(status){
        var firstLink = $(window.click_row).find('td:first a'); 
        $(firstLink).text(status_dict[status][0])
        $(firstLink).removeAttr('style')
        $(firstLink).attr('style', status_dict[status][1])
        if(status_dict[status][2]){
            $(firstLink).attr(status_dict[status][2])
        }
        if(status == 2){
            $(window.click_row).find('.btn-permit').attr('disabled','disabled')
        }
    }
</script>

<ol class="breadcrumb">
        <li>{{subtt.0}}</li>
        <li>{{subtt.1}}</li>     
</ol>

{% if show_add %}
    <div class="title_right">
        <div class="pull-right">
            <span class='input-group'>
                <a type="button" class='btn btn-obj-add l-a create-btn' href='{{murl}}'>
                    <span class='glyphicon glyphicon-plus' style='padding-right:5px;'></span>{% trans '新建' %}
                </a>
            </span>
        </div>
    </div>        
{% endif %}

<form role="form"  class="form-inline" style="margin-top:0px;padding-left:10px;">
    <div class="col-sm-2 input-group">
        <span class="input-group-addon">{% trans "开始日期"%}</span>
        <input class="datepicker form-control" id="dt_fr" />
    </div>
    <div class="col-sm-2 input-group">
        <span class="input-group-addon">{% trans "结束日期"%}</span>
        <input class="datepicker form-control" id="dt_to" />
    </div>
    <div class="col-sm-2 input-group">
        <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>
</form>
<div class="clearfix"></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>