{% block recConsultant %}
<div class="modal fade" id="myConsultant" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                {% import "selectOrganization.html" as macro %}
                {{ macro.selectOrganization("consultant") }}
                <div class="row">
                    <div class="col-md-6">
                        <input type="text" placeholder="请出入置业顾问姓名搜索" id="consultantName" class="form-control">
                    </div>
                    <div class="col-md-2">
                        <button type="button" class="button action" id="searchConsult">搜索</button>
                    </div>
                </div>
            </div>
            <div class="modal-body" style="height: 300px;overflow: auto;">
                <ul id="consultantList">
                </ul>
            </div>
        </div>
    </div>
</div>
<script>

    function close_modal_consultantList(){
        $("#myConsultant").modal('hide');
    }
    function init_modal_myConsultantList(callback){
        //添加置业顾问弹出层中搜索框的搜索内容
        $("#searchConsult").click(function(){
            $("#consultantList").find("li").remove();
            var consultantName = $("#consultantName").val();
            var organizationId = getOrganization('consultant');
            var select = $('#consultant_organization').find('select[data-depth]:not(.hidden)');
            if(select.length > 1 && organizationId == -1 && consultantName == ""){
                alert("请选择部门")
            }
            searchConsultant(organizationId,consultantName);
        });



        $("#consultantList").on("click",".consultantImg",function(){
            var firstId = $("#subsidiaryId").val();
            if(firstId){
                if($(this).data().subsidiaryId != firstId){
                    alert("请选择相同部门的置业顾问");
                    return false;
                }
            }
            var consultantArray = [];
            $('input[name="data[recConsultant][]"]').each(function (index, item) {
                consultantArray.push(Number($(item).val()));
            });
            if($.inArray(Number($(this).data().id),consultantArray) == -1){
                callback($(this).data());
            }else{
                alert('不能重复置业顾问');
            }
        });

    }

    function searchConsultant(organizationId,consultantName){
        $.ajax({
            type: "GET",
            url: "/tospur/wp-admin/admin-ajax.php",
            data: "action=searchConsultant&organizationId="+organizationId+"&consultantName="+consultantName,
            success:function(json){
                $("#consultantList").find("li").remove();
                if(json){
                    for(var i = 0; i <=json.length-1; i++){
                        var item =  json[i];
                        var name = item["name"];
                        var imgUrl = item["imageUrl"];
                        var img = $("<img>").attr({"src":imgUrl,"height":100,"width":100,"style":"margin-right:50px"});
                        var li = $("<li>").attr("id",item["id"]).append(img).append(name).addClass("consultantImg").data(item);
                        $("#consultantList").append(li);
                    }
                }
            }
        });
    }
</script>
{% endblock %}