{% load i18n %}

{% ifequal obj 'Award' %}
<style>

    #tb tbody td img{
        height:30px;
        cursor:pointer;
    }

</style>
{% endifequal %}
<style>
    .word-wrap{
        width: 100%;white-space: normal;word-wrap: break-word;word-break: break-all;
    }
	.ui-datepicker{
		z-index:1051 !important;
	}
	.modal-footer{
		clear: both;

	}
	.ui-autocomplete{
		z-index: 9999 !important;
	}
    {##tb tr td:first-child{#}
    {#    text-align: center;#}
    {# }#}
</style>



<script type="text/javascript">
    $(document).ready(function(){
        var value = []
        var page_length=10;
        var table=$('#tb').DataTable({
            dom: 'Bfrtip',
            autoWidth: true,
            pageLength: page_length,
            buttons: [{extend:'excelHtml5',text: '导出到Excel'}],
            language:lng,
            {#footerCallback:function (e) {#}
            {#    var lists = $('#tb .checkchild')#}
            {#    console.log('lists',lists)#}
            {#    var list = this.api().column(0, { page: 'current' }).data();#}
            {#    console.log('list',list)#}
            {##}
            {#    var t = false#}
            {#    for (var i = 0; i < list.length; i++) {#}
            {#        if (list[i].checked) {#}
            {#            t = true#}
            {#            console.log(1,i,list[i])#}
            {#        } else {#}
            {#            t = false#}
            {#            console.log(i,'0000')#}
            {#            return;#}
            {#        }#}
            {#    }#}
            {#    var is_checked = $('.checkall').prop("checked")#}
            {#    console.log('is_checked',is_checked)#}
            {#    console.log('t',t)#}
            {#    if (is_checked) {#}
            {#        $('.checkall').prop("checked",t)#}
            {#    }#}
            {# },#}
            order: [],
            columnDefs: [ { "bSortable": false, "aTargets": [ 0 ] }],

			{% if order %}
            order: {{order|safe}},
			{% endif %}

            data:{{data|safe}}

        });
        $(".checkall").on('click',function () {
            var list_before = $('.checkchild')
            for (var i = 0; i < list_before.length; i++) {
                if (list_before[i].checked) {
                    $(".checkchild").prop("checked", false);
                    break;
                }else {
                    $(".checkchild").prop("checked", true);
                    break;
                }
            }
            var list = $('.checkchild')
            for (var i = 0; i < list.length; i++) {
                    if (list[i].checked) {
                        if (value.indexOf(parseInt(list[i].value)) !=-1) {
                            {#value.push(parseInt(list[i].value))#}
                        }else{
                            value.push(parseInt(list[i].value))
                        }

                    }else {
                        if ($.inArray(parseInt(list[i].value), value) == -1) {
                            //$.inArray(元素, 数组) == -1 表示要删除的元素不在原数组中
                        } else {
                            value.splice($.inArray(parseInt(list[i].value), value), 1);
                        }
                        $(".checkchild").prop("checked", false);
                    }
            }
        });

        {#$("#tb tbody").on("click","input",function(){#}
        {#     var check = $(this).find(".checkchild").prop("checked");#}
        {#     if(check && check==true){#}
        {#         $(this).find('.checkchild').prop("checked",false);#}
        {#     }else{#}
        {#         $(this).find('.checkchild').prop("checked",true);#}
        {#     }#}
        {# });#}

        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();
                }
            });
        });

		$('#id_saleman_name').autocomplete({
			source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/salesman_lastname/',
                    dataType: "json",
                    data: {
                        term:   $("#id_saleman_name").val()
                    },
                    success: function(data) {
                        response(data);
                    }

                });
            },
			select: function (event, ui) {
				$("#id_saleman").val(ui.item.value);
				$("#id_saleman_name").val(ui.item.label);
				return false;
			}
		});



        $("#tb").on('click','.checkchild',function (e){
            var check = $(this).prop("checked");
            if (check){
                    if (value.indexOf(parseInt(this.value))){
                        value.push(parseInt(this.value))
                    }
                }else{
                    if($.inArray(parseInt(this.value), value) == -1){
                        //$.inArray(元素, 数组) == -1 表示要删除的元素不在原数组中
                    }else{
                        value.splice($.inArray(parseInt(this.value), value), 1);
                    }
                }
        })

		SubmitThisForm = function (form,url) {
                $(form).ajaxSubmit({
                    type: 'post',
                    url,
                    success: function(html) {

                        $('#bg-modal .modal-body').html(html);
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert('连接服务器失败');
                    }
                });
                return; // 阻止表单自动提交事件
		};


        $('.btn-transfer-all').click(function (){

            if (value.length > 0) {
                if ($('#id_saleman').val() != "") {
                    $.ajax({
                        type: 'post',
                        url: '/company/saleman/manage/?obj=saleman_transfer&uid={{uid}}&to_uid=' + $('#id_saleman').val(),
                        data: {
                            company_ids: value.join(','),
                            delflag: 1
                        },
                        success: function (html) {
                            $('#bg-modal .modal-body').html(html);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert('连接服务器失败');
                        }
                    })
                } else {
                    alert("请选择业务员！")
                }
            } else {
                alert('请勾选需要转移的内容！')
            }
        })
	});
</script>



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


<form role="form" method='POST' enctype="multipart/form-data" class="form-horizontal" id="form1"> {% csrf_token %}
{#    <div class="form-group">#}
{#        <label class="col-sm-2 control-label ">{% trans '已选择:' %} </label>#}
{#        <div class="col-sm-8">#}
{#            <input id='id_company_name' class='form-control' />#}
{#			<input id="id_company" name="company_id" type="hidden" />#}
{#        </div>#}
{#    </div>#}
    <div class="form-group">
        <label class="col-sm-2 control-label ">{% trans '转移至:' %} </label>
        <div class="col-sm-8">
            <input id='id_saleman_name' class='form-control' />
			<input id="id_saleman" name="saleman_id" type="hidden" />
        </div>
    </div>

{#    <div class="form-group">#}
{#        <div class="col-sm-offset-2">#}
{#            <a class="btn btn-default btn-save">转移</a>#}
{#        </div>#}
{#    </div>#}
</form>



{#<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='col-md-12 form-group'>
    <div class="table-responsive">
        <table id='tb' class="table table-striped table-bordered dt-responsive nowrap tb"
            cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" class ="checkall" id="group_checkbox" style="display: none;"/>
                        <label for="group_checkbox">全选/取消全选
                        </label>
                    </th>
                    <th>名称</th>
                    <th>父级</th>
                    <th>营销层级</th>
                    <th>所属战区</th>
                    <th>所属分区</th>
                    <th>地址</th>
                    <th>联系电话</th>
                    <th>终端店</th>
                    <th>渠道</th>
                </tr>
            </thead>
            <tbody>

            </tbody>
            <tfoot>
                <th> <span class="btn btn-sm btn-danger btn-transfer-all "  >批量转移</span> </th>
                <th><input type="text" placeholder="名称"></th>
                <th><input type="text" placeholder="父级"></th>
                <th><input type="text" placeholder="营销层级"></th>
                <th><input type="text" placeholder="所属战区"></th>
                <th><input type="text" placeholder="所属分区"></th>
                <th><input type="text" placeholder="地址"></th>
                <th><input type="text" placeholder="联系电话"></th>
                <th><input type="text" placeholder="终端店"></th>
                <th><input type="text" placeholder="渠道"></th>
            </tfoot>
        </table>
    </div>
</div>