{% block recConsultant %}
<style>
    input[type=radio] {
        margin: 0 4px 0 0;
    }
</style>
<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">
                <label>查询类型:</label>
                <input type="radio" name="search_type" value="search_organization" id="search_organization">部门查询
                <input type="radio" name="search_type" value="search_consultant" id="search_consultant">置业顾问查询

                <div class="row" style="display: none;margin-top: 10px;margin-left: 1px" id="search_type_0">
                    {% import "selectOrganization.html" as macro %}
                    {{ macro.selectOrganization("consultant") }}

                </div>
                <div class="row" style="display: none;margin-top: 10px" id="search_type_1">
                    <div class="col-md-6">
                        <input type="text" placeholder="请出入置业顾问姓名搜索" id="consultantName" class="form-control">
                    </div>
                </div>
                <div style="margin-top: 10px">
                    <button type="button" class="btn btn-success" id="searchConsult">搜索</button>
                </div>

            </div>
            <div class="modal-body" style="height: 300px;overflow: auto;">
                <ul id="consultantList">
                </ul>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function(){
        var search_type_0 = $('#search_type_0');
        var search_type_1 = $('#search_type_1');
        $('input[name=search_type]').change(function () {
            var value = $(this).val();
            switch (value) {
                case 'search_organization':
                    search_type_0.show();
                    search_type_1.hide();
                    break;
                case 'search_consultant':
                    search_type_0.hide();
                    search_type_1.show();
                    break;
                default:
                    search_type_0.hide();
                    search_type_1.hide();
                    break;
            }
        });
    });
    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($('#search_organization').is(':checked') && organizationId == -1){
                alert("请选择部门");
                return false;
            }
            if($('#search_consultant').is(':checked') && $.trim(consultantName) == ""){
                alert("请输入置业顾问姓名");
                return false;
            }
            var search_type = $("input[name='search_type']:checked").val();
            if(search_type == "search_organization"){
                searchConsultant(organizationId,"");
            }else if(search_type == "search_consultant"){
                searchConsultant("",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){
                if (confirm('设置'+$(this).data().name+'为置业顾问吗？')) {
                    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 %}