package com.unis.sxzcl.ui.quantity_quality;

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

import com.unis.model.DataCallback;
import com.unis.model.DepotModel;
import com.unis.model.HouseModel;
import com.unis.model.QuantityQualityModel;
import com.unis.model.bean.QuantityQualityBean;
import com.unis.model.bean.detail.DetailItemBean;
import com.unis.model.bean.detail.DetailItemType;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseFragment;
import com.unis.sxzcl.databinding.FragmentAllroundBinding;
import com.unis.sxzcl.ui.allround.ProcessDetailFragment;
import com.xyxsbj.mylibrary.tool.LogTool;
import com.xyxsbj.mylibrary.tool.ToastTool;

import org.greenrobot.eventbus.EventBus;

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

/**
 * 全面流程Fragment
 * (有需要可复制后自行修改)
 * 创建人: mQ
 * 创建时间: 2021/1/26 14:28
 */
public class QuantityQualityDetailFragment extends BaseFragment<FragmentAllroundBinding> implements ProcessDetailFragment.SaveCallback {

    /****************** 传参使用的Key **********************/
    // 页面类型
    public static final String key_page_type = "pageType";
    // 检查类型id
    public static final String key_check_type = "check_type";
    // 检查类型名称
    public static final String key_check_type_name = "check_type_name";
    // 业务类型
    public static final String key_work_type = "work_type";
    // 业务数据
    public static final String key_info = "info";

    // 页面类型
    private String mPageType = "add";
    // 业务类型
    private String mWorkType;
    // 业务数据
    private QuantityQualityBean mBean;

    private String[] titles = new String[]{"质量", "品质", "毒素指标"};

    private String[] options = new String[]{"达标、宜存", "不达标、宜存", "不达标、不宜存"};
    private ProcessDetailFragment mFragment;

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

    @Override
    protected void init() {
        // 接收参数
        Bundle bundle = getArguments();
        if (bundle == null) {
            ToastTool.showLong("没有参数");
            onBack();
            return;
        }

        // 页面类型
        mPageType = bundle.getString(key_page_type, "add");
        mWorkType = bundle.getString(key_work_type);
        String info = bundle.getString(key_info);
        if (info == null) {
            mBean = new QuantityQualityBean();
            mBean.setQtype(bundle.getString(key_check_type));
            mBean.setQtypeName(bundle.getString(key_check_type_name));
            mBean.setDepotYpte(mWorkType);
            mBean.setDepotId(DepotModel.get().getSelectDepot().depotCode);
            mBean.setWarehouseId(HouseModel.get().getSelectHouse().houseCode);
        } else {
            mBean = getGson().fromJson(info, QuantityQualityBean.class);
            mWorkType = mBean.getDepotYpte();
        }


        // 设置标题名称
        mBinding.bar.setTitle(mWorkType.equals("0") ? "库存质量" : "出库质量")
                .setLeftClickListener(v -> onBack());

        LogTool.i("类型:" + mPageType);

    }

    @Override
    protected void onFirstShow() {
        // 首次加载

        if (mPageType.equals("add")) {
            createPage();
        } else {
            getData();
        }

    }

    /**
     * 获取详情数据
     */
    private void getData() {
        showProgress("正在加载...");
        QuantityQualityModel.get().detail(mBean.getQid(), new DataCallback<QuantityQualityBean>() {
            @Override
            public void onSuccess(QuantityQualityBean data) {
                if (!isActive()) return;
                hideProgress();
                mBean = data;
                createPage();
            }

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

    /**
     * 创建选项页面
     */
    private void createPage() {
        // 创建选项
        List<DetailItemBean> optionList = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            String selectIndex = null;
            if (mBean != null) {
                // 回显数据
                if (i == 0) selectIndex = mBean.getCquality();
                if (i == 1) selectIndex = mBean.getCcharacter();
                if (i == 2) selectIndex = mBean.getCtoxinIndex();
            }
            optionList.add(new DetailItemBean(DetailItemType.radio)
                    .setTitle(titles[i])        // 标题
                    .setId(i + "")              // id
                    .setIndex(i + 1)            // 序号
                    .setFlowId(null)            // 流程id
                    .setSelectIndex(selectIndex)       // 选中项
                    .setTime(null)              // 操作时间
                    .setOption(options));       //  选项
        }

        // 创建页面
        mFragment = ProcessDetailFragment.newFragment(
                0,                              // 页面位置
                true,                           //是否最后一页
                mPageType,                      // 页面类型
                null,                           // 业务名称
                optionList,                     // 选项数据
                null); // 审批数据
        // 设置页面的按钮监听
        mFragment.setSaveCallback(this);

        // 添加页面到容器中
        getChildFragmentManager().beginTransaction()
                .add(R.id.container_layout, mFragment)
                .commit();
    }

    /**
     * 保存按钮回调监听
     *
     * @param pageIndex  页面位置
     * @param optionList 选项数据集合
     */
    @Override
    public void onSaveCallback(int pageIndex, List<DetailItemBean> optionList) {
        LogTool.i("保存按钮事件---页面位置:" + pageIndex);
        LogTool.i("保存按钮事件---选项数据:" + getGson().toJson(optionList));

        mBean.setCquality(optionList.get(0).getSelectIndex());
        mBean.setCcharacter(optionList.get(1).getSelectIndex());
        mBean.setCtoxinIndex(optionList.get(2).getSelectIndex());
        mBean.setSaveState(mFragment.isSelectFinish() ? "0" : "1");

        showProgress("正在提交...", false);
        if (mPageType.equals("add")) {
            // 新增
            QuantityQualityModel.get().add(mBean, mDataCallback);
        } else {
            // 修改
            QuantityQualityModel.get().update(mBean, mDataCallback);
        }
    }

    DataCallback mDataCallback = new DataCallback<String>() {
        @Override
        public void onSuccess(String data) {
            if (!isActive()) return;
            hideProgress();
            // 通知列表刷新
            EventBus.getDefault().post(new QuantityQualityBean());
            ToastTool.showLong("保存成功");
            onBack();
        }

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