[% USE CSV -%]

[% SET headers = [] %]
[% SET c_nodes_pos = -1 %]
[% FOREACH item IN settings.port_columns %]
  [% NEXT IF item.name == 'c_admin' %]
  [% NEXT IF item.name == 'c_links' %]
  [% NEXT UNLESS params.${item.name} %]

  [% IF item.name == 'c_power' %]
    [% headers.push('PoE Status') %]
    [% headers.push('PoE Power (mW)') %]
  [% ELSIF item.name == 'c_nac_summary' %]
    [% headers.push('PAE AuthConfig Port Control') %]
    [% headers.push('PAE AuthConfig State') %]
    [% headers.push('PAE AuthConfig Port Status') %]
    [% headers.push('PAE AuthSession User') %]
    [% headers.push('PAE AuthSession MAB') %]
    [% headers.push('PAE Last EAPOL Frame Source') %]
  [% ELSIF item.name == 'c_nodes' %]
    [% SET c_nodes_pos = headers.size %]
    [% headers.push('Node MAC') %]
    [% headers.push('Archived Node') %]
    [% headers.push('Age Stamp') IF params.n_age %]
    [% headers.push('Node IP') IF params.n_ip4 OR params.n_ip6 %]
    [% headers.push('Node DNS') IF params.n_ip4 OR params.n_ip6 %]
    [% headers.push('Archived IP') IF params.n_ip4 OR params.n_ip6 %]
  [% ELSIF item.name == 'c_neighbors' %]
    [% headers.push('Neighbor IP') %]
    [% headers.push('Neighbor DNS') %]
  [% ELSIF item.name == 'c_up' %]
    [% headers.push('Port Setting') %]
    [% headers.push('Port Status') %]
  [% ELSE %]
    [% headers.push(item.label) %]
  [% END %]
[% END %]
[% CSV.dump(headers) %]

