package com.example.memosystem.activity;
/**
 *
 * 这是一个注册界面
 */

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 androidx.appcompat.app.AppCompatActivity;

import com.example.memosystem.MainActivity;
import com.example.memosystem.R;
import com.example.memosystem.dao.AdminDao;

public class RegisterActivity extends AppCompatActivity {

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


        EditText account = findViewById(R.id.register_account);//账号
        EditText pwd = findViewById(R.id.register_pwd);//密码
        EditText name = findViewById(R.id.register_name);//姓名
        RadioButton woman = findViewById(R.id.register_woman);//女
        RadioButton man = findViewById(R.id.register_man);//男
        man.setChecked(true);

        EditText phone = findViewById(R.id.register_phone);//手机号
        EditText age = findViewById(R.id.register_age);

        Button button = findViewById(R.id.register_button);//点击注册按钮
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String accountT=account.getText().toString().trim();
                String pwdT=pwd.getText().toString().trim();
                String nameT=name.getText().toString().trim();
                String phoneT=phone.getText().toString().trim();
                String ageT=age.getText().toString().trim();


                if(accountT.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入账号！", Toast.LENGTH_SHORT).show();
                }else if(pwdT.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入密码！", Toast.LENGTH_SHORT).show();
                }else if(nameT.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入姓名！", Toast.LENGTH_SHORT).show();
                }else if(phoneT.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入联系方式！", Toast.LENGTH_SHORT).show();
                }else if(ageT.isEmpty()){
                    Toast.makeText(RegisterActivity.this, "请输入年纪！", Toast.LENGTH_SHORT).show();
                }else{

                    String sex="女";
                    if(man.isChecked()){
                        sex="男";
                    }
                    AdminDao adminDao=new AdminDao();
                    int a= adminDao.registerAdmin(accountT,pwdT,nameT,sex,phoneT,ageT);
                    if(a==1){
                        Toast.makeText(RegisterActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(RegisterActivity.this, "注册失败,账号冲突！", Toast.LENGTH_SHORT).show();
                    }


                }





            }
        });


        Button Login = findViewById(R.id.register_to_login_button);//点击注册按钮 实现返回按钮
        Login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(RegisterActivity.this, "登录界面", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(RegisterActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });



    }
}