package com.example.memosystem;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.memosystem.activity.AddActivity;
import com.example.memosystem.activity.ChangePwdActivity;
import com.example.memosystem.activity.ManageActivity;
import com.example.memosystem.activity.MesActivity;
import com.example.memosystem.activity.RegisterActivity;
import com.example.memosystem.dao.AdminDao;
import com.example.memosystem.util.DBUtil;

public class MainActivity extends AppCompatActivity {

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


        //这段代码是连接数据库的
        DBUtil dbUtil=new DBUtil(MainActivity.this);
        SQLiteDatabase db=dbUtil.getWritableDatabase();
        DBUtil.db=db;
        //------------------------

        
        //去注册界面
        Button to_reg=findViewById(R.id.register_button);
        to_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, "注册界面", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
        //登录按钮
        EditText ac=findViewById(R.id.login_account);
        EditText pwd=findViewById(R.id.login_pwd);


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

                String acT=ac.getText().toString().trim();
                String pwdT=pwd.getText().toString().trim();
                if(acT.isEmpty()){
                    Toast.makeText(MainActivity.this, "请输入账号！", Toast.LENGTH_SHORT).show();
                }else if(pwdT.isEmpty()) {
                    Toast.makeText(MainActivity.this, "请输入密码！", Toast.LENGTH_SHORT).show();
                }else{

                    AdminDao adminDao=new AdminDao();
                    int a=adminDao.loginAdmin(acT,pwdT);
                    if(a==0){
                        Toast.makeText(MainActivity.this, "账号不存在或者密码错误！", Toast.LENGTH_SHORT).show();

                    }else{
                        Intent intent=new Intent(MainActivity.this, ManageActivity.class);
                        intent.putExtra("account",acT);
                        startActivity(intent);
                        Toast.makeText(MainActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                    }


                }


            }
        });





    }
}