package com.bonait.bnframework.modules.home.fragment.from;

import static com.bonait.bnframework.MainApplication.getContext;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.bonait.bnframework.R;
import com.bonait.bnframework.common.base.BaseActivity;
import com.bonait.bnframework.common.base.BaseFragment;
import com.bonait.bnframework.common.constant.ConfigName;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.res.ResAlertLog;
import com.bonait.bnframework.common.db.res.ResLog;
import com.bonait.bnframework.common.helper.DateUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.adapter.yjrz_adapter;
import com.bonait.bnframework.modules.home.fragment.GuanLifragment;
import com.bonait.bnframework.modules.home.fragment.from.fragment.Jcsjgl_sxgl_fragment;
import com.bonait.bnframework.modules.home.fragment.from.fragment.Jcsjgl_wl_fragment;
import com.qmuiteam.qmui.arch.QMUIFragment;
import com.qmuiteam.qmui.arch.QMUIFragmentPagerAdapter;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.QMUIViewPager;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LogActivity extends BaseActivity {

    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;

    //region 操作变量
    /**
     * 输入框
     */
    @BindView(R.id.edittext)
    TextView edittext;
    /**
     * 查询按钮
     */
    @BindView(R.id.button)
    Button button;
    /**
     * 表格显示
     */
    @BindView(R.id.datatab)
    ListView datatab;
    /**
     * 日志数据
     */
    ArrayList<ResLog> bpa_logs = new ArrayList<>();
    /**
     * 员工操作日志控制器
     */
    yjrz_adapter adapter;
    /**
     * 日期选择器
     */
    TimePickerView pvTime;
    /**
     * 开始时间-结束时间
     */
    @BindView(R.id.starttime)
    EditText starttime;
    @BindView(R.id.stoptime)
    EditText stoptime;
    /**
     * 日志类型
     */
    @BindView(R.id.rzlx)
    Spinner rzlx;
    Map<String,Integer> lx_map = new LinkedHashMap<>();
    //endregion
    private Context context;
    private ViewGroup view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log);
        ButterKnife.bind(this);
        context=getContext();
        view=(ViewGroup)getWindow().getDecorView();
        initTopBar();
        initFragment();
        Init();
    }

    private void initTopBar() {
        mTopBar.setTitle("日志管理");
        mTopBar.addLeftImageButton(R.mipmap.fanhui,R.id.topbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    /**
     * viewPager里添加fragment
     */
    private void initFragment() {

    }

    /**
     * 初始化
     */
    private void Init(){

        lx_map.put("全部日志",0);
        lx_map.put("登录日志",1);
        lx_map.put("角色操作日志",2);
        lx_map.put("数据接收",3);
        lx_map.put("上传日志",4);
        lx_map.put("订单处理日志",5);


        ArrayAdapter<String> adapter2 = new ArrayAdapter<>(context, R.layout.spinner_text_item, new ArrayList<>(lx_map.keySet()));
        adapter2.setDropDownViewResource(R.layout.spinner_dropdown_item);
        rzlx.setAdapter(adapter2);

        starttime.setText(new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(new Date()));
        stoptime.setText(new SimpleDateFormat("yyyy-MM-dd 23:59:59").format(new Date()));
        starttime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (pvTime != null) {
                    pvTime.show(starttime);
                }
            }
        });
        stoptime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (pvTime != null) {
                    pvTime.show(stoptime);
                }
            }
        });
        CalendarTime();
        Initdata();
    }
    /**
     * CalendarTime
     * 时间选择器
     */
    public void CalendarTime() {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(2023, 0, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2099, 11, 28);
        //时间选择器
        pvTime = new TimePickerView.Builder(context, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
                TextView btn = (TextView) v;
                btn.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
            }
        })
                //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, true, true, true, false})
                .setLabel("年", "月", "日", "时", "分", "秒")
                .isCenterLabel(true)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(16)//字号
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setDecorView(view)
                .build();


    }


    /**
     * 初始化数据加载
     */
    public void Initdata() {
        try {
            String str= starttime.getText().toString();
            String stop= stoptime.getText().toString();
            if(DateUtils.compareDate(stop,str))
            {
                ToastUtils.warning("开始时间不能大于结束时间!!!");
                return;
            }
            String lx= rzlx.getSelectedItem().toString();
            String text= edittext.getText().toString();
            bpa_logs= QueryDB.Getlogs(str,stop, lx_map.get(lx),text);
            adapter = new yjrz_adapter(context,R.layout.yjrz_item,bpa_logs);
            datatab.setAdapter(adapter);
        } catch (Exception e) {

        }
    }

    //region 点击事件
    @OnClick({R.id.button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.button://查询按钮
                Initdata();
                break;
        }
    }
    //endregion
    //-------------------------配置viewPager与fragment关联----------------------------//

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected boolean canDragBack() {
        return false;
    }
}