package com.example.map;

import android.content.Intent;
import android.content.SharedPreferences;
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.map.database.UserDBHelper;
import com.example.map.enity.User;

public class LogOut extends AppCompatActivity implements View.OnClickListener{

    private EditText username,userpassword;
    private String userName;
    private UserDBHelper mHelper;
    private SharedPreferences.Editor editor;

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

        initView();//初始化界面
        UserDBHelper userDBHelper = new UserDBHelper(this);

        SharedPreferences sp = getSharedPreferences("user_mes", MODE_PRIVATE);
        editor = sp.edit();
        if(sp.getBoolean("flag",false)){
            String user_read = sp.getString("user","");
            String psw_read = sp.getString("psw","");
            username.setText(user_read);
            userpassword.setText(psw_read);
        }
    }
    private void initView() {
        //初始化控件
        username = findViewById(R.id.zhuxiao_name);
        userpassword = findViewById(R.id.zhuxiao_passwd);
        Button btn_Login = findViewById(R.id.zhuxiao_queding);
        Button tv_register = findViewById(R.id.zhuxiao_fanhui);
        //设置点击事件监听器
        btn_Login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
    }

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

    @Override
    public void onClick(View v) {
        String name=username.getText().toString().trim();
        String password_=userpassword.getText().toString().trim();
        User user=null;
        switch (v.getId()){
            case R.id.zhuxiao_fanhui:
                Intent intent=new Intent(LogOut.this, UserScreen.class);
                startActivity(intent);
                finish();
                break;

            case R.id.zhuxiao_queding:
                user=new User(name,password_);
                if(name.isEmpty()&&password_.isEmpty()){
                    Toast.makeText(this,"请输入正确的用户名或密码",Toast.LENGTH_SHORT).show();
                }
                else{
                    if (mHelper.delete(user)>0){
                        Toast.makeText(this, "课表删除成功", Toast.LENGTH_SHORT).show();
                        Intent intent_=new Intent(LogOut.this,LoginScreen.class);
                        startActivity(intent_);
                        finish();
                    }
                }

        }
    }
}