[% USE CSV -%]

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

  [% IF 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_duplex' %]
    [% headers.push('Duplex Setting') %]
    [% headers.push('Duplex') %]
  [% ELSIF item.name == 'c_power' %]
    [% headers.push('PoE Status') %]
    [% headers.push('PoE Power (mW)') %]
  [% 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 = [] %]

  [% 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('') %]
    [% END %]
  [% END %]

  [% IF params.c_port %]
    [% myport.push(row.port) %]
  [% 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('') %]
    [% END %]
  [% 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_duplex %]
    [% IF row.up == 'up' AND row.duplex %]
      [% myport.push( row.duplex_admin.ucfirst || 'Auto' ) %]
      [% myport.push( row.duplex.ucfirst ) %]
    [% ELSE %]
      [% myport.push('') %]
      [% myport.push('') %]
    [% END %]
  [% END %]

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

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

  [% IF params.c_speed %]
    [% myport.push(row.speed) %]
  [% 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(row.vlan) %]
  [% END %]

  [% IF params.c_vmember %]
    [% SET portname = row.port %]
    [% SET output = '' %]
    [% FOREACH vlan IN vlans.$portname.vlan_set.nsort %]
      [% 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_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('') %]
    [% 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 %]

        [% 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 %]
