package mjkarbasian.moshtarimadar;

import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import mjkarbasian.moshtarimadar.Data.KasebContract;

/**
 * Created by family on 10/19/2016.
 */
public class CostInsert extends Fragment {
    private static final String LOG_TAG = CostInsert.class.getSimpleName() ;
    EditText costName;
    EditText costCode;
    EditText costAmount;
    Spinner costType;
    EditText costDate;
    EditText costDescription;
    ContentValues costValues = new ContentValues();
    private Uri insertUri;

    public CostInsert() {
        setHasOptionsMenu(true);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.fragment_cost_insert, container, false);

         costType = (Spinner) rootView.findViewById(R.id.input_cost_type_spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
                R.array.cost_types, android.R.layout.simple_spinner_item);
        costName = (EditText)rootView.findViewById(R.id.input_cost_name);
        costCode = (EditText)rootView.findViewById(R.id.input_cost_code);
        costAmount = (EditText)rootView.findViewById(R.id.input_cost_amount);
        costDate = (EditText)rootView.findViewById(R.id.input_cost_date);
        costDescription = (EditText)rootView.findViewById(R.id.input_cost_description);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        costType.setAdapter(adapter);

        return rootView;
    }

    @Override
    public void onCreateOptionsMenu(
            Menu menu, MenuInflater inflater) {
        menu.removeItem(R.id.sort_button);
        menu.removeItem(R.id.search_button);
        inflater.inflate(R.menu.fragments_for_insert, menu);
    }
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.save_inputs:{
                costValues.put(KasebContract.Costs.COLUMN_COST_NAME,costName.getText().toString());
                costValues.put(KasebContract.Costs.COLUMN_COST_CODE,costCode.getText().toString());
                costValues.put(KasebContract.Costs.COLUMN_AMOUNT,costAmount.getText().toString());
                costValues.put(KasebContract.Costs.COLUMN_DATE,costDate.getText().toString());
                costValues.put(KasebContract.Costs.COLUMN_DESCRIPTION,costDescription.getText().toString());
                costValues.put(KasebContract.Costs.COLUMN_COST_TYPE_ID,costType.getSelectedItemPosition());
               insertUri =getActivity().getContentResolver().insert(
                       KasebContract.Costs.CONTENT_URI,
                        costValues
                );
                //region disabling edit
                costName.setEnabled(false);
                costCode.setEnabled(false);
                costAmount.setEnabled(false);
                costDescription.setEnabled(false);
                costType.setEnabled(false);
                costDate.setEnabled(false);
                //just a message to show everything are under control
                Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.msg_insert_succeed),
                        Toast.LENGTH_LONG).show();
                
                checkForValidity();
                backToLastPage();

                break;
            }

        }
        return super.onOptionsItemSelected(item);
    }

    // this method check the validation and correct entries. its check fill first and then check the validation rules.
    private void checkForValidity() {
    }

    // this method back to the activity view. this must be a utility method.
    private void backToLastPage() {
        getFragmentManager().popBackStackImmediate();
    }
}


@@ -684,17 +684,16 @@
 leName()
- 
 ;%0A    Ed
@@ -1123,16 +1123,17 @@
 ceState)
+ 
 %7B%0A      
@@ -1217,18 +1217,16 @@
 false);%0A
-%0A 
         
@@ -1487,32 +1487,33 @@
 ame = (EditText)
+ 
 rootView.findVie
@@ -1562,32 +1562,33 @@
 ode = (EditText)
+ 
 rootView.findVie
@@ -1639,32 +1639,33 @@
 unt = (EditText)
+ 
 rootView.findVie
@@ -1716,32 +1716,33 @@
 ate = (EditText)
+ 
 rootView.findVie
@@ -1806,16 +1806,17 @@
 ditText)
+ 
 rootView
@@ -1855,24 +1855,25 @@
 scription);%0A
+%0A
         adap
@@ -2011,32 +2011,263 @@
 ootView;%0A    %7D%0A%0A
+    private void clearEditText() %7B%0A        costName.getText().clear();%0A        costCode.getText().clear();%0A        costDate.getText().clear();%0A        costDescription.getText().clear();%0A        costAmount.getText().clear();%0A    %7D%0A%0A
     @Override%0A  
@@ -2500,24 +2500,25 @@
 enu);%0A    %7D%0A
+%0A
     public b
@@ -2633,16 +2633,17 @@
 _inputs:
+ 
 %7B%0A      
@@ -2704,16 +2704,17 @@
 ST_NAME,
+ 
 costName
@@ -2805,16 +2805,17 @@
 ST_CODE,
+ 
 costCode
@@ -2903,16 +2903,17 @@
 _AMOUNT,
+ 
 costAmou
@@ -3001,16 +3001,17 @@
 MN_DATE,
+ 
 costDate
@@ -3104,16 +3104,17 @@
 RIPTION,
+ 
 costDesc
@@ -3215,16 +3215,17 @@
 TYPE_ID,
+ 
 costType
@@ -3268,16 +3268,17 @@
         
+ 
 insertUr
@@ -3280,16 +3280,17 @@
 ertUri =
+ 
 getActiv
@@ -3347,16 +3347,17 @@
         
+ 
 KasebCon
@@ -3435,322 +3435,9 @@
     
- 
-);%0A                //region disabling edit%0A                costName.setEnabled(false);%0A                costCode.setEnabled(false);%0A                costAmount.setEnabled(false);%0A                costDescription.setEnabled(false);%0A                costType.setEnabled(false);%0A                costDate.setEnabled(false
+ 
 );%0A 
@@ -3671,32 +3671,16 @@
 show();%0A
-                
 %0A       
@@ -4121,24 +4121,49 @@
 astPage() %7B%0A
+        clearEditText();%0A
         getF

clear preinserted cost data,most add to other insert fragment