#include <iostream>
#include <string.h>
#include <fstream>
#include <cstdlib>

using namespace std;

typedef struct{
	int ngay;
	int thang;
	int nam;
}date;

bool KiemTra(int ngay, int thang, int nam) {
	if ((nam % 100 != 0 && nam % 4 == 0) || (nam % 400 == 0)) {
		if(thang==2) {
			if(ngay<=29) return true;
		}
		else if(thang==4 || thang==6 || thang==9 || thang==11) {
			if(ngay<=30) return true;
		}
		else {
			if(ngay<=31) return true;
	    }
	}
	else if ((nam % 100 == 0 && nam % 4 != 0) || (nam % 400 != 0)) {
		if(thang==2) {
			if(ngay<=28) return true; 
		}
		else if(thang==4 || thang==6 || thang==9 || thang==11) {
			if(ngay<=30) return true; 
		}
		else if(ngay<=31) return true; 
	}
	else return false;
}

class NhanVien
{
	private:
		char tennv[30];
		char msnv[30];
		char gioitinh[20];
		double luong;
	public:
		void setTennv(char*);
		char* getTennv();
		void setMsnv(char*);
		char* getMsnv();
		void setGioitinh(char*);
		char* getGioitinh();
		void setLuong(double);
		double getLuong();
		void Input();
		void show();		
};
void NhanVien::setTennv(char* ten)
    {
      strcpy(tennv,ten);
    }
char* NhanVien::getTennv() 
    {
    	return tennv;
	}
void NhanVien::setMsnv(char* ms)
    {
       strcpy(msnv,ms);	
	}	     
char* NhanVien::getMsnv()
	{
		return msnv;
	}
void NhanVien::setGioitinh(char* gt)
    {	
	     strcpy(gioitinh,gt);
	}	
char* NhanVien::getGioitinh()
    {
        return gioitinh;	
	}	
void NhanVien::setLuong(double luog)
    {
    	luong=luog;
	}	
double NhanVien::getLuong()
    {
        return luong;	
	}	
void NhanVien::Input(){
	char ten[30];
	char gt[20];
	char ms[30];
	double luog;
	cout<<"Ten nhan vien: "; cin.ignore(); cin.getline(ten,30); setTennv(ten);
	cout<<"Ma so nhan vien: "; cin.getline(ms,30); setMsnv(ms);
	cout<<"Gioi tinh: ";  cin.getline(gt,20); setGioitinh(gt); 
	cout<<"Luong: "; cin>>luog; setLuong(luog);
}
void NhanVien::show(){

	cout<<"Ten nhan vien: " << getTennv() << endl; 
	cout<<"Ma so nhan vien: "<< getMsnv() << endl; 
	cout<<"Gioi tinh: "<< getGioitinh() << endl;
	cout<<"Luong: "<< getLuong() << endl;
}
class HoaDon{
	protected:
		date datein;
		char type[20];
		char soHD[20];
	public:
		void setngaynhap(date ngaylap){
			datein.ngay = ngaylap.ngay;
		}
		int getngaynhap(){
			return datein.ngay;
		}
		void setthangnhap(date ngaylap){
			datein.thang = ngaylap.thang;
		}
		int getthangnhap(){
			return datein.thang;
		}
		void setnamnhap(date ngaylap){
			datein.nam = ngaylap.nam;
		}
		int getnamnhap(){
			return datein.nam;
		}
		void setsoHD(char* sohd){
			strcpy(soHD,sohd);
		}
		char* getsoHD(){
			return soHD;
		}
		void setloai(char* loai){
			strcpy(type,loai);
		}
		char* getloai(){
			return type;
		}
		
//		void setmaNV(char* manv){
//			strcpy(maNV,manv);
//		}
//		char* getmaNV(){
//			return maNV;
//		}
		void Nhaphoadon();
		void Showhoadon();
		void ghiHD(fstream & ofhd){
			ofhd.write( reinterpret_cast <char *> (this), sizeof(HoaDon));
		};
		void docHD(fstream & ifhd){
			ifhd.read(reinterpret_cast <char *> (this), sizeof(HoaDon));
		};
	
};
void HoaDon::Nhaphoadon(){
	date ngaylap;
	char sohd[20];
	char loai[20];
//	char manv[30];
	cout << "========== Nhap thong tin Hoa Don. ============ " << endl;
	cout << "Nhap so Hoa Don: "; cin.ignore(); cin.getline(sohd,30); setsoHD(sohd);
	cout << "nhap loai hoa don: "; cin.getline(loai,30); setloai(loai);
//	cout << "Nhap ma nhan vien quan ly: "; cin.getline(manv,30); setmaNV(manv);	

	do {
		cout << "Nhap ngay nhap hoa don: "; cin >> ngaylap.ngay; setngaynhap(ngaylap);
		cout << "Nhap thang nhap hoa don: "; cin >> ngaylap.thang; setthangnhap(ngaylap);
		cout << "Nhap nam nhap hoa don: "; cin >> ngaylap.nam; setnamnhap(ngaylap);
		if (KiemTra(ngaylap.ngay, ngaylap.thang, ngaylap.nam) == false) {
			cout << "Xin nhap lai ngay thang nam\n";
		}
	} while(KiemTra(ngaylap.ngay, ngaylap.thang, ngaylap.nam) == false);	
}

