
package model;

import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author user
 */
public class BillMod {


	   public boolean createbill(BillInfo bill) {
		   PreparedStatement createbill = null;
			boolean result = false;

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

				createbill = con.prepareStatement("insert into bill(date, user, idproduct, payinfo, total, status) values(?, ?, ?, ?, ?, ?)");
				createbill.setString(1, bill.getDate());
				createbill.setString(2, bill.getUser());
				createbill.setString(3, bill.getIdproduct());
				createbill.setString(4, bill.getPayinfo());
				createbill.setDouble(5, bill.getTotal());
				createbill.setString(6, bill.getStatus());
				
				int number = createbill.executeUpdate();
				if (number > 0) {
					result = true;
				}

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

			return result;
		}

    public List<BillInfo> getBill(String user) throws SQLException {
    	 PreparedStatement getbill = null;
       List<BillInfo> result = new ArrayList<BillInfo>();
       Connection con = new ConnectionPool().getConnection();

        getbill = con.prepareStatement("select * from bill where user = ?");
        getbill.setString(1, user);
        ResultSet rs = getbill.executeQuery();
        
        while (rs.next()) {
        	String id = rs.getString(1);
        	String date = rs.getString(2);
            String usern= rs.getString(3);
            String idproduct = rs.getString(4);
            String payinfo = rs.getString(5);
            double total = rs.getDouble(6);
            String status = rs.getString(7);

            BillInfo bill = new BillInfo(id,date,usern, idproduct, payinfo, total,status);
            result.add(bill);
        }

        con.close();
        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"));
		String a = "luanxt";
    System.out.println(new BillMod().getBill(a));
//    	int size = new BillMod().getBill("luanxt").size();
//    	System.out.println(size);

    	}
	}


    

