package com.example.logistics.ui;

import com.example.logistics.R;
import com.example.logistics.dao.userDao;
import com.example.logistics.entity.User;
import com.example.logistics.tools.MD5Utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;


public class MainActivity extends Activity {

    private int islogin = 0;
    // 调用Actvity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button loginButton = (Button) this.findViewById(R.id.LoginButton);
        Button signUpButton = (Button) this.findViewById(R.id.SignUpButton);

        loginButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        login(v);
                    }
                }
        );
        signUpButton.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
                        startActivity(intent);
                    }
                }
        );

    }

    public void login(View view){
        EditText userName = (EditText) this.findViewById(R.id.UserNameEdit);
        EditText passWord = (EditText) this.findViewById(R.id.PassWordEdit);

        new Thread(){
            @Override
            public void run(){
                userDao userDao = new userDao();
                int msg = userDao.login(userName.getText().toString().trim(), passWord.getText().toString().trim());
                Log.e("MAin", "msg");
                hand1.sendEmptyMessage(msg);
                if(msg == 1){
                    User owner = userDao.findUser(userName.getText().toString().trim());
                    Intent intent = new Intent(MainActivity.this, MenuActivity.class);
                    intent.putExtra("user", owner.getPhoneNum());
                    startActivity(intent);
                    finish();
                }
            }
        }.start();
    }

    @SuppressLint("HandlerLeak")
    final Handler hand1 = new Handler(){
        @Override
        public void handleMessage(Message msg){
            if(msg.what == 0){
                Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_SHORT).show();
            }else if(msg.what == 1){
                Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                islogin = 1;
            }else if(msg.what == 2){
                Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT).show();
            }else if(msg.what == 3){
                Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_SHORT).show();
            }
        }
    };

}