package com.unis.sxzcl.ui.grain_check;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.unis.model.DataCallback;
import com.unis.model.DepotModel;
import com.unis.model.GrainCheckModel;
import com.unis.model.HouseModel;
import com.unis.model.UserModel;
import com.unis.model.bean.HouseListBean;
import com.unis.model.bean.grain_check.GrainCheckBean;
import com.unis.model.event.GrainCheckEvent;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseFragment;
import com.unis.sxzcl.databinding.FragmentBaseTabPagerBinding;
import com.unis.sxzcl.widget.TabFragmentPagerAdapter;
import com.unis.sxzcl.widget.dialog.SystemDialog;
import com.xyxsbj.mylibrary.tool.DateTool;
import com.xyxsbj.mylibrary.tool.ToastTool;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 包名: com.unis.sxzcl.ui.grain_check
 * 类名: GrainCheckFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/10 17:52
 * 更新人:
 * 更新时间: 2020/12/10 17:52
 * 更新说明:
 * 版本号: 1.0
 */
public class GrainCheckHomeFragment extends BaseFragment<FragmentBaseTabPagerBinding> {

    public static final String BGY = "保管员每周检查粮情";
    public static final String KZ = "主管科长每2周检查粮情";
    public static final String FZJL = "分管副总每月检查粮情";
    public static final String ZJL = "总经理每季检查粮情";

    private List<String> mTabs = Arrays.asList(BGY, KZ, FZJL, ZJL);
    private List<Fragment> mFragments;
    private SystemDialog mSystemDialog;

    @Override
    protected FragmentBaseTabPagerBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentBaseTabPagerBinding.inflate(inflater, container, false);
    }

    @Override
    protected void init() {
        HouseListBean house = HouseModel.get().getSelectHouse();
        if (house == null) {
            ToastTool.showLong("未选择仓房");
            onBack();
            return;
        }

        mBinding.bar.setTitle("粮情检查(" + house.houseName + ")")
                .setLeftClickListener(v -> onBack())
                .setRightVisible(true).setRightText("新增")
                .setRightClickListener(v -> goAdd());

        initFragment();
        mBinding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        TabFragmentPagerAdapter adapter = new TabFragmentPagerAdapter(
                getChildFragmentManager(), mTabs, mFragments);
        mBinding.viewPager.setAdapter(adapter);
        mBinding.viewPager.setOffscreenPageLimit(mFragments.size());
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        for (String tab : mTabs) {
            mFragments.add(GrainCheckListFragment.newFragment(tab));
        }
    }

    @Override
    protected void onFirstShow() {

    }

    /**
     * 新增
     */
    private void goAdd() {
        String tab = mTabs.get(mBinding.viewPager.getCurrentItem());
        if (tab.equals(BGY)) {
            // 保管员新增
            Bundle bundle = new Bundle();
            bundle.putString("tab", tab);
            bundle.putString("pageType", "add");
            navigation(R.id.grainCheckDetailFragment, bundle);
        } else {
            showAddDialog(tab);
        }
    }

    private void showAddDialog(String tab) {
        if (mSystemDialog == null)
            mSystemDialog = new SystemDialog(getContext())
                    .setCancelable(false)
                    .setLeftButton("取消", null);
        mSystemDialog
                .setTitle("新增")
                .setMsg("新增" + tab + "？")
                .setRightButton("确定", dialog -> {
                    dialog.dismiss();
                    showProgress("正在新增...", false);
                    GrainCheckBean bean = new GrainCheckBean();
                    bean.recordStatus = "1";
                    bean.checkPerson = tab;
                    bean.checkTime = DateTool.formatDate(new Date(), DateTool.FULL_DATE);
                    bean.grainAbnormalDescribe = null;
                    bean.uid = UserModel.get().getUser().getUid();
                    bean.depotId = DepotModel.get().getSelectDepot().depotCode;
                    String houseCode = HouseModel.get().getSelectHouse().houseCode;
                    bean.houseId = houseCode != null ? Integer.valueOf(houseCode) : null;

                    GrainCheckModel.get().add(bean, new DataCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            if (!isActive()) return;
                            hideProgress();
                            ToastTool.showLong("新增完成");
                            EventBus.getDefault().post(new GrainCheckEvent(tab));
                        }

                        @Override
                        public void onError(String error) {
                            super.onError(error);
                            if (!isActive()) return;
                            hideProgress();
                        }
                    });
                }).show();
    }


}