package com.example.waoperations.Transference;

import java.sql.Date;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class CrossDockingDataBaseHelper {

    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "WMSCrsdck.db";
    private static final String TABLE_NAME = "CrsDck_Table";
    private static final String CROSSDOCKING_ID = "_id";
    private static final String PONUMBER = "ponumber";
    private static final String ITEMID = "articulo";
    private static final String QTY = "cantidad";
    private static final String TRANSDATE = "fechacreacion";
    private static final String INVENTLOCATIONIDTO = "bodegadestinoA";
    private static final String PURCHID = "pedidocompra";
    private static final String DATAAREAIDTO = "empresaA";
    private static final String STATUS = "Estado";
    private static final String INVENTTRANSID = "inventtransid";


    private ReceivingOpenHelper openHelper;
    private SQLiteDatabase database;

    public CrossDockingDataBaseHelper(Context context) {
        openHelper = new ReceivingOpenHelper(context);
        database = openHelper.getWritableDatabase();
    }

    public void saveCrsDckLine(String ponumber,String itemid,String qty,String transdate,String inventlocationidto,String purchid,String dataareaidto,String status,String inventtransid) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(PONUMBER, ponumber);
        contentValues.put(ITEMID,itemid);
        contentValues.put(QTY, qty);
        contentValues.put(TRANSDATE, transdate);
        contentValues.put(INVENTLOCATIONIDTO, inventlocationidto);
        contentValues.put(PURCHID, purchid);
        contentValues.put(DATAAREAIDTO, dataareaidto);
        contentValues.put(STATUS, status);
        contentValues.put(INVENTTRANSID, inventtransid);


        database.insert(TABLE_NAME, null, contentValues);
    }
    public void deleteAllData()
    {
        database.delete(TABLE_NAME, null, null);
    }
    public Cursor getAllCrsDckLines() {
        return database.rawQuery(
                "SELECT * FROM " + TABLE_NAME+"",
                null);
    }
    public Cursor getCrsDckAllLines() {
        return database.rawQuery(
                "SELECT * FROM " + TABLE_NAME+" ",
                null);
    }

    public Cursor getCrsDckLines(String purchid) {
        return database.rawQuery(
                "SELECT * FROM " + TABLE_NAME+" WHERE " +PURCHID+ " ='"
                        +purchid+"'",
                null);
    }

    /* public Cursor getRemainingShippingLines(String ponumber) {
         return database.rawQuery(
               "SELECT * FROM " + TABLE_NAME+" WHERE " +PONUMBER+ " ='"
               +ponumber+"' AND "+QTYRECEIVED+" ='0'",
               null);
     }*/
    public Cursor getCrsDckItem(String ponumber,String itemid) {
        return database.rawQuery(

                "SELECT * FROM " + TABLE_NAME+" WHERE " +PONUMBER+ " ='"
                        +ponumber+"' AND "+ITEMID+" ='"+itemid+"'",
                null);
    }
    /*public Cursor getShippingItemBarcode(String ponumber,String barcode) {
        return database.rawQuery(
             "SELECT * FROM " + TABLE_NAME+" WHERE " +PONUMBER+ " ='"
             +ponumber+"' AND "+ITEMBARCODE+" ='"+barcode+"'",
             null);
    } */
    public Cursor getItemByID(String ID) {
        String query= "SELECT * FROM "+TABLE_NAME+" where "+ CROSSDOCKING_ID+"='"+ID+"'";
        return database.rawQuery(query,null);
    }

    public Cursor getItemByIT(String ID) {
        String query= "SELECT * FROM "+TABLE_NAME+" where "+ INVENTTRANSID+"='"+ID+"'";
        return database.rawQuery(query,null);
    }

       /* public Cursor getItemDetail(String ponumber,String inventrans,String sku,String orderid) {
            String query= "SELECT * FROM "+TABLE_NAME+" where "+ PONUMBER+"='"+ponumber+"' AND "+ ITEMLINE+"='"+inventrans+"' AND "+ SKU+"='"+sku+"' AND "+ ORDERID+"='"+orderid+"'";
            return database.rawQuery(query,null);
            }*/

    public void UpdateCrsDckItem(String purchqty,String id) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(QTY , purchqty);
        //contentValues.put(SCANNED , "1");
        String strFilter = "_id=" + id;
        database.update(TABLE_NAME, contentValues, strFilter, null);
    }

    private class ReceivingOpenHelper extends SQLiteOpenHelper {

        public ReceivingOpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase database) {
            database.execSQL("CREATE TABLE " + TABLE_NAME + "("
                    + CROSSDOCKING_ID + " INTEGER PRIMARY KEY, "
                    + PONUMBER + " TEXT, "
                    + ITEMID + " TEXT, "
                    + QTY + " TEXT, "
                    + TRANSDATE + " TEXT, "
                    + INVENTLOCATIONIDTO + " TEXT, "
                    + PURCHID + " TEXT, "
                    + DATAAREAIDTO + " TEXT, "
                    + STATUS + " TEXT, "
                    + INVENTTRANSID +" TEXT)");


        }
        @Override
        public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
            database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + "");
            onCreate(database);
        }

    }

}
