package hf.lskj.com.carstockcount.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import hf.lskj.com.carstockcount.MyApplication;
import hf.lskj.com.carstockcount.R;
import hf.lskj.com.carstockcount.activity.CarListActivity;
import hf.lskj.com.carstockcount.activity.RecordListActivity;
import hf.lskj.com.carstockcount.activity.LoginActivity;
import hf.lskj.com.carstockcount.db.User;
import hf.lskj.com.carstockcount.db.UserDaoUtils;


public class MeFragment extends Fragment {


    @Bind(R.id.tv_realName)
    TextView tvRealName;
    @Bind(R.id.tv_role)
    TextView tvRole;
    @Bind(R.id.tv_loginName)
    TextView tvLoginName;
    @Bind(R.id.layout_count_records)
    RelativeLayout layoutCountRecords;
    @Bind(R.id.layout_cars)
    RelativeLayout layoutCars;
    @Bind(R.id.bt_exit)
    Button btExit;
    @Bind(R.id.tv_cars_title)
    TextView tvCarsTitle;

    public MeFragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_me, container, false);
        ButterKnife.bind(this, rootView);
        tvRole.setText(MyApplication.currentUser.getRole());
        tvLoginName.setText(MyApplication.currentUser.getLoginId());
        tvRealName.setText(MyApplication.currentUser.getRealName());
        tvCarsTitle.setText(MyApplication.currentUser.getStoreName() == null ?
                "库存车辆" : MyApplication.currentUser.getStoreName() + "库存车辆");
        setListener();
        return rootView;
    }

    private void setListener() {
        btExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UserDaoUtils userDaoUtils = new UserDaoUtils(getActivity());
                User current = userDaoUtils.loadCurrent();
                current.setCurrent(false);
                userDaoUtils.update(current);
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                getActivity().finish();
            }
        });
        layoutCountRecords.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), RecordListActivity.class));
            }
        });
        layoutCars.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), CarListActivity.class));
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
