package com.wd.workoffice.ui.activity.bat.work;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseActivity;
import com.wd.workoffice.bean.ProductBean;
import com.wd.workoffice.contract.WorkProContract;
import com.wd.workoffice.presenter.WorkProPresenter;
import com.wd.workoffice.ui.adapter.ProSupplierAdapter;
import com.wd.workoffice.ui.adapter.SaleSupplierAdapter;
import com.wd.workoffice.ui.adapter.WorkProductAdapter;
import com.wd.workoffice.util.WorkUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 销售订单搜索
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkProSearchActivity extends WorkBaseActivity implements WorkProContract.View {

    @BindView(R.id.et_key)
    EditText etKey;
    @BindView(R.id.tb_title)
    Toolbar tbTitle;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.iv_search)
    TextView ivSearch;
    @BindView(R.id.tl_tab)
    TabLayout tlTab;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    private Map<String, Object> param;
    private WorkProPresenter workProPresenter;
    private WorkProductAdapter workProductAdapter;
    private List<ProductBean.RecordsBean> dataList;
    private int page = 1;
    private String ofKind="OS";
    private String tab[] = {"外购产品", "自制产品", "原料"};
    private String ofKindArrays[] = {"OS", "SM", "RM"};
    @Override
    protected void initView() {
        ButterKnife.bind(this);
        workProPresenter = new WorkProPresenter();
        workProPresenter.onAttach(this);
        setSupportActionBar(tbTitle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        rvData.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
        for (int i = 0; i < tab.length; i++) {
            TabLayout.Tab newTab = tlTab.newTab();
            tlTab.addTab(newTab.setText(tab[i]));
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void initData() {
        param = WorkUtils.pageKey();
        param.put("ofKind", ofKind);
        page = 1;
        param.put("current", page);
        dataList = new ArrayList<>();
        workProductAdapter = new WorkProductAdapter(R.layout.item_work_product, dataList);
        workProductAdapter.bindToRecyclerView(rvData);
        workProductAdapter.setEmptyView(R.layout.view_empty_content, rvData);
        workProPresenter.getPro(param);
    }

    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            page = 1;
            param.put("current", page);
            workProPresenter.getPro(param);
        });
        workProductAdapter.setOnLoadMoreListener(() -> {
            param.put("current", page);
            workProPresenter.getPro(param);
        }, rvData);
        tlTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                page=1;
                ofKind=ofKindArrays[tab.getPosition()];
                param.put("ofKind", ofKind);
                param.put("current", page);
                workProPresenter.getPro(param);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        workProductAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.btn_modify:
                        openModify(dataList.get(position).getId());
                        break;
                    case R.id.btn_on_sale:
                        openOnSale(dataList.get(position));
                        break;
                }
            }
        });
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_pro_search;
    }


    @OnClick(R.id.iv_search)
    public void onViewClicked() {
        String key = etKey.getText().toString();
        if (TextUtils.isEmpty(key)) {
            toast("请输入搜索关键字");
            return;
        }
        page=1;
        param.put("current", page);
        param.put("searchValue", key);
        workProPresenter.getPro(param);
    }


    @Override
    public void getPro(List<ProductBean.RecordsBean> data) {
        toast("加载完成");
        if (page == 1) {
            dataList.clear();
            dataList.addAll(data);
            workProductAdapter.notifyDataSetChanged();
            workProductAdapter.loadMoreComplete();
        } else {
            workProductAdapter.loadMoreComplete();
            dataList.addAll(data);
            workProductAdapter.notifyDataSetChanged();
        }
        if (data.size() == 0) {
            workProductAdapter.loadMoreEnd();
        } else {
            page++;
        }
    }

    @Override
    public void modify(String msg) {
        page = 1;
        workProPresenter.getPro(param);
        toast(msg);
    }

    @Override
    public void onSale(String msg) {
        toast(msg);
    }
    /**
     * 修改弹窗
     *
     * @param id
     */
    private void openModify(int id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.view_pro_modify_price, null);
        TextView tvOk = view.findViewById(R.id.tv_ok);
        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        EditText etPrice = view.findViewById(R.id.et_price);
        EditText etRemark = view.findViewById(R.id.et_remark);
        builder.setView(view);
        AlertDialog addCartDialog = builder.create();
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(etPrice.getText().toString())) {
                    toast("请填写内部价格");
                    return;
                }
                if (TextUtils.isEmpty(etRemark.getText().toString())) {
                    toast("请填写备注");
                    return;
                }
                workProPresenter.modify(id, etPrice.getText().toString(), etRemark.getText().toString());
                addCartDialog.dismiss();
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCartDialog.dismiss();
            }
        });
        addCartDialog.show();
    }

    /**
     * 修改上下架
     *
     * @param data
     */
    private void openOnSale(ProductBean.RecordsBean data) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.view_pro_on_sale, null);
        TextView tvOk = view.findViewById(R.id.tv_ok);
        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        TextView tvTitle = view.findViewById(R.id.tv_title);
        TextView tvPrice = view.findViewById(R.id.tv_price);
        TextView tvName = view.findViewById(R.id.tv_name);
        tvPrice.setText(String.valueOf(data.getInternalPrice()).concat("/").concat(data.getUnit()));
        tvName.setText(data.getName());
        builder.setView(view);
        AlertDialog addCartDialog = builder.create();
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                workProPresenter.onSale(data.getId(),"down");
                addCartDialog.dismiss();
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addCartDialog.dismiss();
            }
        });
        addCartDialog.show();
    }
}
