package com.example.logistics.ui;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
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.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.logistics.R;
import com.example.logistics.manager.goodManager;

public class SearchUI extends Fragment implements View.OnClickListener{
    private Context mContext;
    private Button mButton;
    private EditText mPickUpCode;
    private EditText mPhoneNum;

    public SearchUI(){

    }
    @Nullable

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_fragment1,container,false);
        mButton = (Button)view.findViewById(R.id.Sea_Button);
        mPickUpCode = (EditText)view.findViewById(R.id.Sea_PickUpCode);
        mPhoneNum = (EditText)view.findViewById(R.id.Sea_PhoneNum);
        mButton.setOnClickListener(this);
        mContext = getActivity();
        return view;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.Sea_Button:
                    search(v);
                break;
        }
    }

    public void search(View view){

        new Thread(){
            @Override
            public void run(){
                goodManager goodManager = new goodManager();
                int msg = goodManager.search(mPickUpCode.getText().toString().trim(), mPhoneNum.getText().toString().trim(), getActivity());
                Log.e("Main", "msg");
                hand1.sendEmptyMessage(msg);
            }
        }.start();
    }

    @SuppressLint("HandlerLeak")
    final Handler hand1 = new Handler(){
        @Override
        public void handleMessage(Message msg){
            if(msg.what == 0){
                Toast.makeText(mContext, "搜索失败", Toast.LENGTH_SHORT).show();
            }else if(msg.what == 2){
                Toast.makeText(mContext, "手机尾号错误", Toast.LENGTH_SHORT).show();
            }else if(msg.what == 3){
                Toast.makeText(mContext, "取件码不存在", Toast.LENGTH_SHORT).show();
            }
        }
    };
}
