{% set organization = function('SearchDao::searchOrganization')|json_encode() %}
<div id="organization" style="margin-bottom: 10px;">
    <span>门店</span>
    <label for="depth_1" class="hidden"></label>
    <select data-depth="1" id="depth_1">
        <option value="-1">请选择</option>
    </select>
    <label for="depth_2" class="hidden"></label>
    <select data-depth="2" id="depth_2" class="hidden">
        <option value="-1">请选择</option>
    </select>
    <label for="depth_3" class="hidden"></label>
    <select data-depth="3" id="depth_3" class="hidden">
        <option value="-1">请选择</option>
    </select>
    <label for="depth_4" class="hidden"></label>
    <select data-depth="4" id="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 = $('#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($('select[data-depth]:not(:last)'), function (index, item) {
            var next_depth = Number($(item).data('depth')) + 1;
            var next_select = $('#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 = $('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($('select[data-depth]:not(.hidden):last').val());
    }
</script>