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

import android.content.Context;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ConfigData;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_PLCADDRESS;
import com.bonait.bnframework.common.utils.AlertDialogUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

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


public class fragment_plc extends LinearLayout {
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.name_PLC)
    TextView name_PLC;
    @BindView(R.id.update_plc)
    Button update_plc;
    @BindView(R.id.edittext_plc)
    EditText edittext_plc;

    @BindView(R.id.check_isread)
    CheckBox check_isread;
    @BindView(R.id.check_iswrite)
    CheckBox check_iswrite;
    private View root;
    public fragment_plc(Context context, @Nullable AttributeSet attrs,BPA_PLCADDRESS _model) {
        super(context, attrs);
        root= LayoutInflater.from(context).inflate(R.layout.fragment_item_plc, this);
        ButterKnife.bind(this, root);
        model=_model;
        Init();
    }
    public void Init()
    {
        if (model.address.equals("-------------"))
        {
            name.setText(model.name+"：");
            edittext_plc.setVisibility(View.GONE);
            update_plc.setVisibility(View.GONE);
            check_isread.setVisibility(View.GONE);
            check_iswrite.setVisibility(View.GONE);
        }else
        {
            name_PLC.setText(model.name);
            edittext_plc.setInputType(InputType.TYPE_CLASS_TEXT);
            edittext_plc.setText(model.address.toCharArray(), 0, model.address.length());
            check_isread.setChecked(model.isread==1);
            check_iswrite.setChecked(model.iswrite==1);
        }
    }
    public BPA_PLCADDRESS model=null;

    @OnClick({R.id.update_plc})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.update_plc:
                String text=edittext_plc.getText().toString();
                if(text.isEmpty())
                {
                    ToastUtils.warning("地址不允许为空！");
                    return;
                }

                String title = "温馨提示！";
                String message = "客官确定要修改["+model.name+"]地址吗，小菠萝温馨提示你,此操作将会影响到实际运行哦？";
                AlertDialogUtils.showDialog(getContext(), title, message, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        model.address=text;
                        model.iswrite=check_iswrite.isChecked()?1:0;
                        model.isread=check_isread.isChecked()?1:0;
                        QueryDB.UpdatePlcaddress(model);
                        ToastUtils.info("修改成功！");
                        dialog.dismiss();
                    }
                });
                break;

        }
    }
}