package com.example.administrator.zhapp.activity.xfbwxcgl;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;

import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.administrator.zhapp.DBModel.XfBaxcRwqy;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.activity.MipcaActivityCapture;
import com.example.administrator.zhapp.adapter.MyFragmentAdapter;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.fragment.xfbwxcgl.XxzlbaFragment;
import com.example.administrator.zhapp.fragment.xfbwxcgl.XzaxlFragment;
import com.example.administrator.zhapp.utils.SPUtils;
import com.example.administrator.zhapp.weight.AutoRadioGroup;
import com.example.administrator.zhapp.weight.NoscrollViewPager;
import org.litepal.crud.DataSupport;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * 消防保卫巡查管理-保安巡更详情
 */
public class Xbaxg2Activity extends BaseActivity {

    NoscrollViewPager vp_centent;
    RadioButton rb1;
    RadioButton rb2;
    AutoRadioGroup rg;
    Button btnSys;

    private XzaxlFragment xzaxl;
    private XxzlbaFragment xxzlba;
    private List<Fragment> list;
    private MyFragmentAdapter adapter;

    private String jhid;

    private ArrayList<XfBaxcRwqy> zaxl;//治安巡逻
    private ArrayList<XfBaxcRwqy> xzlbaqd;//行政楼保安签到

    private void findView() {
        btnSys = findViewById(R.id.btn_sys);
        vp_centent = findViewById(R.id.vp_centent);
        rb1 = findViewById(R.id.rb1);
        rb2 = findViewById(R.id.rb2);
        rg = findViewById(R.id.rg);
        btnSys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MipcaActivityCapture.class);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    public int getContentViewID() {
        return R.layout.activity_xbaxg2;
    }

    protected void initView() {
        findView();
    }

    public void initData() {

        initNFC();

        setTitle("治安巡逻签到点");
        setBack();

        zaxl = new ArrayList<>();
        xzlbaqd = new ArrayList<>();
        jhid = (String) getIntent().getExtras().get("jhid");
        getDataInSqlite();

        xzaxl = XzaxlFragment.newInstance(" ", zaxl);
        xxzlba = XxzlbaFragment.newInstance(" ", xzlbaqd);
        list = new ArrayList<>();

        list.add(xzaxl);
        list.add(xxzlba);
        adapter = new MyFragmentAdapter(getSupportFragmentManager(), list);
        vp_centent.setAdapter(adapter);
        rg.setOnCheckedChangeListener((radioGroup, i) -> {
            if(i==R.id.rb1){
                vp_centent.setCurrentItem(0, false);
                title.setText("治安巡逻签到点");
                rb1.setTextColor(Color.WHITE);
                rb2.setTextColor(Color.GRAY);
            }else if(i==R.id.rb2){
                vp_centent.setCurrentItem(1, false);
                title.setText("行政楼保安签到点");
                rb2.setTextColor(Color.WHITE);
                rb1.setTextColor(Color.GRAY);
            }
        });
    }

    //读取本地数据
    private void getDataInSqlite() {

        zaxl.clear();
        xzlbaqd.clear();

        List<XfBaxcRwqy> rwqys = DataSupport.where("jhid = ?", jhid).find(XfBaxcRwqy.class);
        for (XfBaxcRwqy rwqy : rwqys) {
            if (rwqy.getXgtype().equals("1"))//治安
            {
                zaxl.add(rwqy);
            } else {
                xzlbaqd.add(rwqy);
            }
        }
    }

    //处理nfc的数据
    @Override
    public void handleNfc(String result) {
        super.handleNfc(result);
        ContentValues values = new ContentValues();
        values.put("checked", "1");
        values.put("smfx", "NFC标签");
        values.put("cjr", (String) SPUtils.get(context, Contans.NAME, "测试"));
        values.put("cjsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        int count = DataSupport.updateAll(XfBaxcRwqy.class, values, "qynfc = ?", result);

        if (count != 0) {
            List<XfBaxcRwqy> list = DataSupport.where("qynfc = ?",result).find(XfBaxcRwqy.class);
            if (list.size() != 0 ) {
                showToast(list.get(0).getQywz()+"签到成功");
                getDataInSqlite();
                xzaxl.notifiDataChange();
                xxzlba.notifiDataChange();
            }
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {
                Bundle bundle = data.getExtras();
                String txm = bundle.getString("result");
                ContentValues values = new ContentValues();
                values.put("checked", "1");
                values.put("smfx", "扫描条码");
                values.put("cjr", (String) SPUtils.get(context, Contans.NAME, "测试"));
                values.put("cjsj", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                int count = DataSupport.updateAll(XfBaxcRwqy.class, values, "txmbh = ?", txm);
                if (count != 0) {

                    List<XfBaxcRwqy> list = DataSupport.where("txmbh = ?", txm).find(XfBaxcRwqy.class);
                    if (list.size() != 0) {
                        showToast(list.get(0).getQywz() + "签到成功");
                        getDataInSqlite();
                        xzaxl.notifiDataChange();
                        xxzlba.notifiDataChange();
                    }
                }
            }
        }
    }
}