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.AbsListView;
import android.widget.Button;
import android.widget.EditText;
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;


public class ChangePwdActivity extends AppCompatActivity {

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

        Intent intent=getIntent();
        String account=intent.getStringExtra("account");

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





        EditText one=findViewById(R.id.change_pwd_one);
        one.addTextChangedListener(new ChangeListen(one));
        EditText two=findViewById(R.id.change_pwd_two);
        two.addTextChangedListener(new ChangeListen(two));


        one.setOnTouchListener(new TouchListen(one));
        two.setOnTouchListener(new TouchListen(two));

        //初始X的状态
        one.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,0,0);
        two.setCompoundDrawablesRelativeWithIntrinsicBounds(0,0,0,0);



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

                String ontT=one.getText().toString().trim();
                String twoT=two.getText().toString().trim();
                if(ontT.isEmpty()){
                    Toast.makeText(ChangePwdActivity.this, "请输入密码！", Toast.LENGTH_SHORT).show();
                }else if(twoT.isEmpty()){
                    Toast.makeText(ChangePwdActivity.this, "请输入确认密码！", Toast.LENGTH_SHORT).show();
                }else if(!ontT.equals(twoT)){
                    Toast.makeText(ChangePwdActivity.this, "两次密码输入不一致！", Toast.LENGTH_SHORT).show();
                }else{

                    AdminDao adminDao=new AdminDao();
                    int a=adminDao.changePassword(account,ontT);
                    if(a==0){
                        Toast.makeText(ChangePwdActivity.this, "更改失败！", Toast.LENGTH_SHORT).show();
                    }else
                    if(a>=1){
                        Toast.makeText(ChangePwdActivity.this, "修改密码成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(ChangePwdActivity.this, "未知错误", Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });



        //写返回功能



    }




}

