import React from 'react';
import { DecisionSupportFile, DecisionSupportFileStatus } from '../../types/decision';
import { ItemPanel, TabDefinition, Item } from './ItemPanel';
import { useUserProfile } from '../../gql/queries/profile';
import { inWorkgroup } from '../../types/workgroup';
import { useDecisions } from '../../gql/queries/decisions';

export function DecisionSupportFilePanel() {
  const { user } = useUserProfile();
  const { decisions } = useDecisions();

  const tabs: TabDefinition[] = [
    {
      label: 'Mes dossiers en cours',
      itemFilter: (item: Item) => {
        const dsf = item as DecisionSupportFile;
        return dsf.status === DecisionSupportFileStatus.Opened && inWorkgroup(user, dsf.workgroup);
      }
    },
    {
      label: 'Ouverts',
      itemFilter: (item: Item) => (item as DecisionSupportFile).status === DecisionSupportFileStatus.Opened
    },
    {
      label: 'Clos',
      itemFilter: (item: Item) => (item as DecisionSupportFile).status === DecisionSupportFileStatus.Closed
    },
  ];


  return (
    <ItemPanel
      className='is-link'
      title="D.A.D."
      newItemUrl="/decisions/new"
      items={decisions}
      tabs={tabs}
      itemIconClassName='fas fa-folder'
      itemKey={item => item.id}
      itemLabel={item => item.title}
      itemUrl={item => `/decisions/${item.id}`}
    />
  );
}