void HoaDon::Showhoadon(){
	cout << "So Hoa Don: " << getsoHD() << endl;
	cout << "Thoi gian nhap hoa don: " << getngaynhap() << "/" << getthangnhap() << "/" << getnamnhap() << endl;
	cout << "loai hoa don: " << getloai() << endl;
//	cout << "Ma nhan vien quan ly: " << getmaNV() << endl;
}

class VATTU {
private:
	char maVT[10];
	char tenVT[20];
	char hangVT[20];
	int soluong;
	int giaVT;
public:
	
	void  setmaVT(char* maVTIn) {
		strcpy(maVT,maVTIn);
	};  
	char*  getmaVT(){
		return maVT;
    };   
	
	void settenVT(char* tenVTIn)
	{
		strcpy(tenVT,tenVTIn);	
	};
	char* gettenVT() {
		return tenVT;
	};
	
	void sethangVT(char* hangVTIn){
		strcpy(hangVT,hangVTIn);
	}
	char* gethangVT(){
		return hangVT;
	}

	void  setsoluong(int soluongIn) {
		soluong = soluongIn;
    };  
	int   getsoluong() {
		return soluong;
	}; 
	void setgiaVT(int giaVTIn){
		giaVT = giaVTIn;
	}
	int getgiaVT(){
		return giaVT;
	}
	void  nhapVT();
	void  hienthiVT();
};

void VATTU::nhapVT() {
	char maVTIn[10];
	char tenVTIn[20];
	char hangVTIn[20];
	int soluongIn;
	int giaVTIn;
	cout << "Nhap ten vat tu:"; cin.ignore(); cin.getline(tenVTIn,20); settenVT(tenVTIn);
	cout << "Nhap ma vat tu: "; cin >> maVTIn; setmaVT(maVTIn);
	cout << "Nhap hang : "; cin.ignore(); cin.getline(hangVTIn,20); sethangVT(hangVTIn);
	cout << "Nhap gia: "; cin >> giaVTIn; setgiaVT(giaVTIn);
	cout <<"Nhap so luong: "; cin >> soluongIn; setsoluong(soluongIn);
}
void VATTU::hienthiVT(){
	cout << "\nMa vat tu : " << getmaVT() << endl;
	cout << "Ten vat tu :" << gettenVT() << endl;
	cout << "Hang: " << gethangVT() << endl;
	cout << "So luong : " << getsoluong() << endl;
	cout << "Don Gia: " << getgiaVT() << endl;
};



