{% macro selectOrganization(prefix) %}
{% set organization = function('SearchDao::searchOrganization')|json_encode() %}
{% set quyu = function('SearchDao::searchOrganization',1) %}
<div class="row" id="{{ prefix }}_organization">
    <div class="col-md-12">
        <div class="input-group">
            <span class="input-group-addon">部门</span>
            <label for="{{ prefix }}_depth_1" class="hidden"></label>
            <select data-depth="1" id="{{ prefix }}_depth_1" class="form-control widthAuto">
                <option value="-1">请选择</option>
            </select>
            <label for="{{ prefix }}_depth_2" class="hidden"></label>
            <select data-depth="2" id="{{ prefix }}_depth_2" class="form-control widthAuto">
                <option value="-1">请选择</option>
            </select>
            <label for="{{ prefix }}_depth_3" class="hidden"></label>
            <select data-depth="3" id="{{ prefix }}_depth_3" class="form-control widthAuto">
                <option value="-1">请选择</option>
            </select>
            <label for="{{ prefix }}_depth_4" class="hidden"></label>
            <select data-depth="4" id="{{ prefix }}_depth_4" class="form-control widthAuto hidden">
                <option value="-1">请选择</option>
            </select>
        </div>
    </div>
</div>
<br />
<script>
    $(document).ready(function () {
        var organization = eval('{{ organization }}');
        $.each(organization, function (index, item) {
            var select = $('#{{ prefix }}_depth_' + item.Depth);
            var css = '';
            if (item.Depth != 1) {
                css = 'class="hidden"';
            }
            select.append('<option value="' + item.Id + '" data-parent="' + item.ParentId + '" ' + css + '>' + item.Name + '</option>');
        });
        $("#{{ prefix }}_depth_1").find("option[value='1']").attr("selected","selected").show();
        $("#{{ prefix }}_depth_2").find("option[value='6']").attr("selected","selected").show();
        $("#{{ prefix }}_depth_3").find("option[value='28']").attr("selected","selected").show();

        $.each($('#{{ prefix }}_organization select[data-depth]:not(:last)'), function (index, item) {
            var next_depth = Number($(item).data('depth')) + 1;
            var next_select = $('#{{ prefix }}_depth_' + next_depth);
            $(item).change(function () {
                var parent_id = $(this).val();
                $(this).nextAll().find('option:not(:first)').addClass('hidden');
                $(this).nextAll().find('option:first').attr('selected', 'selected');
                $(this).nextAll().addClass('hidden');
                $.each(next_select.find('option[data-parent=' + parent_id + ']'), function (index, item) {
                    $(item).removeClass('hidden');
                    next_select.removeClass('hidden');
                });
            });
        });

        if('{{ prefix }}' != 'consultant'){
            var search_organization = Number(getUrlParmas().organization);
            if (search_organization > 0) {
                var organization_select = $('#{{ prefix }}_organization select[data-depth]');
                var search_option = organization_select.find('option[value=' + search_organization + ']');
                var index = search_option.parent().index('[data-depth]');
                for (var i = 0; i <= index; i++) {
                    var option = organization_select.find('option[value=' + search_organization + ']');
                    //显示 筛选门店
                    option.attr('selected', 'selected');
                    search_organization = option.data('parent');
                    //显示 筛选门店 相同parent门店
                    var same_parent_option = organization_select.find('option[data-parent=' + search_organization + ']');
                    same_parent_option.removeClass('hidden');
                    //下拉框
                    option.parent().removeClass('hidden');
                }
            }
        }
    });

    function getOrganization(prefix) {
        return Number($('#' + prefix + '_organization').find('select[data-depth]:not(.hidden):last').val());
    }
</script>
{% endmacro %}