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;
import ir.hamsaa.persiandatepicker.PersianDatePickerDialog;

/**
 * A simple {@link Fragment} subclass.
 */
public class frg_sett_sal_mali extends DialogFragment  {
private EditText mab_hozv,et_mab_moj_avalsal,date_sal,idsal,name_sal;
    static String javab="no";
public frg_sett_sal_mali() {
        // 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_sett_sal_mali, container, false);
       //مقداردهی متغیرها

        idsal=view.findViewById(R.id.et_idsal);
        name_sal=view.findViewById(R.id.et_name_sal);
        Button btn_back;
        btn_back=view.findViewById(R.id.btn_goto_home1);
        mab_hozv=view.findViewById(R.id.et_mab_hozv);
        et_mab_moj_avalsal=view.findViewById(R.id.et_mab_moj_avalsal);
        date_sal=view.findViewById(R.id.et_date_sal);

        btn_back.setOnClickListener(v -> {
            this.dismiss();
        });
      // سه رقم سه رقم جداکردن ارقام
      //  mab_hozv.addTextChangedListener(new numbertodecimal(mab_hozv));
       // et_mab_moj_avalsal.addTextChangedListener(new numbertodecimal(et_mab_moj_avalsal));
        PersianDatePickerDialog picker = new PersianDatePickerDialog(getContext());
        Button btn_dp=view.findViewById(R.id.btn_dp);
//            btn_dp.setOnClickListener(v -> {
//
//            PersianDatePickerDialog picker2 = new PersianDatePickerDialog(getContext());
//            picker2.setPositiveButtonString("باشه");
//            picker2.setNegativeButton("بیخیال");
//            picker2.setActionTextColor(Color.GRAY);
//            picker2.setListener(new Listener() {
//                @SuppressLint({"DefaultLocale", "SetTextI18n"})
//                @Override
//                public void onDateSelected(PersianCalendar persianCalendar) {
//                    Toast.makeText(getContext(), persianCalendar.getPersianYear() + "/" + persianCalendar.getPersianMonth() + "/" + persianCalendar.getPersianDay(), Toast.LENGTH_SHORT).show();
//                    date_sal.setText(persianCalendar.getPersianYear() + "/" + persianCalendar.getPersianMonth() + "/" + persianCalendar.getPersianDay());
//                }
//
//                @Override
//                public void onDisimised() {
//
//                }
//
//            });//.setTypeFace(iran)
//
//            picker.show();
//
//        });
        Button btn_sabt_salmali = view.findViewById(R.id.btn_sabt_salmali);
            btn_sabt_salmali.setOnClickListener(v -> {

                sabt_salmali(idsal.getText().toString(),name_sal.getText().toString(),
                        mab_hozv.getText().toString(),date_sal.getText().toString(),
                        et_mab_moj_avalsal.getText().toString());
                if (javab.equals("ok")){
                    Toast.makeText(getContext(), "با موفقیت ثبت گردید", Toast.LENGTH_LONG).show();
                    onStop();
                }});


     return view;

    }
    private void sabt_salmali(String text, String text1, String text2, String text3, String text4) {
        String url="http://sandogh-bs.makkh.ir/setting_modir/salmali/sabt_salmali.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";

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

                }
            };

            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("idsal",text);
                    Data.put("namesal", text1);
                    Data.put("mhozv",text2);
                    Data.put("date", text3);
                    Data.put("mavalsal",text4);
                    return Data;
                }
            };

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

        }


}