void menu(){
	cout<< "================== " << endl;
	cout<< "= QUAN LY VAT TU = " << endl;
	cout<< "================== " << endl;
	cout<< "                    " << endl;
	cout<< "1. Nhap thong tin nhan vien"<<endl;
	cout<< "2. Hien thi thong tin nhan vien"<<endl;
	cout<< "3. Nhap thong tin vat tu " << endl;
	cout<< "4. Hien thi thong tin vat tu" << endl;
	cout<< "5. Nhap hoa don" << endl;
	cout<< "6. Hien thi hoa don " << endl;
	cout<< ". Nhan '0' de thoat. "<<endl;
	cout<< "========================= "<<endl;
}
int choice(){
	int c;
	cout<<" Moi nhap lua chon: ";
	cin>>c;
	return c;
}

void option(){
	HoaDon hd[100];
	VATTU vt[100];
	NhanVien *nv = new NhanVien[100];
	int choose;
	do{
		menu();
		choose=choice();
		switch (choose){
			case 1:{
				fstream fileOut("cuong123.txt",ios::out | ios::app | ios::binary);
					if (!fileOut){   
				     cout << "Khong the tao duoc tep tin " << endl;
				       exit(1);
			    }
					NhanVien *nv = new NhanVien[100];

		
					int n;
				cout << "Nhap so luong nhan vien: ";
				cin >> n;
				//fileOut.write(reinterpret_cast<char *>(nv),sizeof(NhanVien));
				for(int i=1;i<=n;i++){
				    cout << "Moi nhap thong tin nhan vien " << i <<endl;
					nv[i].Input();	
			    fileOut.write(reinterpret_cast<char *>(&nv[i]),sizeof(NhanVien)); 
			    }
				fileOut.close();
				break;
				} 
			case 2:{
				fstream fileIn("cuong123.txt",ios::in | ios::binary);
				if(!fileIn){
					cout << "Khong the mo duoc tep tin "<<endl;
					exit(1);
				}
				int n;
				cout << "cac nhan  vien co trong file: ";
				cin >> n;
				for(int i=1;i<=n;i++){
				fileIn.read(reinterpret_cast<char*>(&nv[i]),sizeof(NhanVien));
				cout << "Hien thi nhan vien thu: " << i << endl;
				nv[i].show();
				}
				fileIn.close(); 
				break; 
			}
				case 3: {
				fstream fileOut("khoVT.txt", ios::out | ios::binary | ios::app);
				if (!fileOut) {    // Không mở được tệp 
					cout << "Khong the tao duoc tep tin " << endl;
					exit(1);
				}
	
				int n;
				cout << "So luong vat tu muon nhap: ";
				cin >> n;
				for (int i = 0; i < n; i++) {
					cout << "Thong tin vat tu " << i + 1 << endl;
					vt[i].nhapVT();
					fileOut.write(reinterpret_cast<char *>(vt),
						sizeof(VATTU));
				}
				fileOut.close();
				
				break;	
			}
			case 4: {
				int m = 0;
				fstream fileIn("khoVT.txt", ios::in | ios::binary);
	
				while (fileIn.read(reinterpret_cast<char *>(vt), sizeof(VATTU))) {
					m = m + 1;
				}
				for (int i = 0; i < m; i++) {
	
					vt[i].hienthiVT();
				}
				fileIn.close();
				break;
			}
			case 5:{
				int n;
				cout << "Nhap so luong hoa don: ";
				cin  >> n;
				fstream ofhd("HoaDon.txt", ios::out|ios::binary);
				for(int i =1; i<=n; i++){
				
					
					if(!ofhd){
						cout << "khong mo duoc tep tin" << endl;
						exit(1);
						}
						
						hd[i].Nhaphoadon();
						hd[i].ghiHD(ofhd);	
				}
				ofhd.close();
				break;
			}
			case 6:{    
				fstream ifhd("HoaDon.txt", ios::in|ios::binary);
				if(!ifhd){
					cout << "Khong mo duoc file " << endl;
					exit(1);
				}
				int n;
				cout << "Nhap so hoa don dang co trong file: ";
				cin >> n;
				for(int i=1; i<=n; i++){
				
					hd[i].docHD(ifhd);
					if(!ifhd.eof() ) hd[i].Showhoadon();
				}
				
				
				ifhd.close();
				break;
			}
		}
	}
	while(choose !=0);	
}

int main(){
	option();
	return 0;
}
