#include <iostream>
#include <fstream>
#include <cstring>
#include <process.h>
using namespace std;
char ch, pass[20];
void PAT();
struct address                                                                                          //for address
{
    char addr[60], district[20], state[30], country[20];
};
class patient                                                                                           //class containing patient info
{
    char name[40];
    int age;
    char gender[10];
    int nods;
    char fname[40];
    int phoneno;
    char naphy[40];
    int roomno;
    char bgroup[4];
    int chargep;
    char attendant[40];
    char dept[40];
    address ap;
public:
    int pid;
    void padd();
    void pview();
    void pviewf();
    void pmodify();
}p, t, s, u, a;
void patient::padd()                                                                                    //funtion to add a patient record
{
    cout<<"\nEnter password to add:";               cin.getline(pass, 20);
    if (strcmp(pass, "vampuhi")==0)
    {
        cout<<"\nEnter patient ID:";                    cin>>pid;
        cin.get(ch);
        cout<<"\nEnter name of the patient:";           cin.getline(name, 40);
        cout<<"\nEnter age of the patient:";            cin>>age;
        cin.get(ch);
        cout<<"\nGender of patient:";                   cin.getline(gender, 10);
        cout<<"\nNumber of days stayed:";               cin>>nods;
        cin.get(ch);
        cout<<"\nEnter the patient's father name:";     cin.getline(fname, 40);
        cout<<"\nEnter address:";
        cout<<"\n\tStreet:";                            cin.getline(ap.addr, 60);
        cout<<"\n\tDistrict:";                          cin.getline(ap.district, 20);
        cout<<"\n\tState:";                             cin.getline(ap.state, 30);
        cout<<"\n\tCountry:";                           cin.getline(ap.country, 20);
        cout<<"\nEnter phone number:";                  cin>>phoneno;
        cin.get(ch);
        cout<<"\nEnter name of physician:";             cin.getline(naphy, 40);
        cout<<"\nEnter room number:";                   cin>>roomno;
        cin.get(ch);
        cout<<"\nBlood Group:";                         cin.getline(bgroup, 4);
        cout<<"\nEnter charge per day:";                cin>>chargep;
        cin.get(ch);
        cout<<"\nEnter attendant:";                     cin.getline(attendant, 40);
        cout<<"\nEnter department in which admitted:";  cin.getline(dept, 40);
    }
    else
    {
        cout<<"\nGoing back to patients menu!";
        PAT();
    }   
}
void patient::pview()                                                                                   //function to view one patient record
{
    cin.get(ch);
    cout<<"\nEnter password to view full information:";               cin.getline(pass, 20);
    if (strcmp(pass, "vampuhi")==0)
    {
        cout<<"\nPatient ID:"<<pid;
        cout<<"\nName:"<<name;
        cout<<"\nAge:"<<age;
        cout<<"\nGender:"<<gender;
        cout<<"\nNo. of days stayed:"<<nods;
        cout<<"\nFather's name:"<<fname;
        cout<<"\nAddress:"<<"\t"<<ap.addr<<"\t"<<ap.district<<"\t"<<ap.state<<"\t"<<ap.country;
        cout<<"\nPhone number:"<<phoneno;
        cout<<"\nPhysician:"<<naphy;
        cout<<"\nRoom number:"<<roomno;
        cout<<"\nBlood Group:"<<bgroup;
        cout<<"\nTotal charge:"<<chargep*nods;
        cout<<"\nAttendant:"<<attendant;
        cout<<"\nDepartment in which admitted:"<<dept;
    }
    else
    {        cout<<"\nGoing back to patients menu!";
             PAT();
    }
}
void patient::pviewf()                                                                                  //function to view all patient record
{
    cout<<"\n\nPateint ID:"<<pid;
    cout<<"\nName:"<<name;
    cout<<"\nAge:"<<age;
    cout<<"\nGender:"<<gender;
    cout<<"\nTotal charge:"<<chargep*nods;
    cout<<"\nPhysician:"<<naphy;
    cout<<"\nDepartment in which admitted:"<<dept;
}
void patient::pmodify()                                                                                 //function to modify a patient record
{
    address apm;
    int ag, n, ph, rno;
    char nphy[40], depart[40];
    cin.get(ch);
    cout<<"\nEnter password to modify:";               cin.getline(pass, 20);
    if (strcmp(pass, "vampuhi")==0)
    {
        cout<<"\nPatient ID:"<<pid;
        cout<<"\nName of patient:"<<name;
        cout<<"\nEnter new age (enter -1 to keep it same):";    cin>>ag;
        age=ag;
        cout<<"\nEnter new no. of days stayed (enter -2 to keep it same):"; cin>>n;
        nods=n;
        cout<<"\nEnter new address (enter addno to keep it same):";
        cin.getline(apm.addr, 60);
        if ((strcmp(apm.addr, "addno")!=0)|(strcmp(apm.addr, "Addno")!=0))
        {
            cin.getline(apm.district, 20);
            cin.getline(apm.state, 30);
            cin.getline(apm.country, 20);
            strcpy(ap.addr, apm.addr);
            strcpy(ap.district, apm.district);
            strcpy(ap.state, apm.state);
            strcpy(ap.country, apm.country);
        }
        cout<<"\nEnter new phone no. (enter -3 to keep it same):";              cin>>ph;
        phoneno=ph;
        cout<<"\nEnter new room number (enter -4 to keep it same):";            cin>>rno;
        roomno=rno;
        cout<<"\nEnter name of new physician (enter phyno to keep it same):";   cin.getline(nphy, 40);
        if(strcmp(nphy, "phyno")!=0)
            strcpy(naphy, nphy);
        cout<<"\nEnter new department (enter depno to keep it same):";          cin.getline(depart, 40);
        if(strcmp(depart, "depno")!=0)
            strcpy(dept, depart);
    }
    else
    {    
         cout<<"\nGoing back to patients menu!";
         PAT();
    }
}
int main()
{
    PAT();
    return 0;
}
void PAT()                                                                                              //Patients menu!!
{
    int choice, cs, id;
    long pos;
    char c='y', found='f', f='f';
    cout<<"\n\n";
    for (int i=0;i<10;i++)
        cout<<"-";
    cout<<"PATIENT MENU";
    for (int i=0;i<10;i++)
        cout<<"-";
    cout<<"\n\n\t1. Add a patient record.";
    cout<<"\n\t2. View a patient record.";
    cout<<"\n\t3. View all patients records.";
    cout<<"\n\t4. Modify a patient record.";
    cout<<"\n\t5. Delete a patient record.";
    cout<<"\n\t6. Exit.";
    cout<<"\n\t7. Return to main menu.";
    cout<<"\n\nEnter your choice (1-7):";                           cin>>choice;
    cin.get(ch);
    switch (choice)
    {
        case 1:
        { 
                cout<<"\n\n\nNote: Password is Required!\n";
                ofstream fout("Patients.txt", ios::app);
                while (c=='y'||c=='Y')
                {
                    p.padd();
                    fout.write((char *)&p, sizeof(p));
                    cout<<"\nRecord added to file.";
                    cout<<"\nWant to add more patient records?";
                    cin>>c;
                }
                cout<<"\nWanna go to patients menu or to main menu?";
                cout<<"\n\t1. Patients menu.";
                cout<<"\n\t2. Main menu.";
                cout<<"\nEnter choice (1 or 2):";
                cin>>cs;
                if (cs==1)                                              PAT();
                else                                                    main();
                fout.close();
                break;
        }
        case 2:
        { 
                cout<<"\n\n\nNote: Password is Required!";
                cout<<"\n\nEnter ID of the patient to be viewed:";
                cin>>id;
                ifstream fi("Patients.txt");
                while (!fi.eof())
                {
                    fi.read((char *)&t, sizeof(t));
                    if (t.pid==id)
                    {
                        t.pview();
                        f='t';
                        break;
                    }
                }
                if(f=='f')
                    cout<<"\nID not found!!";
                fi.close();
                cout<<"\n\nWanna go to patients menu or to main menu?";
                cout<<"\n\t1. Patients menu.";
                cout<<"\n\t2. Main menu.";
                cout<<"\nEnter choice (1 or 2):";
                cin>>cs;
                if (cs==1)                                              PAT();
                else                                                    main();
                break;
        }
        case 3: 
        {
                ifstream fin("Patients.txt", ios::in);
                while (!fin.eof())
                {
                    fin.read((char *)&u, sizeof(u));
                    u.pviewf();
                }
                fin.close();
                cout<<"\n\nWanna go to patients menu or to main menu?";
                cout<<"\n\t1. Patients menu.";
                cout<<"\n\t2. Main menu.";
                cout<<"\nEnter choice (1 or 2):";
                cin>>cs;
                if (cs==1)                                              PAT();
                else                                                    main();
                break;
        }
        case 4: 
        {
                fstream fio("Patients.txt");
                cout<<"\n\n\nNote: Password is Required!";
                cout<<"\n\nEnter ID of the patient to be modified:";
                cin>>id;
                while (fio.read((char *)&s, sizeof(s)))
                {
                    pos=fio.tellg();
                    if(s.pid==id)
                    {
                        s.pmodify();
                        fio.seekg(pos);
                        fio.write((char *)&s, sizeof(s));
                    }
                    else
                        cout<<"\nID not found!!";
                }
                fio.close();
                cout<<"\nWanna go to patients menu or to main menu?";
                cout<<"\n\t1. Patients menu.";
                cout<<"\n\t2. Main menu.";
                cout<<"\nEnter choice (1 or 2):";
                cin>>cs;
                if (cs==1)                                              PAT();
                else                                                    main();
                break;
        }
        case 5: 
        {
                ifstream fi("Patients.txt", ios::in);
                ofstream fo("temp.txt", ios::out);
                cout<<"\n\n\nNote: Password is Required!";
                cout<<"\nEnter password to delete:";               cin.getline(pass, 20);
                if (strcmp(pass, "vampuhi")==0)
                {
                    cout<<"\n\nEnter ID of the patient to be deleted:";
                    cin>>id;
                    while (fi.read((char *)&a, sizeof(a)))
                    {
                        if(a.pid==id)
                        {
                            a.pviewf();
                            found='t';
                            cout<<"\n\nAre you sure you want to delete this patient record?(y/n)";
                            cin>>c;
                            if (c!='y'||c!='Y')
                                fo.write((char *)&a, sizeof(a));
                        }
                        else
                            fo.write((char *)&a, sizeof(a));
                    }
                    if (found=='f')
                        cout<<"\n\nID not found!!";
                }
                fi.close();
                fo.close();
                remove ("Patients.txt");
                rename ("temp.txt", "Patients.txt");
                cout<<"\nWanna go to patients menu or to main menu?";
                cout<<"\n\t1. Patients menu.";
                cout<<"\n\t2. Main menu.";
                cout<<"\nEnter choice (1 or 2):";
                cin>>cs;
                if (cs==1)                                              PAT();
                else                                                    main();
                break;
        }
        case 6: 
        {
                cout<<"\n\n";
                for (int k=0; k<20; k++)
                    cout<<"-";
                cout<<"Thank You for using our Software!!";
                for (int k=0; k<20; k++)
                    cout<<"-";
                exit(0);
                break;
        }
        case 7: 
        {       
                cout<<"\n\nGoing back to main menu!!";
                int main();
                break;
        }
        default:
        {       
                cout<<"\n\nWrong choice!!\nGoing back to patients menu!!";
                PAT();
                break;
        }
    }
}