package com.qhclh.ytzh.index.planorder;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.bean.NewsBean;

import org.litepal.crud.DataSupport;

import java.util.List;

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2017/11/30.
 */
///< 预警详情页
public class WarningActivity extends BaseActivity{
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_toolbar_title)
    TextView tv_toolbar_title;
    @BindView(R.id.lv_warning)
    ListView lv_warning;

    private WarningAdapter warningAdapter;
    private String type;

    private List<NewsBean> newsBeanList;
    @Override
    protected int setLayoutId() {
        return R.layout.act_warning;
    }

    @Override
    protected void initView() {
        initToolbar(mToolbar, "", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        type =getIntent().getStringExtra("type");
        if ("01".equals(type)) {
            tv_toolbar_title.setText("通知通告");
        }else if ("02".equals(type)){
            tv_toolbar_title.setText("报警信息");
        }else if ("03".equals(type)){
            tv_toolbar_title.setText("鸡苗运输信息");
        }else if ("04".equals(type)){
            tv_toolbar_title.setText("胴体验收");
        }
        getDatas();
    }

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

                return true;
            }
        });
    }

    private void getDatas(){
        ContentValues values = new ContentValues();
        values.put("red", 0);
        DataSupport.updateAll(NewsBean.class, values, "type = ?",type);
        newsBeanList = DataSupport.select("id", "url", "title", "content", "time", "type","red").where("type = ?",type).order("time desc").find(NewsBean.class);
        warningAdapter = new WarningAdapter(this,newsBeanList);
        lv_warning.setAdapter(warningAdapter);
    }
}
