package com.example.memosystem.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.memosystem.MainActivity;
import com.example.memosystem.R;
import com.example.memosystem.activity.listen.ChangeListen;
import com.example.memosystem.activity.listen.TouchListen;
import com.example.memosystem.dao.AdminDao;

import java.util.Map;

public class MesActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mes);


        Intent intent=getIntent();
        String account=intent.getStringExtra("account");
        //需要查看个人信息

        Map<String, String> result = new AdminDao().showAccountMes(account);







        Toolbar toolbar= findViewById(R.id.toolbar_mes);
        this.setSupportActionBar(toolbar);
        //返回功能
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1=new Intent(MesActivity.this,ManageActivity.class);
                intent1.putExtra("account",account);
                startActivity(intent1);
            }
        });




        //姓名
        EditText name=findViewById(R.id.change_name);//姓名
        name.setText(result.get("name"));



        RadioButton man=findViewById(R.id.change_man);//男
        RadioButton woman=findViewById(R.id.change_woman);//女
        if(result.get("sex").equals("男")){
            man.setChecked(true);
        }else{
            woman.setChecked(true);
        }


        EditText age=findViewById(R.id.change_age);//男
        age.setText(result.get("age"));

        EditText phone=findViewById(R.id.change_phone);//男
        phone.setText(result.get("phone"));


        clearText(name);
        clearText(age);
        clearText(phone);



        Button change_pwd_button = findViewById(R.id.change_pwd_button);
        change_pwd_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String nameT=name.getText().toString().trim();
                String ageT=age.getText().toString().trim();
                String phoneT=phone.getText().toString().trim();

                if(nameT.isEmpty()){
                    Toast.makeText(MesActivity.this, "请输入姓名！", Toast.LENGTH_SHORT).show();
                }else if(phoneT.isEmpty()){
                    Toast.makeText(MesActivity.this, "请输入联系方式！", Toast.LENGTH_SHORT).show();
                }else if(ageT.isEmpty()){
                    Toast.makeText(MesActivity.this, "请输入年纪！", Toast.LENGTH_SHORT).show();
                }else{

                    String sex="女";
                    if(man.isChecked()){
                        sex="男";
                    }

                    int a=new AdminDao().changeMes(account,nameT,sex,phoneT,ageT);
                    if(a>=1){
                        Toast.makeText(MesActivity.this, "更改个人信息成功！", Toast.LENGTH_SHORT).show();
                    }
                    if(a==0){
                        Toast.makeText(MesActivity.this, "更改失败！", Toast.LENGTH_SHORT).show();
                    }
                    if(a==-1){
                        Toast.makeText(MesActivity.this, "未知错误！", Toast.LENGTH_SHORT).show();
                    }

                }

            }
        });
    }

    private void clearText(EditText text){



        text.addTextChangedListener(new ChangeListen(text));
        text.setOnTouchListener(new TouchListen( text));
        //初始X的状态
        text.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,0,0);

    }
}