package com.example.leudaemialikeme.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.leudaemialikeme.Adapter.EventAdapter;
import com.example.leudaemialikeme.Model.Event;
import com.example.leudaemialikeme.R;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EventActivity extends BaseActivity {

//    private List<Event> mData = new ArrayList<>();
    private List<Map<String,String>> contentList = new ArrayList<Map<String,String>>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);

        ImageView addContent = (ImageView) findViewById(R.id.add_event);
        addContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(EventActivity.this,AddEventActivity.class);
                startActivity(intent);
            }
        });

        ImageView back_my = findViewById(R.id.back_my);
        back_my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(EventActivity.this,MainActivity.class);
//                startActivity(intent);
                finish();
            }
        });
//        getData();//获得数据
//        RecyclerView recycleView = (RecyclerView) findViewById(R.id.eventList);//获得视图
//        LinearLayoutManager layoutManager;
//        layoutManager = new LinearLayoutManager(this);
//        recycleView.setLayoutManager(layoutManager);//建立线性布局
//        EventAdapter adapter = new EventAdapter(mData);//创建适配器
//        recycleView.setAdapter(adapter);//将视图与适配器连接起来
    }

    @Override
    protected void onStart(){
        super.onStart();
        contentList.clear();
        initContent();
        RecyclerView recycleView = (RecyclerView) findViewById(R.id.eventList);//获得视图
        LinearLayoutManager layoutManager;
        layoutManager = new LinearLayoutManager(this);
        recycleView.setLayoutManager(layoutManager);//建立线性布局
        EventAdapter adapter = new EventAdapter(contentList);//创建适配器
        recycleView.setAdapter(adapter);//将视图与适配器连接起来
        adapter.notifyDataSetChanged();
    }

    private void initContent() {
        List<Event> events = LitePal.order("id desc").find(Event.class);
        for(Event event:events){
            String event_day = event.getEvent_day();
            String event_month = event.getEvent_month();
            String event_info = event.getEvent_info();
            String event_place = event.getEvent_place();
            String event_doctor_name = event.getEvent_doctor_name();
            String event_time = event.getEvent_time();
            String event_create_time = event.getEvent_create_time();
            Map<String,String> map = new HashMap<String,String>();
            map.put("event_day",event_day);
            map.put("event_month",event_month);
            map.put("event_info",event_info);
            map.put("event_place",event_place);
            map.put("event_doctor_name",event_doctor_name);
            map.put("event_time",event_time);
            map.put("event_create_time",event_create_time);
            contentList.add(map);
        }
    }
//    private void getData() {
//        Event event1 = new Event("04","10月","第三次化疗","08:00");
//        mData.add(event1);
//        Event event2 = new Event("11","5月","检查","09:00");
//        mData.add(event2);
//    }
}
