package com.qhclh.ytzh.index;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseFragment;
import com.qhclh.ytzh.bean.NewsBean;
import com.qhclh.ytzh.index.planorder.OrderActivity;
import com.qhclh.ytzh.index.planorder.WarningActivity;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2017/11/28.
 */
///< 首页消息
public class IndexFragment extends BaseFragment {
    @BindView(R.id.lv_index)
    ListView lv_index;
    private NewsAdapter newsAdapter;
    private List<NewsBean> newsBeanList;

    @Override
    protected int setLayout() {
        return R.layout.fragment_index;
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initOper() {
        lv_index.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (l % 2 == 0) {
                    startActivity(new Intent(getActivity(), OrderActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), WarningActivity.class));
                }
            }
        });

        lv_index.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, final long l) {
                new AlertDialog.Builder(getActivity())
                        .setTitle("是否删除")
                        .setIcon(R.mipmap.ic_launcher)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                DataSupport.delete(NewsBean.class, l);
                                datas();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        })
                        .show();
                return true;
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        datas();
    }

    private void datas() {
        newsBeanList = DataSupport.select("id", "url", "title", "content", "time").order("time desc").find(NewsBean.class);
        newsAdapter = new NewsAdapter(getActivity(), newsBeanList);
        lv_index.setAdapter(newsAdapter);
    }
}
