package com.wd.workoffice.ui.fg;

import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkBaseFg;
import com.wd.workoffice.bean.vo.BatLayoutVo;
import com.wd.workoffice.ui.activity.WorkOrderActivity;
import com.wd.workoffice.ui.activity.bat.approval.ApprovalDealActivity;
import com.wd.workoffice.ui.activity.bat.approval.ApprovalFromMeActivity;
import com.wd.workoffice.ui.activity.bat.work.DeviceActivity;
import com.wd.workoffice.ui.activity.bat.approval.WorkApprovalActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkContactsActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkDepActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkFinancialActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkInStockActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkProActivity;
import com.wd.workoffice.ui.activity.bat.work.WorkWarehouseActivity;
import com.wd.workoffice.ui.adapter.BatLayoutAdapter;

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

import androidx.recyclerview.widget.GridLayoutManager;
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 WorkFragment extends WorkBaseFg {
    @BindView(R.id.rv_function)
    RecyclerView rvFunction;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;


    List<BatLayoutVo> workList = new ArrayList<>();
    int[] workImg = new int[]{R.mipmap.work_icon1, R.mipmap.work_icon2,
//            R.mipmap.work_icon3,
            R.mipmap.work_icon4, R.mipmap.work_icon5, R.mipmap.work_icon6, R.mipmap.work_icon7,
//            R.mipmap.work_icon8, R.mipmap.work_icon9,
            R.mipmap.work_icon10, R.mipmap.work_icon11};
    String[] workName = new String[]{"产品", "往来",
//            "部门账户",
            "仓库", "部门", "财务中心", "设备",
//            "流程", "报表",
            "库存类别", "订单"};
    @BindView(R.id.ll_submit)
    LinearLayout llSubmit;
    @BindView(R.id.ll_not_deal)
    LinearLayout llNotDeal;
    @BindView(R.id.ll_deal)
    LinearLayout llDeal;

    @Override
    protected void initView() {
        ButterKnife.bind(this, getContentView());
        addDate(workList, workImg, workName);
        initRvLayout(rvFunction, workList);
    }


    @Override
    protected void initData() {
//        RtfUtils.getRtf().userInfo().compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
//            @Override
//            public void doOnSubscribe(Disposable d) {
//            }
//
//            @Override
//            public void onFail(String errorMsg) {
//            }
//
//            @Override
//            public void onSuccess(BaseBean data) {
//                if (data.getCode() != 0) {
//                    return;
//                }
//                UserKeeper.getInstance().keepUserInfo(data.getData().toString());
//            }
//        });
//        Map<String, Object> param = WorkUtils.pageKey();
//        RtfUtils.getRtf().notice(param).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
//            @Override
//            public void doOnSubscribe(Disposable d) {
//            }
//
//            @Override
//            public void onFail(String errorMsg) {
//                hideLoading();
//                toast(errorMsg);
//            }
//
//            @Override
//            public void onSuccess(BaseBean data) {
//                hideLoading();
//                if (data.getCode() != 0) {
//                    toast(data.getMessage());
//                    return;
//                }
//                List<MsgBean.RecordsBean> getList = JSON.parseObject(data.getData().toString(), MsgBean.class).getRecords();
//                if (getList != null && getList.size() != 0) {
//                    MsgBean.RecordsBean recordsBean = getList.get(0);
//                    tvNotice.setText(recordsBean.getTitle());
//                }
//            }
//        });

    }


    @Override
    protected void initEvent() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh();
        });
    }

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

    private void addDate(List<BatLayoutVo> saleList, int[] saleImg, String[] saleName) {
        for (int i = 0; i < saleImg.length; i++) {
            BatLayoutVo batLayoutVo = new BatLayoutVo();
            batLayoutVo.setImg(saleImg[i]);
            batLayoutVo.setName(saleName[i]);
            saleList.add(batLayoutVo);
        }

    }

    /**
     * 初始化布局
     *
     * @param rvSale
     * @param saleList
     */
    private void initRvLayout(RecyclerView rvSale, List<BatLayoutVo> saleList) {
        rvSale.setLayoutManager(new GridLayoutManager(getActivity(), 4, RecyclerView.VERTICAL,
                false));
        BatLayoutAdapter batLayoutAdapter = new BatLayoutAdapter(R.layout.item_bat_layout, saleList);
        batLayoutAdapter.bindToRecyclerView(rvSale);
        batLayoutAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                String[] workName = new String[]{"产品", "往来",
////            "部门账户",
//                        "仓库", "部门", "财务中心", "设备",
////            "流程", "报表",
//                        "库存类别", "订单"};
                switch (position) {
                    case 0:
                        startActivity(WorkProActivity.class);
                        break;
                    case 1:
                        startActivity(WorkContactsActivity.class);
                        break;
//                    case 2:
//                        startActivity(WorkAccountActivity.class);
//                        break;
                    case 2:
                        startActivity(WorkWarehouseActivity.class);
                        break;
                    case 3:
                        startActivity(WorkDepActivity.class);
                        break;
                    case 4:
                        startActivity(WorkFinancialActivity.class);
                        break;
                    case 5:
                        startActivity(DeviceActivity.class);
                        break;
//                    case 7:
//                        toast("暂无数据");
//                        break;
//                    case 8:
//                        toast("暂无数据");
//                        break;
                    case 6:
                        startActivity(WorkInStockActivity.class);
                        break;
                    case 7:
                        startActivity(WorkOrderActivity.class);
                        break;
                }
            }
        });
    }

    @OnClick({R.id.ll_submit, R.id.ll_deal, R.id.ll_not_deal})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_submit:
                startActivity(WorkApprovalActivity.class,"type","1","status","0","title","待审批");
                break;
            case R.id.ll_deal:
                startActivity(ApprovalDealActivity.class);
                break;
            case R.id.ll_not_deal:
                startActivity(WorkApprovalActivity.class,"type","2","status","0","title","待处理");
                break;
        }
    }
}
