package com.example.waoperations.Stats;

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 StatsDatabaseHelper {

        private static final int DATABASE_VERSION = 2;
        private static final String DATABASE_NAME = "WMSStats.db";
        private static final String TABLE_NAME = "StatsList_Table";
        private static final String STATS_ID = "_id";
        private static final String PONUMBER = "usercode";
        private static final String NAME = "nombre";
        private static final String LASTDATE = "ultimafecha";
        private static final String LASTOPERATION = "lastop";
        private static final String LASTPO = "lastpo";
        private static final String COMPLETED = "completed";
        private static final String TOTAL = "total";
       

        
        private ContactOpenHelper openHelper;
        private SQLiteDatabase database;

        public StatsDatabaseHelper(Context context) {
                openHelper = new ContactOpenHelper(context);
                database = openHelper.getWritableDatabase();
        }
        
        public void saveStatsLine(String UID,String name,String lastdate,String lastop,String lastpo,String completed,String total) {
        	    ContentValues contentValues = new ContentValues();
                contentValues.put(PONUMBER, UID);
                contentValues.put(NAME, name);
                contentValues.put(LASTDATE, lastdate);
                contentValues.put(LASTOPERATION, lastop);
                contentValues.put(LASTPO, lastpo);
                contentValues.put(COMPLETED, completed);
                contentValues.put(TOTAL, total);
                database.insert(TABLE_NAME, null, contentValues);
                
        }
        public void deleteAllData()
        {
                 database.delete(TABLE_NAME, null, null);
        }
     
        public Cursor getAllPickLines() {
                return database.rawQuery(
                                "SELECT * FROM " + TABLE_NAME+"", 
                                null);
        } 
   
        public Cursor getPickingLines(String ponumber) {
            return database.rawQuery(
                            "SELECT * FROM " + TABLE_NAME+" WHERE " +PONUMBER+ " ='"
                            	     +ponumber+"'",
                            null);
        }  
   
    
        public void UpdateScannedAmountItem(String completed,String id) {
    	    ContentValues contentValues = new ContentValues();
            contentValues.put(COMPLETED , completed);
            String strFilter = "_id=" + id;
            database.update(TABLE_NAME, contentValues, strFilter, null);    
        }
     
        
      
   
        public Cursor getItemByID(String ID) {
            String query= "SELECT * FROM "+TABLE_NAME+" where "+ STATS_ID+"='"+ID+"'";
            return database.rawQuery(query,null);
            }
     
        
     
     
        
        private class ContactOpenHelper extends SQLiteOpenHelper {

                public ContactOpenHelper(Context context) {
                        super(context, DATABASE_NAME, null, DATABASE_VERSION);
                }
                
                @Override
                public void onCreate(SQLiteDatabase database) {
                        database.execSQL("CREATE TABLE " + TABLE_NAME + "("
                                        + STATS_ID + " INTEGER PRIMARY KEY, "
                                        + PONUMBER + " TEXT, "
                                        + NAME + " TEXT, "
                                        + LASTDATE + " TEXT, "
                                        + LASTOPERATION + " TEXT, "
                                        + LASTPO + " TEXT, "
                                        + TOTAL + " TEXT, "
                                        + COMPLETED + " TEXT)");
                        
              
                        

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

        }

}

