package admin.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import model.ConnectionPool;
import model.ThongTinSanPham;


public class ProductMods {
	
	 public boolean CreateProduct(ThongTinSanPham sp) {
		 PreparedStatement createpd = null;
			boolean result = false;

			Connection con = new ConnectionPool().getConnection();
			try {
				
				createpd = con.prepareStatement("insert into product(name, image, price, description, category, status)  values(?, ?, ?, ?, ?, ?)");
				createpd.setString(1, sp.getName());
				createpd.setString(2, sp.getImage());
				createpd.setDouble(3, sp.getPrice());
				createpd.setString(4, sp.getDescription());
				createpd.setString(5, sp.getCategory());
				createpd.setString(6, sp.getStatus());
				
				int number = createpd.executeUpdate();
				if (number > 0) {
					result = true;
				}

				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}

			return result;
		}
	 
 	public boolean EditProduct(String id, ThongTinSanPham sp) {
 		PreparedStatement editpd = null;
		boolean result = false;

		Connection con = new ConnectionPool().getConnection();
		try {

			editpd = con.prepareStatement("update product set name = ?,image = ?,price = ?," +
					"description = ?, category = ?, status = ? where id = ?");
			editpd.setString(1, sp.getName());
			editpd.setString(2, sp.getImage());
			editpd.setDouble(3, sp.getPrice());
			editpd.setString(4, sp.getDescription());
			editpd.setString(5, sp.getCategory());
			editpd.setString(6, sp.getStatus());
			editpd.setString(7, id);
			int number = editpd.executeUpdate();
			if (number > 0) {
				result = true;
			}

			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return result;
	}
	 
 	public boolean DeleteProduct(String id) {
 		PreparedStatement delpd = null;
		boolean result = false;

		Connection con = new ConnectionPool().getConnection();
		try {
			
			delpd = con.prepareStatement("DELETE FROM product WHERE id = ?");
			delpd.setString(1, id);
			int number = delpd.executeUpdate();
			if (number > 0) {
				result = true;
			}

			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return result;
	}
 	
 	
 	public boolean DeleteAllProduct() {
 		PreparedStatement delallpd = null;
		boolean result = false;

		Connection con = new ConnectionPool().getConnection();
		try {
			
			delallpd = con.prepareStatement("DELETE FROM product");

			int number = delallpd.executeUpdate();
			if (number > 0) {
				result = true;
			}

			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return result;
	}
 	
 	public boolean DeleteAllProductSO() {
 		PreparedStatement delallpd = null;
		boolean result = false;
		String dk = "HetHang";
		Connection con = new ConnectionPool().getConnection();
		try {
			
			delallpd = con.prepareStatement("DELETE FROM product where status = ?");
			delallpd.setString(1, dk);
			int number = delallpd.executeUpdate();
			if (number > 0) {
				result = true;
			}

			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return result;
	}
 	
 	
	  public static void main(String[] args) throws SQLException {
	    	// new BillMod().createbill(new BillInfo("01/02/2000 20:19:02","LuanXT","LuanXt","luanxt",12,"luanxt"));
			ThongTinSanPham b = new ThongTinSanPham(1,null,"",10.0,"","","");
	    System.out.println(new ProductMods().CreateProduct(b));
//	    	int size = new BillMod().getBill("luanxt").size();
//	    	System.out.println(size);

	    	}
}
