package ir.almahdi.sandogh_bsa_aza.setting.sal_mali;

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

import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

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

/**
 * A simple {@link Fragment} subclass.
 */
public class frg_set_sabt_vam extends DialogFragment {
    private static String javab="no";
    private EditText idvam,id_ozviat,id_chk_san,
            mab_vam,baz_vam,date_vam,tozihat;
    private Button btn_sabt_vam,btn_back;
    public frg_set_sabt_vam() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_frg_set_sabt_vam, container, false);
        //مقداردهی متغیرها
        idvam=view.findViewById(R.id.et_idvam);
        id_ozviat=view.findViewById(R.id.et_idozv);
        id_chk_san=view.findViewById(R.id.et_chkvam);
        mab_vam=view.findViewById(R.id.et_mab_vam);
        baz_vam=view.findViewById(R.id.et_baz_vam);
        date_vam=view.findViewById(R.id.et_tarikh_vam);
        tozihat=view.findViewById(R.id.et_tozih);
        btn_sabt_vam=view.findViewById(R.id.btn_sabt_vam);
        btn_back=view.findViewById(R.id.btn_goto_home1);
        btn_sabt_vam.setOnClickListener(v -> {
            String id_vam="pvn"+idvam.getText().toString();
            sabt_vam(id_vam,id_ozviat.getText().toString()
                    ,id_chk_san.getText().toString(),mab_vam.getText().toString()
                    ,baz_vam.getText().toString(),date_vam.getText().toString()
                    ,tozihat.getText().toString());

        });
        btn_back.setOnClickListener(v -> {
            this.dismiss();        });

        return view;
    }

    private void sabt_vam(String text, String text1, String text2, String text3, String text4, String text5, String text6) {
        String url="http://sandogh-bs.makkh.ir/setting_modir/sabt_vam/sabt_vam.php";
        final ProgressDialog pDialog;
        pDialog = new ProgressDialog(getContext());
        pDialog.setMessage("در حال ارسال اطلاعات...");
        pDialog.setCancelable(false);
        pDialog.show();
        Response.Listener<String> listener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                String pasokh = response.toLowerCase();
                pDialog.dismiss();
                if (pasokh.equals("ok")){
                    javab="ok";
                    pDialog.dismiss();

                }else{
                    Toast.makeText(getContext(), pasokh, Toast.LENGTH_SHORT).show();
                    pDialog.dismiss();

                }
                //

            }
        };

        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("idvam",text);
                Data.put("id_ozviat", text1);
                Data.put("id_chk_san",text2);
                Data.put("mab_vam", text3);
                Data.put("baz_vam",text4);
                Data.put("date_vam", text5);
                Data.put("tozihat",text6);
                return Data;
            }
        };

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

    }

}
