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.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bonait.bnframework.R;
import com.bonait.bnframework.common.db.QueryDB;
import com.bonait.bnframework.common.db.mode.BPA_GOODS;
import com.bonait.bnframework.common.db.mode.BPA_GOODSRECIPENAME;
import com.bonait.bnframework.common.db.mode.BPA_GOODSTYPE;
import com.bonait.bnframework.common.db.mode.BPA_MATERIAL;
import com.bonait.bnframework.common.db.res.ResGoodsRecipe;
import com.bonait.bnframework.common.helper.I.MyClickListener;
import com.bonait.bnframework.common.utils.ToastUtils;
import com.bonait.bnframework.modules.home.adapter.goodpf_apapter;
import com.bonait.bnframework.modules.home.adapter.wl_adapter;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class add_pf_home_control  extends LinearLayout implements MyClickListener {

    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.type_name)
    TextView type_name;

    @BindView(R.id.datatab_pf)
    ListView datatab_pf;

    @BindView(R.id.xinjianpeifang)
    Button xinjianpeifang;

    @BindView(R.id.close_from)
    Button close_from;

    /**
     * 点击事件
     */
    public MyClickListener mListener=null;
    private View root;
    public add_pf_home_control(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        root= LayoutInflater.from(context).inflate(R.layout.dialog_add_pf_home, this);
        ButterKnife.bind(this, root);
        Init();
    }


    public void Init()
    {
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
    }


    private void initData()
    {

    }


    private void initEvent()
    {
        xinjianpeifang.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.clickListenerNew(view,1,good);
                }
            }
        });
        close_from.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.clickListenerNew(v,0,null);
                }
            }
        });
    }

    BPA_GOODS good =null;
    public void SetData(BPA_GOODS _good, ArrayList<BPA_GOODSTYPE> types)
    {
        good=_good;

        title.setText(good.name+"");
        for (BPA_GOODSTYPE item:types)
        {
            if(item.id.equals(good.goodtype))
            {
                type_name.setText(item.name+"");
            }
        }

        Initdata_pf();
    }

    public void Initdata_pf()
    {
        if(good!=null)
        {
            //根据商品查询配方
            ArrayList<BPA_GOODSRECIPENAME> goodsrecipenames=QueryDB.GetGoodsRecipeNameGoodId(good.id);
            goodpf_apapter adapter = new goodpf_apapter(getContext(), R.layout.wl_item, goodsrecipenames, this);
            datatab_pf.setAdapter(adapter);
        }
    }



    public BPA_GOODSRECIPENAME SelectData;
    /**
     * 点击事件
     *
     * @param v
     * @param data
     */
    @Override
    public void clickListener(View v, Object data) {
        switch (v.getId()) {
            case R.id.button_update://点击 修改
                SelectData =(BPA_GOODSRECIPENAME)data;
                if (mListener != null) {
                    mListener.clickListenerNew(v,2,good);
                }
                break;
            case R.id.button_item://删除按钮

                ArrayList<ResGoodsRecipe> re= QueryDB.GetGoodsSreciperecipeList(((BPA_GOODSRECIPENAME) data).id);
                for(ResGoodsRecipe item:re)
                {
                    QueryDB.DeleteGoodsSrecipe(item);
                }
                QueryDB.DeleteGoodsRecipeName((BPA_GOODSRECIPENAME) data);
                Initdata_pf();
                ToastUtils.info("删除成功");
                break;
        }
    }

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

    }

}
