<ul class="orgListRow borderBottomNone orgListBg">
  <li class="orgOrder fb">顺序</li>
  <li class="orgListUser fb">已有栏目</li>
  <li class="orgListStatus fb">状态</li>
  <li class="orgListStatusList fb fb">排列</li>
  <li class="orgListType fb">类型</li>
  <li class="orgListCatalog fb">域名目录</li>
  <li class="orgListOperation fb"><span class="mr10">操作</span></li>
  <div class="cl"></div>
</ul>
<% subfields.each do |field| %>
    <% if is_default_field?(field) %>
        <% name = get_default_name(field) %>
        <ul class="orgListRow">
          <li class="orgOrder">
            <div id="show_priority_<%= field.id %>" ondblclick="edit_priority('#show_priority_<%= field.id %>','#edit_priority_<%= field.id %>');" style="cursor:pointer;background-color:#fffce6;color: #0d90c3; width:30px; margin-left:20px;" title="双击可编辑">
              <%= field.priority %>
            </div>
            <div id="edit_priority_<%= field.id %>" style="display:none; width:30px; margin-left:20px;">
              <input type="text" onblur="update_priority('#show_priority_<%= field.id %>','#edit_priority_<%= field.id %>','<%= field.id %>',$(this).val());" style="width:20px;" value="<%= field.priority %>"/>
            </div>
          </li>
          <li class="orgListUser" title="<%= name %>"><%= name %></li>
          <li class="orgListStatus">默认</li>
          <li class="orgListStatusList">
            <% if field.name == "activity" %>
                默认
            <% else %>
                <%= form_tag({:controller => 'org_subfields', :action => 'update_status', :id => field.id,},:remote=>'true', :method => 'post', :id=>"update_status_form_#{field.id}", :class => 'query_form') do %>
                    <div class="update_status_class"><span  class="hidden"><%= subfield_list_type(field.status) %></span>
                      <a style="display: inline-block;" href="javascript:void(0)" class="pic_edit2"></a>
                    </div>
                    <%= select( :name,:group_id, subfield_status_option_default, { :include_blank => false,:selected => field.status},
                           { :onchange=>"update_status('#update_status_form_#{field.id}');", :id =>"field_status_id", :name => "status",:class=>"undis class-edit fl", :style => "width:56px;"}) %>
                <% end %>
            <% end %>
          </li>
          <li class="orgListType">默认</li>
          <a href="javascript:void(0);" class="linkGrey fr mr10" onclick="hide($(this),'<%= field.id %>');" id="hide_<%= field.id %>"><%= field.hide==0?"隐藏":"可见" %></a>
          <div class="cl"></div>
        </ul>
    <% else %>
        <ul class="orgListRow">
          <li class="orgOrder">
            <div id="show_priority_<%= field.id %>" ondblclick="edit_priority('#show_priority_<%= field.id %>','#edit_priority_<%= field.id %>');" style="cursor:pointer;background-color:#fffce6;color: #0d90c3; width:30px; margin-left:20px;" title="双击可编辑">
              <%= field.priority %>
            </div>
            <div id="edit_priority_<%= field.id %>" style="display:none; width:30px; margin-left:20px;">
              <input type="text" onblur="update_priority('#show_priority_<%= field.id %>','#edit_priority_<%= field.id %>','<%= field.id %>',$(this).val());" style="width:20px;" value="<%= field.priority %>"/>
            </div>
          </li>
          <li class="orgListUser">
            <div id="subfield_show_<%= field.id %>" class="hidden" title="<%= field.name %>"><%= field.name %></div>
            <div id="subfield_edit_<%= field.id %>" style="display:none;">
              <input type="text" name="name" onblur="update_subfield('#subfield_show_<%= field.id %>','#subfield_edit_<%= field.id %>','<%= field.id %>',$(this).val());" value="<%= field.name %>" style="width:70px;"/>
            </div>
          </li>
          <li class="orgListStatus">新增</li>
          <li class="orgListStatusList">
            <% if field.field_type == "Resource" || field.field_type == "Comptec" %>
                默认
            <% else %>
                <%= form_tag({:controller => 'org_subfields', :action => 'update_status', :id => field.id,},:remote=>'true', :method => 'post', :id=>"update_status_form_#{field.id}", :class => 'query_form') do %>
                    <div class="update_status_class"><span class="hidden"><%= subfield_list_type(field.status) %></span>
                      <a style="display: inline-block;" href="javascript:void(0)" class="pic_edit2"></a>
                    </div>
                    <%= select( :name,:group_id, subfield_status_option,
                           { :include_blank => false,:selected => field.status},
                           { :onchange=>"update_status('#update_status_form_#{field.id}');", :id =>"field_status_id", :name => "status",:class=>"undis class-edit fl", :style => "width:56px;"}) %>
                <% end %>
            <% end %>
          </li>
          <li class="orgListType"><%= org_subfield_type field %></li>
          <li class="orgListCatalog hidden">
            <% if Secdomain.where("sub_type=2 and pid=?", @organization.id).count > 0 %>
                <div id="sub_dir_show_<%= field.id %>" ondblclick="edit_dir('#sub_dir_show_<%= field.id %>','#sub_dir_edit_<%= field.id %>');" style="cursor:pointer;background-color:#fffce6;color: #0d90c3;  width:30px; margin-left:20px;" title="双击可编辑">
                  <%= field.subfield_subdomain_dir.nil? ?  '未设置': field.subfield_subdomain_dir.name  %>
                </div>
            <% else %>
                <div id="sub_dir_show_<%= field.id %>" >
                  <%= field.subfield_subdomain_dir.nil? ?  '未设置': field.subfield_subdomain_dir.name  %>
                </div>
            <% end %>
            <div id="sub_dir_edit_<%= field.id %>" style="display:none;">
              <input type="text" name="name" onfocus="observe_input_to_lowercase($(this));" onblur="update_sub_dir('#sub_dir_show_<%= field.id %>','#sub_dir_edit_<%= field.id %>','<%= field.id %>',$(this).val().toLowerCase());" value="<%= field.subfield_subdomain_dir.nil? ?  '': field.subfield_subdomain_dir.name %>" style="width:70px;"/>
            </div>
          </li>
          <%#= link_to "隐藏", hide_org_subfield_organizations_path(field), :method => 'post', :remote => true, :id => "hide_#{field.id}", :class => "linkBlue fr mr5" %>
          <li class="orgListOperation">
            <a href="javascript:void(0);" class="linkGrey fr ml5 mr10" onclick="hide($(this),'<%= field.id %>');" id="hide_<%= field.id %>"><%= field.hide==0?"隐藏":"可见" %></a>
            <span class="fr">|</span>
            <%= link_to "删除", org_subfield_path(field), :method => 'delete', :remote => true, :confirm => "您确定删除吗？", :class => "linkGrey fr ml5 mr5" %>
            <span class="fr">|</span>
            <a href="javascript:void(0);" class="linkGrey fr ml5 mr5" onclick="edit('#subfield_show_<%= field.id %>','#subfield_edit_<%= field.id %>');">编辑</a>
            <% unless field.field_type == "Resource" || field.field_type == "Comptec" %>
                <span class="fr">|</span>
                <!--escape_javascript auto_complete_issues_path(:project_id => @project, :scope => (Setting.cross_project_issue_relations? ? 'all' : nil)-->
                <!--<a href="javascript:void(0);" class="linkGrey fr mr5" onclick="org_submain_new('<%#= escape_javascript new_org_subfield_sub_domain_path(:org_subfield_id => field.id, :field => field) %>');">添加二级目录</a>-->
                <%= link_to("添加二级目录", new_org_subfield_sub_domain_path(:org_subfield_id => field.id, :field => field), :class => "linkGrey fr mr5", :remote => true) %>
            <% end%>
          </li>
          <div class="cl"></div>
          <%= render :partial => 'organizations/subfield_sub_domains_list', :locals => {:field => field} %>
        </ul>
    <% end %>
<% end %>

<script>
    function org_submain_new(url) {
        alert(url);
        $.post(
                url,
                function (data) {
                }
        );
    }
    //二级子目录样式处理
    $(".orgListRow").each(function(){
        $(this).children(".orgSubList:first").css({"border-top":"1px solid #e4e4e4","marginTop":"5px"});
        $(this).children(".orgSubList:last").css("marginBottom","-5px");
        $(this).children(".orgSubList:last").children("li:first").addClass("orgSubTree2");
    });

    $(document).ready(function(){
        $(".pic_edit2").click(function(){
            $(this).parent().hide();
            $(this).parent().next().show();
        });
        $(".class-edit").blur(function(){
            $(this).hide();
            $(this).prev().show();
            var editValue = $(this).children("option:selected").text();
            $(this).prev().children(":first").text(editValue);
        });
        function stopPropagation(e) {
            if (e.stopPropagation)
                e.stopPropagation();
            else
                e.cancelBubble = true;
        };
        $(document).bind('click',function(){
            $('.class-edit').css('display','none');
            $('.update_status_class').show();
        });
        $('.class-edit,.pic_edit2').bind('click',function(e){
            stopPropagation(e);
        });
    });
    function update_status(id){
        $(id).submit();
    }
    function edit(show_id, edit_id) {
        $(show_id).toggle();
        $(edit_id).toggle();
        $(edit_id).find('input').focus();
        $(edit_id).find('input').on('keypress', function (e) {
            if (e.keyCode == 13) {
                this.blur();
            }
        })
    }
    function update_subfield(show_id, edit_id, field_id, input_value) {
        if ($(show_id).html().trim() != input_value.trim()) {
            if (confirm('确定修改为' + input_value + "?"))
                $.ajax({
                    url: "/org_subfields/" + field_id + "?name=" + input_value,
                    type: 'put'
                });
            else{
                $(edit_id).children("input").val($(show_id).html().trim());
            }
        }
        $(show_id).show();
        $(edit_id).hide();
//       $(edit_id).focus();
    }

    function edit_dir(show_id, edit_id){
        $(show_id).toggle();
        $(edit_id).toggle();
        $(edit_id).find('input').focus();
        $(edit_id).find('input').on('keypress', function(e){
            if (e.keyCode == 13){
                this.blur();
            }
        });
    }

    function update_sub_dir(show_id, edit_id, field_id, input_value) {
        if ($(show_id).html().trim() != input_value.trim() && input_value.trim() != '') {
            if (confirm('确定修改为' + input_value + "?"))
                $.ajax({
                    url: "/org_subfields/" + field_id + "/update_sub_dir?sub_dir_name=" + input_value,
                    type: 'put'
                });
            else{
                $(edit_id).children("input").val($(show_id).html().trim());
            }
        }
        $(show_id).show();
        $(edit_id).hide();
    }

    function edit_priority(show_id, edit_id){
        $(show_id).toggle();
        $(edit_id).toggle();
        $(edit_id).find("input").focus();
        $(edit_id).find("input").on('keypress',function(e){
            if (e.keyCode == 13){
                this.blur();
            }
        });
    }

    function update_priority(show_id, edit_id, field_id, input_value){
        var re = /^[0-9]*[1-9]*[0-9]$/
        if(re.test(input_value) && $(show_id).html().trim() != input_value.trim() && input_value.trim() != ''){
            $.ajax({
                url: "/org_subfields/" + field_id + "/update_priority?priority=" + input_value,
                type: 'put'
            });
        }
        else{
            $(edit_id).children("input").val($(show_id).html().trim());
        }
        $(show_id).show();
        $(edit_id).hide();
    }
    function hide(content, id){
        if (content.text() == '隐藏')
            $.ajax({
                url: "/organizations/hide_org_subfield?org_subfield_id=" + id,
                type: "post"
            });
        else
            $.ajax({
                url: "/organizations/show_org_subfield?org_subfield_id=" + id,
                type: "post"
            });
    }
</script>
