/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package function.database;

import function.Function;
import static function.database.AdminProcess.parseListOptionFromItemApp;
import static function.database.AdminProcess.parseListSizeFromItemApp;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import main.Main;
import object.billmanager.BillItem;
import object.tablemanager.Table;
import object.itemmanager.ProductApp;
import object.itemmanager.ItemApp;

/**
 *
 * @author Administrator
 */
public class StaffProcess {

    public static boolean addBill(java.sql.Connection connection, Table table) {
        String sql = "INSERT INTO hoa_don(total_money, time, id_ban) VALUES(?, ?, ?);";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            preparedStatement.setDouble(1, table.getTotalMoney());
            preparedStatement.setTimestamp(2, new Timestamp(Function.getCurrentMilis()));
            preparedStatement.setString(3, table.getIdTable());

            if (preparedStatement.executeUpdate() > 0) {
                ResultSet resultSet = preparedStatement.getGeneratedKeys();
                if (resultSet.next()) {
                    int id = resultSet.getInt(1);
                    int error = 0;
                    sql = "INSERT INTO ct_hoa_don(id_hd, id_sp, sizes, amount, total_money, note) VALUES(?, ?, ?, ?, ?, ?);";
                    for (BillItem billItem : table.getAlBillItems()) {
                        preparedStatement = connection.prepareStatement(sql);
                        preparedStatement.setInt(1, id);
                        preparedStatement.setString(2, billItem.getProductApp().getIdSP());
                        preparedStatement.setString(3, billItem.getSize().getName());
                        preparedStatement.setInt(4, billItem.getAmount());
                        preparedStatement.setDouble(5, billItem.getTotalMoney());
                        preparedStatement.setString(6, billItem.getNote());
                        error += preparedStatement.executeUpdate() == 0 ? 1 : 0;
                    }
                    return error == 0;
                }
            }
        } catch (SQLException e) {
        }
        return false;
    }

    //
    public static ArrayList<ProductApp> getArrayListProduct(java.sql.Connection connection, String idItem) {
        ArrayList<ProductApp> alProductApps = new ArrayList<>();
        String sql = "SELECT * FROM san_pham WHERE id_mh=? AND status_sp=1;";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, idItem);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                ProductApp productApp = new ProductApp();
                productApp.setIdSP(resultSet.getString("id_sp"));
                productApp.setNameSP(resultSet.getString("name_sp"));
                productApp.setPriceSP(resultSet.getInt("price_sp"));
                productApp.setActive(true);
                alProductApps.add(productApp);
            }
        } catch (SQLException ex) {
        }
        return alProductApps;
    }

    public static ArrayList<ItemApp> getArrayListItem(java.sql.Connection connection) {
        ArrayList<ItemApp> alItemApps = new ArrayList<>();
        String sql = "SELECT * FROM mat_hang WHERE status=1;";
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                ItemApp itemApp = new ItemApp();
                itemApp.setId(resultSet.getString("id_mh"));
                itemApp.setName(resultSet.getString("name"));
                itemApp.setListSize(parseListSizeFromItemApp(resultSet.getString("sizes")));
                itemApp.setListOption(parseListOptionFromItemApp(resultSet.getString("options")));
                itemApp.setActive(true);
                alItemApps.add(itemApp);
            }
        } catch (SQLException ex) {
        }
        return alItemApps;
    }
}
