package com.example.map;

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

import androidx.appcompat.app.AppCompatActivity;

import com.example.map.database.UserDBHelper;
import com.example.map.enity.User;
import com.example.map.util.ToastUtil;

public class Register extends AppCompatActivity implements View.OnClickListener{

    private EditText et_user_name;//用户账号
    private EditText password;//用户密码
    private UserDBHelper mHelper;

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

        et_user_name = findViewById(R.id.register_username);
        password = findViewById(R.id.register_password);
        //跳转事件
        Button button =findViewById(R.id.back_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(getApplicationContext(),LoginScreen.class);//跳转的目的地
                startActivity(intent);
                finish();
            }
        });

        //监听事件
        findViewById(R.id.register2_button).setOnClickListener(this);
        findViewById(R.id.register_username).setOnClickListener(this);
        findViewById(R.id.register_password).setOnClickListener(this);
    }

    protected void onStart() {
        super.onStart();
        // 获得数据库帮助器的实例
        mHelper = UserDBHelper.getInstance(this);
        // 打开数据库帮助器的读写连接
        mHelper.openWriteLink();
        mHelper.openReadLink();
    }

    @Override
    public void onClick(View v) {
        String name=et_user_name.getText().toString().trim();
        String password_=password.getText().toString().trim();
        User user = null;
        switch (v.getId()){
            case R.id.register2_button:
                user=new User(name, password_);
                if (mHelper.insert(user) > 0) {
                    ToastUtil.show(this, "注册成功");
                    Intent intent=new Intent(Register.this,LoginScreen.class);
                    startActivity(intent);
                    finish();
                }
                break;
        }
    }

//    protected void onStop() {
//        super.onStop();
//        // 关闭数据库连接
//        mHelper.closeLink();
//    }
}