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

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.bonait.bnframework.R;
import com.bonait.bnframework.business.ConfigData;
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.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.mode.BPA_USER;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.utils.NetworkUtils;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.adapter.user_adapter;
import com.bonait.bnframework.modules.home.adapter.wl_adapter;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.ArrayList;

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

public class UserActivity extends BaseActivity implements MyClickListener {
    @BindView(R.id.topbar)
    QMUITopBarLayout mTopBar;

    @BindView(R.id.add_wl)
    Button add_wl;
    @BindView(R.id.datatab)
    ListView datatab;
    /**
     * 用户数据
     */
    ArrayList<BPA_USER> bpa_users = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);
        //属性绑定
        ButterKnife.bind(this);
        initTopBar();
        InitView();
    }

    private void initTopBar() {
        mTopBar.setTitle("账户管理");
        mTopBar.addLeftImageButton(R.mipmap.fanhui, R.id.topbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    /**
     * 初始化界面
     */
    public void InitView() {
        Initdata();
    }

    @OnClick({R.id.add_wl, R.id.synchronous})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.add_wl://增加物料
                final QMUIDialog.EditTextDialogBuilder builder_add = new QMUIDialog.EditTextDialogBuilder(this);
                builder_add.setTitle("用户名称")
                        .setPlaceholder("在此输入用户名称")
                        .setInputType(InputType.TYPE_CLASS_TEXT)
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                CharSequence text = builder_add.getEditText().getText();
                                if (text != null && text.length() > 0) {
                                    if (text.toString().equals("admin")) {
                                        ToastUtils.warning("不能设置账号为admin！");
                                        return;
                                    }
                                    BPA_USER bpa_user = new BPA_USER();
                                    bpa_user.name = text.toString();
                                    bpa_user.account = text.toString();
                                    bpa_user.pass = "123456";
                                    bpa_user.exp = "";
                                    bpa_user.deviceID = ConfigName.getInstance().DeviceId;
                                    bpa_user.userID = ConfigName.getInstance().user.userID;//更改为,用户权限 // 一般用户 研发用户 超级管理员
                                    QueryDB.AddUser(bpa_user);
                                    ToastUtils.info("新增成功");
                                    Initdata();
                                    dialog.dismiss();
                                } else {
                                    //请填入昵称
                                    ToastUtils.info("名称不能为空！");
                                }
                            }
                        })
                        .show();
                break;
        }
    }


    /**
     * 初始化数据加载
     */
    public void Initdata() {
        try {
            bpa_users = QueryDB.GetUserALL();
            user_adapter adapter = new user_adapter(getBaseContext(), R.layout.user_item, bpa_users, this);
            datatab.setAdapter(adapter);
        } catch (Exception e) {

        }
    }

    @Override
    public void clickListener(View v, Object data) {
        String name = ((BPA_USER) data).name;
        String ac = ((BPA_USER) data).account;
        if (name.equals("admin") || ac.equals("admin")) {
            ToastUtils.warning("对不起,admin账户禁止操作！");
            return;
        }
        switch (v.getId()) {
            case R.id.name://修改物料名称按钮
                final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(this);
                builder.setTitle("用户名称")
                        .setPlaceholder("在此输入用户名称")
                        .setDefaultText(((BPA_USER) data).name)
                        .setInputType(InputType.TYPE_CLASS_TEXT)
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                CharSequence text = builder.getEditText().getText();
                                if (text != null && text.length() > 0) {
                                    if (text.toString().equals("admin")) {
                                        ToastUtils.warning("不能设置账号为admin！");
                                        return;
                                    }
                                    QueryDB.UpdateUserName(((BPA_USER) data).id, text.toString());
                                    Initdata();
                                    ToastUtils.info("修改成功");
                                    dialog.dismiss();
                                } else {
                                    //请填入昵称
                                    ToastUtils.warning("不能为空！");
                                }
                            }
                        })
                        .show();
                break;
            case R.id.zhanghao://修改物料名称按钮
                final QMUIDialog.EditTextDialogBuilder builder1 = new QMUIDialog.EditTextDialogBuilder(this);
                builder1.setTitle("用户账号")
                        .setPlaceholder("在此输入用户账号")
                        .setDefaultText(((BPA_USER) data).account)
                        .setInputType(InputType.TYPE_CLASS_TEXT)
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                CharSequence text = builder1.getEditText().getText();
                                if (text != null && text.length() > 0) {
                                    if (text.toString().equals("admin")) {
                                        ToastUtils.warning("不能设置账号为admin！");
                                        return;
                                    }
                                    QueryDB.UpdateAccount(((BPA_USER) data).id, text.toString());
                                    Initdata();
                                    ToastUtils.info("修改成功");
                                    dialog.dismiss();
                                } else {
                                    //请填入昵称
                                    ToastUtils.info("不能为空！");
                                }
                            }
                        })
                        .show();
                break;
            case R.id.mima://修改物料名称按钮
                final QMUIDialog.EditTextDialogBuilder builder2 = new QMUIDialog.EditTextDialogBuilder(this);
                builder2.setTitle("用户密码")
                        .setPlaceholder("在此输入用户密码")
                        .setDefaultText(((BPA_USER) data).pass)
                        .setInputType(InputType.TYPE_CLASS_TEXT)
                        .addAction("取消", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                dialog.dismiss();
                            }
                        })
                        .addAction("确定", new QMUIDialogAction.ActionListener() {
                            @Override
                            public void onClick(QMUIDialog dialog, int index) {
                                CharSequence text = builder2.getEditText().getText();
                                if (text != null && text.length() > 0) {
                                    QueryDB.UpdateUserPass(((BPA_USER) data).id, text.toString());
                                    Initdata();
                                    ToastUtils.info("修改成功");
                                    dialog.dismiss();
                                } else {
                                    //请填入昵称
                                    ToastUtils.info("不能为空！");
                                }
                            }
                        })
                        .show();
                break;

            case R.id.button_item://删除按钮
                QueryDB.DeleteUser((BPA_USER) data);
                Initdata();
                ToastUtils.info("删除成功");
                break;
        }
    }

    @Override
    public void clickListenerNew(View v, int k, Object data) {

    }
}