package com.wd.workoffice.ui.activity.bat.order.sale;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.material.tabs.TabLayout;
import com.wd.workoffice.R;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.ui.activity.bat.order.ProductOrderAddActivity;
import com.wd.workoffice.ui.activity.bat.order.ProductOrderSearchActivity;
import com.wd.workoffice.ui.adapter.VpDataAdapter;
import com.wd.workoffice.ui.fg.ProductOrderApplyFragment;
import com.wd.workoffice.ui.fg.ProductOrderFragment;
import com.wd.workoffice.util.WorkUtils;

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;

import static com.google.android.material.tabs.TabLayout.MODE_FIXED;

/**
 * 组装订单
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class SaleProductOrderActivity extends WorkToolBarActivity {

    @BindView(R.id.tl_tab)
    TabLayout tlTab;
    @BindView(R.id.vp_data)
    ViewPager vpData;
    private String tab[] = {
            "组装订单", "组装申请"};

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

    @Override
    protected void initData() {
        FragmentManager sfm = getSupportFragmentManager();
        List<Fragment> data = new ArrayList<>();
        ProductOrderFragment saleFragment = new ProductOrderFragment();
        ProductOrderApplyFragment applyFragment = new ProductOrderApplyFragment();
        Bundle bundle = new Bundle();
        bundle.putString("identity", getIntent().getStringExtra("identity"));
        saleFragment.setArguments(bundle);
        applyFragment.setArguments(bundle);
        data.add(saleFragment);
        data.add(applyFragment);
        VpDataAdapter vpFgAdapter = new VpDataAdapter(sfm, data);
        vpData.setAdapter(vpFgAdapter);
        for (int i = 0; i < tab.length; i++) {
            tlTab.getTabAt(i).setText(tab[i]);
        }
    }

    @Override
    protected void initEvent() {
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (WorkUtils.hasPermission(PagePermissionType.AO_PACKAGE_APPLY_SAVE.getPermission())) {
            getMenuInflater().inflate(R.menu.menu_add_and_search, menu);
        }else {
            getMenuInflater().inflate(R.menu.menu_search, menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.search:
                startActivity(ProductOrderSearchActivity.class, "identity", getIntent().getStringExtra("identity"));
                break;
            case R.id.add:
                startActivity(ProductOrderAddActivity.class, "identity", getIntent().getStringExtra("identity"));
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