[% FOREACH row IN results %]
  [% SET myport = [] %]

  [% IF params.c_port %]
    [% myport.push(row.port) %]
  [% END %]

  [% IF params.c_descr %]
    [% myport.push(row.descr) %]
  [% END %]

  [% IF params.c_comment %]
    [% myport.push(row.last_comment) %]
  [% END %]

  [% IF params.c_type %]
    [% myport.push(row.type) %]
  [% END %]

  [% IF params.c_ifindex %]
    [% myport.push(row.ifindex) %]
  [% END %]

  [% IF params.c_lastchange %]
    [% myport.push(row.lastchange_stamp) %]
  [% END %]

  [% IF params.c_name %]
    [% myport.push(row.name) %]
  [% END %]

  [% IF params.c_tags %]
    [% myport.push(row.filtered_tags.join(':')) %]
  [% END %]

  [% FOREACH config IN settings._extra_device_port_cols %]
    [% NEXT UNLESS config.position == 'left' AND params.${config.name} %]
    [% TRY %]
    [% PROCESS "plugin/${config.name}/device_port_column_csv.tt" %]
    [% CATCH %]
    [% myport.push( row.get_column(config.field) ) %]
    [% END %]
  [% END %]

  [% IF params.c_speed_admin %]
    [% myport.push(row.speed_admin) %]
  [% END %]

  [% IF params.c_speed %]
    [% myport.push(row.speed) %]
  [% END %]

  [% IF params.c_duplex_admin %]
    [% myport.push(row.duplex_admin.ucfirst) %]
  [% END %]

  [% IF params.c_duplex %]
    [% myport.push(row.duplex.ucfirst) %]
  [% END %]

  [% IF params.c_error %]
    [% myport.push(row.error_disable_cause) %]
  [% END %]

  [% IF params.c_mac %]
    [% myport.push(row.mac) %]
  [% END %]

  [% IF params.c_mtu %]
    [% myport.push(row.mtu) %]
  [% END %]

  [% IF params.c_pvid %]
    [% myport.push(params.p_vlan_names ? row.get_column('native_vlan_name') : row.vlan) %]
  [% END %]

  [% IF params.c_vmember %]
    [% SET portname = row.port %]
    [% SET output = '' %]
    [% IF params.p_vlan_names %][% SET vlanlist = vlans.$portname.vlan_name_set %]
      [% ELSE %][% SET vlanlist = vlans.$portname.vlan_set %][% END %]
    [% FOREACH vlan IN vlanlist %]
      [% SET output = output _ ',' IF NOT loop.first %]
      [% SET output = output _ vlan %]
    [% END %]
    [% myport.push(output) %]
  [% END %]

  [% IF params.c_power %]
    [% IF row.power AND row.power.admin == 'true' %]
      [% myport.push( row.power.status ) %]
      [% myport.push( row.power.power || '0' ) %]
    [% ELSE %]
      [% myport.push('') %]
      [% myport.push('') %]
    [% END %]
  [% END %]

  [% IF params.c_nac_summary %]
    [% myport.push( row.pae_authconfig_port_control ) %]
    [% myport.push( row.pae_authconfig_state ) %]
    [% myport.push( row.pae_authconfig_port_status ) %]
    [% myport.push( row.pae_authsess_user ) %]
    [% myport.push( row.pae_authsess_mab ) %]
    [% myport.push( row.pae_last_eapol_frame_source ) %]
  [% END %]

  [% FOREACH config IN settings._extra_device_port_cols %]
    [% NEXT UNLESS config.position == 'mid' AND params.${config.name} %]
    [% TRY %]
    [% PROCESS "plugin/${config.name}/device_port_column_csv.tt" %]
    [% CATCH %]
    [% myport.push( row.get_column(config.field) ) %]
    [% END %]
  [% END %]

  [% IF params.c_nodes %]
    [% myport.push('') %]
    [% myport.push('') %]
    [% myport.push('') IF params.n_age %]
    [% myport.push('') IF params.n_ip4 OR params.n_ip6 %]
    [% myport.push('') IF params.n_ip4 OR params.n_ip6 %]
    [% myport.push('') IF params.n_ip4 OR params.n_ip6 %]
  [% END %]

  [% IF params.c_neighbors %]
    [% IF (row.remote_ip OR row.is_uplink) %]
      [% IF row.get_column('neighbor_ip') %]
        [% myport.push( row.get_column('neighbor_ip') ) %]
        [% myport.push( row.get_column('neighbor_dns').remove(settings.domain_suffix) ) %]
      [% ELSIF row.remote_ip AND row.remote_port %]
        [% myport.push( row.remote_ip ) %]
        [% myport.push('') %]
      [% ELSE %]
        [% myport.push('(possible uplink)') %]
        [% myport.push('') %]
      [% END %]
    [% ELSE %]
      [% myport.push('') %]
      [% myport.push('') %]
    [% END %]
  [% END %]

  [% IF params.c_stp %]
    [% myport.push(row.stp) %]
  [% END %]

  [% IF params.c_up %]
    [% myport.push( row.up_admin.ucfirst ) %]
    [% myport.push( row.up.ucfirst ) %]
  [% END %]

  [% FOREACH config IN settings._extra_device_port_cols %]
    [% NEXT UNLESS config.position == 'right' AND params.${config.name} %]
    [% TRY %]
    [% PROCESS "plugin/${config.name}/device_port_column_csv.tt" %]
    [% CATCH %]
    [% myport.push( row.get_column(config.field) ) %]
    [% END %]
  [% END %]

  [% SET has_nodes = 0 %]
  [% IF params.c_nodes %]
    [% FOREACH node IN row.$nodes %]
      [% SET has_nodes = 1 %]
      [% SET mynode = [] %]

      [% CALL mynode.push(node.net_mac.$mac_format_call) %]
      [% CALL mynode.push((node.active ? 'No' : 'Yes')) %]

      [% CALL mynode.push(node.time_last_age) IF params.n_age %]

      [% IF params.n_ip4 OR params.n_ip6 %]
        [% mynode.push('') %]
        [% mynode.push('') %]
        [% mynode.push('') %]
        [% SET has_ips = 0 %]

        [% FOREACH ip IN node.$ips %]
          [% SET has_ips = 1 %]

          [% CALL mynode.splice(-3, 1, ip.ip) %] 
          [% CALL mynode.splice(-2, 1, ip.dns) %] 
          [% CALL mynode.splice(-1, 1, (ip.active ? 'No' : 'Yes')) %] 

          [% CALL myport.splice(c_nodes_pos, mynode.size, mynode) %]
          [% CSV.dump(myport) %]
        [% END %]

        [% CALL myport.splice(c_nodes_pos, mynode.size, mynode) %]
        [% CSV.dump(myport) IF NOT has_ips %]
      [% ELSE %]
        [% CALL myport.splice(c_nodes_pos, mynode.size, mynode) %]
        [% CSV.dump(myport) %]
      [% END %]
    [% END %]
  [% END %]

  [% CSV.dump(myport) IF NOT has_nodes %]
[% END %]
