#include"emp_library.h"

 
void add_record(FILE *fp)
{
  //int recsize = sizeof(struct emp);   
  struct emp e;   
  system("cls");
  fflush(stdin);
  printf("\t ADD RECORD \n");   
  char another;
  fseek ( fp, 0 , SEEK_END ) ;
  another = 'Y' ;
  while ( another == 'Y' || another == 'y' )
  {
    printf ( "\nEnter name : ");
    gets(e.name);
    printf("Enter age : ");
    scanf("%d", &e.age);
    printf("Enter basic sal. : " ) ;
    scanf("%f",&e.bs );
    fwrite ( &e, sizeof(struct emp), 1, fp ) ;
    printf ( "\nAdd another Record (Y/N) " ) ;
    fflush ( stdin ) ;
    another = getche( ) ;
  }     
}

void display_record(FILE *fp)
{
  struct emp e;   
  rewind ( fp ) ;
  system("cls");
  printf("%-15s%-6s%-10s\n", "Name", "Age", "Salary");
  printf("----------------------------------\n");
  while ( fread ( &e, sizeof(struct emp), 1, fp ) == 1 )
    printf ( "%-15s%-6d%-10.2f \n", e.name, e.age, e.bs ) ;
  system("pause");     
}
void modify_record(FILE *fp)
{
   char another, empname[30];
   struct emp e;
   another = 'Y' ;
   while ( another == 'Y' )
   {
     system("cls");  
     fflush(stdin);  
     printf ( "\nEnter name of employee to modify " ) ;
     scanf ( "%s", empname ) ;
     rewind ( fp ) ;
     while ( fread ( &e, sizeof(struct emp), 1, fp ) == 1 )
     {
       if ( strcmp ( e.name, empname ) == 0 )
       {
            printf ( "\nEnter new name, age & bs" ) ;
            scanf ( "%s %d %f", e.name, &e.age, &e.bs ) ;
            fseek ( fp, - sizeof(struct emp), SEEK_CUR ) ;
            fwrite ( &e, sizeof(struct emp), 1, fp ) ;
            break ;
          }
        }
        printf ( "\nModify another Record (Y/N) " ) ;
        fflush ( stdin ) ;
        another = getche( ) ;
     }    
}
void delete_record(FILE *fp)
{
  char another, empname[30];
  FILE *ft;
  struct emp e;
  another = 'Y' ;
  while ( another == 'Y' )
  {
        system("cls");
        fflush(stdin);
        printf ( "\nEnter name of employee to delete " ) ;
        scanf ( "%s", empname ) ;
        ft = fopen ( "TEMP.DAT", "wb" ) ;
        rewind ( fp ) ;
        while ( fread ( &e, sizeof(struct emp), 1, fp ) == 1 )
        {
          if ( strcmp ( e.name, empname ) != 0 )
            fwrite ( &e, sizeof(struct emp), 1, ft ) ;
        }
        fclose ( fp ) ;
        fclose ( ft ) ;
        remove ( "EMP.DAT" ) ;
        rename ( "TEMP.DAT", "EMP.DAT" ) ;
        fp = fopen ( "EMP.DAT", "rb+" ) ;
        printf ( "Delete another Record (Y/N) " ) ;
        fflush ( stdin ) ;
        another = getche( ) ;
  }       
}
void query_record(FILE *fp)
{
  int choice;
  system("cls");
  printf(":::: Query Record Section :::: \n");
  printf("1.  query by age \n");
  printf("2.  query by salary \n");
  printf("3.  query by name \n");
  scanf("%d", &choice);
  switch(choice)
  {
    case 1:
       query_age(fp);
       break;
    case 2:
       query_salary(fp); 
       break;
    case 3: 
       query_name(fp);
       break;      
     default:
       printf("please enter correct choice \n");
       system("pause");            
   }         
} 
void query_age(FILE *fp)
{  
  int age, choice;
  struct emp e;
  system("cls");
  printf("Query record by Age \n");
  printf("Enter age for query : ");
  scanf("%d",&age);
  printf("Please select from the following \n");
  printf("1. greater than %d\n",age);
  printf("2. less than %d\n",age);
  printf("3. equal than %d\n",age);
  fflush(stdin);
  scanf("%d", &choice);
  switch(choice)
  {
    case 1:
      rewind ( fp ) ;
      printf("Name  Age  Salary\n");
      while ( fread ( &e, sizeof(struct emp), 1, fp ) == 1 )
      {
         if(e.age>age)   
           printf ( "%s %d %0.2f \n", e.name, e.age, e.bs ) ;
      }     
      break;
      case 2:
      rewind ( fp ) ;
      printf("Name  Age  Salary\n");
      while ( fread ( &e, sizeof(struct emp), 1, fp ) == 1 )
      {
         if(e.age<age)   
            printf ( "%s %d %0.2f \n", e.name, e.age, e.bs ) ;
      }     
      break;
      case 3:
      rewind ( fp ) ;
      printf("Name  Age  Salary\n");
      while ( fread ( &e, sizeof(struct emp), 1, fp ) == 1 )
      {
         if(e.age == age)   
            printf ( "%s %d %0.2f \n", e.name, e.age, e.bs ) ;
      }     
      break;
      default:
          printf("Please enter correct choice \n");         
    }
    system("pause"); 
}
void query_salary(FILE *fp)
{
     
}
void query_name(FILE *fp)
{

}
