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

import android.os.Bundle;
import android.text.TextUtils;

import com.google.android.material.tabs.TabLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.event.ChangeStatus;
import com.wd.workoffice.ui.adapter.VpDataAdapter;
import com.wd.workoffice.ui.fg.MesOrderFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

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

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 质量工单详情
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class WorkOrderDetailActivity extends WorkToolBarActivity {

    @BindView(R.id.tl_tab)
    TabLayout tlTab;
    @BindView(R.id.vp_data)
    ViewPager vpData;
    private List<String> tab = new ArrayList<>();
//    {
//            "工单", "计划", "实际情况" ,"故障报告"};

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        tlTab.setupWithViewPager(vpData);
        tlTab.setTabMode(TabLayout.MODE_FIXED);
    }

    @Override
    protected void initData() {
        String status = getIntent().getStringExtra("status");
        Bundle bundle = new Bundle();
        bundle.putString("info", getIntent().getStringExtra("info"));
        FragmentManager sfm = getSupportFragmentManager();
        List<Fragment> data = new ArrayList<>();
        QualityWorkFragment qualityWorkFragment = new QualityWorkFragment();
        QualityWorkPlanFragment qualityWorkPlanFragment = new QualityWorkPlanFragment();
        QualityWorkTrueFragment qualityWorkTrueFragment = new QualityWorkTrueFragment();
        QualityWorkFaultFragment qualityWorkFaultFragment =new QualityWorkFaultFragment();
        qualityWorkFragment.setArguments(bundle);
        qualityWorkPlanFragment.setArguments(bundle);
        qualityWorkTrueFragment.setArguments(bundle);
        qualityWorkFaultFragment.setArguments(bundle);
        data.add(qualityWorkFragment);
        data.add(qualityWorkPlanFragment);
        tab.add("工单");
        tab.add("计划");
        if (TextUtils.equals(status, "3") || TextUtils.equals(status, "4")) {
            tab.add("实际情况");
            data.add(qualityWorkTrueFragment);
        }
        tab.add("故障报告");
        data.add(qualityWorkFaultFragment);
        VpDataAdapter vpFgAdapter = new VpDataAdapter(sfm, data);
        vpData.setAdapter(vpFgAdapter);
        for (int i = 0; i < tab.size(); i++) {
            tlTab.getTabAt(i).setText(tab.get(i));
        }
        vpData.setOffscreenPageLimit(data.size());
    }

    @Override
    protected void initEvent() {
    }

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

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void refresh(ChangeStatus event) {
        finish();
    }
}
