package ir.almahdi.sandogh_bsa_aza.Tarakonesh_ozv;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ir.almahdi.sandogh_bsa_aza.Class.MySingleton;
import ir.almahdi.sandogh_bsa_aza.Global_var;
import ir.almahdi.sandogh_bsa_aza.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class tarako_karbar_frg extends Fragment {
    public tarako_karbar_frg() {
        // Required empty public constructor
    }
TextView txt_mab;
    ProgressDialog progressDialog;
  List<md_tarakonesh_ozv> myVamList ;
    public RecyclerView rc_tarako_ozv;
    tarakoesh_adapter adapter_mos;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view= inflater.inflate(R.layout.fragment_frg_tarakonesh_ozv, container, false);
        myVamList=new ArrayList<>();

        rc_tarako_ozv = view. findViewById(R.id.rc_tarako_ozv);//set the Layout manager
        adapter_mos = new tarakoesh_adapter(tarako_karbar_frg.this,myVamList);
        LinearLayoutManager linear = new LinearLayoutManager(getActivity());
        rc_tarako_ozv.setLayoutManager(linear);
        rc_tarako_ozv.setHasFixedSize(true);
        rc_tarako_ozv.setAdapter(adapter_mos);
        get_tarakonesh_ozv();
        txt_mab=view.findViewById(R.id.txt_mab_tarak);
      //  txt_mab.setText(Global_var.splitDigits((1000000)));
            return view;

    }



    private void  get_tarakonesh_ozv(){
        final ProgressDialog pDialog;
        pDialog = new ProgressDialog(getContext());
        pDialog.setMessage("در حال دریافت اطلاعات...");
        pDialog.setCancelable(false);
        pDialog.show();
        final String url = "http://sandogh-bs.makkh.ir/sel_tarakonesh_ozv/read_tarakonesh_ozv.php";

        Response.Listener<String> listener = new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                //  daryaft etelaat
          // Toast.makeText(getContext(), "دریافت ازسرور"+response, Toast.LENGTH_LONG).show();
                if (!response.equals("no")) {
                    Gson gson = new Gson();
                    md_tarakonesh_ozv[] tarkonesh_karbar = gson.fromJson(response, md_tarakonesh_ozv[].class);
                    // Toast.makeText(getApplicationContext(), response+"111", Toast.LENGTH_LONG).show();
                    for (md_tarakonesh_ozv one_tarak_ozv : tarkonesh_karbar) {
                        myVamList.add(new md_tarakonesh_ozv(
                                one_tarak_ozv.getId_ozviat(), one_tarak_ozv.getId_order(),
                                one_tarak_ozv.getAmount(), one_tarak_ozv.getTrack_id(), one_tarak_ozv.getDate()));
                        //  Toast.makeText(getContext(), one_tarak_ozv.getId_ozviat(), Toast.LENGTH_LONG).show();

                        //  s+=vam_karbars[i].toString();
                    }
                }
                else{
                    myVamList.add(new md_tarakonesh_ozv("0","0","0","0","0"));
                }
//
//                //   Toast.makeText(getApplicationContext(), Global_var.gkol_id_ozviat.get(i[0]), Toast.LENGTH_LONG).show();
//                //  Toast.makeText(getContext(), Global_var.sabad_pardakht.toString(), Toast.LENGTH_LONG).show();
//                rc_res_add_otherozv.setHasFixedSize(true);
                adapter_mos.notifyDataSetChanged();
                pDialog.dismiss();
                //Global_var.numLoad_Vamozv+=1;
            }




        };
        Response.ErrorListener errorListener = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getContext(), "خطا در اتصال به سرور یا اینترنت", Toast.LENGTH_SHORT).show();

                pDialog.dismiss();
            }
        };

        StringRequest request = new StringRequest(Request.Method.POST, url, listener, errorListener) {
            @Override

            protected Map<String, String> getParams() {
                Map<String,String> Data=new HashMap<>();
                Data.put("id_ozviat",Global_var.gid_ozviat);
                return Data;
            }
        };

        MySingleton.getInstance(getContext()).addToRequestQueue(request);

    }

}
