{% load i18n %}

<style> 

</style>



<script type="text/javascript">
    var status_dict = {
        0: ['等待资料上传','color:grey;text-decoration:none;pointer-events:none','disabled'],
        1: ['待审核','color:blue;text-decoration:underline',''],
        2: ['已激活','color:#07c160;text-decoration:underline;pointer-events:none','disabled'],
        3: ['激活驳回','color:red;text-decoration:underline',''],
    }
    $(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);       
        });

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

            $('#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)};
                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('&'));
                }
            });
        {% endif %}

        
        //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 durl = '{{request.path}}delete/'+id+'/'
            $.RefreshContent(durl);
        }
    }

    function deleteObj(obj, id) {
        var r = confirm('您确定要删除吗？');
        if(r) {
            $.ajax({
                url: `/obj/delete/${obj}/${id}/`,
                success: function(res) {
                    if(res.e == '') {
                        $.RefreshContent('{{request.path}}');
                    }
                }
            })
        }
    }

    //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 %}

{% if dt %}
    <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-5 input-group">
            <input type="checkbox" id="c1" name="status" value="0" style="margin-left: 10px;" {% if '0' in query_status %}checked{% endif %}>
            <label for="c1">&nbsp;等待资料上传</label>
            <input type="checkbox" id="c2" name="status" value="1" style="margin-left: 10px;" {% if '1' in query_status %}checked{% endif %}>
            <label for="c2">&nbsp;待审核</label>
            <input type="checkbox" id="c3" name="status" value="2" style="margin-left: 10px;" {% if '2' in query_status %}checked{% endif %}>
            <label for="c3">&nbsp;已激活</label>
            <input type="checkbox" id="c4" name="status" value="3" style="margin-left: 10px;" {% if '3' in query_status %}checked{% endif %}>
            <label for="c4">&nbsp;激活驳回</label>
        </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>
{% endif %}

<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>