{% macro selectOrganization(prefix) %}
{% set organization = function('SearchDao::searchOrganization')|json_encode() %}
<div id="{{prefix}}_organization" style="margin-bottom: 10px;">
    <label>部门:</label>
    <label for="{{prefix}}_depth_1" class="hidden"></label>
    <select data-depth="1" id="{{prefix}}_depth_1">
        <option value="-1">请选择</option>
    </select>
    <label for="{{prefix}}_depth_2" class="hidden"></label>
    <select data-depth="2" id="{{prefix}}_depth_2" class="hidden">
        <option value="-1">请选择</option>
    </select>
    <label for="{{prefix}}_depth_3" class="hidden"></label>
    <select data-depth="3" id="{{prefix}}_depth_3" class="hidden">
        <option value="-1">请选择</option>
    </select>
    <label for="{{prefix}}_depth_4" class="hidden"></label>
    <select data-depth="4" id="{{prefix}}_depth_4" class="hidden">
        <option value="-1">请选择</option>
    </select>
</div>
<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>');
        });

        $.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');
                });
            });
        });

        var search_organization = Number('{{ req.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() {
        return Number($('#{{prefix}}_organization select[data-depth]:not(.hidden):last').val());
    }
</script>
{% endmacro %}