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

import android.os.Bundle;
import android.text.TextUtils;
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.OrderType;
import com.wd.workoffice.bean.workEnum.PagePermissionType;
import com.wd.workoffice.ui.adapter.VpDataAdapter;
import com.wd.workoffice.ui.fg.AddStockApplyFragment;
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;

/**
 * 入库列表
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class AddStockApplyActivity extends WorkToolBarActivity {

    @BindView(R.id.tl_tab)
    TabLayout tlTab;
    @BindView(R.id.vp_data)
    ViewPager vpData;
    private String tab[] = {
            "待审核", "已通过", "已驳回"};
    private String order[] = {
            "0", "1", "2"};

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

    @Override
    protected void initData() {
        FragmentManager sfm = getSupportFragmentManager();
        List<Fragment> data = new ArrayList<>();
        for (int i = 0; i < tab.length; i++) {
            AddStockApplyFragment saleFragment = new AddStockApplyFragment();
            Bundle bundle = new Bundle();
            bundle.putString("state", order[i]);
            bundle.putString("orderId", getIntent().getStringExtra("orderId"));
            bundle.putString("orderType", getIntent().getStringExtra("orderType"));
            bundle.putString("productId", getIntent().getStringExtra("productId"));
            bundle.putString("num", getIntent().getStringExtra("num"));
            bundle.putString("isCrucible", getIntent().getStringExtra("isCrucible"));
            saleFragment.setArguments(bundle);
            data.add(saleFragment);

        }
        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) {
        String orderType = getIntent().getStringExtra("orderType");
        String num = getIntent().getStringExtra("num");
        if (Double.valueOf(num) == 0d) {
            return super.onCreateOptionsMenu(menu);
        }
        if (TextUtils.equals(getIntent().getStringExtra("status"), "300")) {
            if (TextUtils.equals(orderType, OrderType.SALE.getCode())) {//销售权限判断
                if (WorkUtils.hasPermission(PagePermissionType.SO_ENTER_SAVE.getPermission())) {
                    getMenuInflater().inflate(R.menu.menu_add, menu);
                }
            } else if (TextUtils.equals(orderType, OrderType.OUT_BUY.getCode())) {
                if (WorkUtils.hasPermission(PagePermissionType.PO_ENTER_SAVE.getPermission())) {
                    getMenuInflater().inflate(R.menu.menu_add, menu);
                }
            } else if (TextUtils.equals(orderType, OrderType.SUPPLIER.getCode())) {
                if (WorkUtils.hasPermission(PagePermissionType.FO_ENTER_SAVE.getPermission())) {
                    getMenuInflater().inflate(R.menu.menu_add, menu);
                }
            } else if (TextUtils.equals(orderType, OrderType.CRUCIBLE.getCode())) {
                if (WorkUtils.hasPermission(PagePermissionType.CO_ENTER_SAVE.getPermission())) {
                    getMenuInflater().inflate(R.menu.menu_add, menu);
                }
            } else if (TextUtils.equals(orderType, OrderType.IN.getCode())) {
                if (WorkUtils.hasPermission(PagePermissionType.IO_ENTER_SAVE.getPermission())) {
                    getMenuInflater().inflate(R.menu.menu_add, menu);
                }
            } else {
            }
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add:
                if (TextUtils.equals("1", getIntent().getStringExtra("isCrucible"))) {
                    startActivity(AddStockApplyCrucibleAddActivity.class,
                            "num", getIntent().getStringExtra("num"),
                            "orderType", getIntent().getStringExtra("orderType"),
                            "productId", getIntent().getStringExtra("productId"),
                            "orderItemId", getIntent().getStringExtra("orderItemId"),
                            "simpleCode", getIntent().getStringExtra("simpleCode"),
                            "depId", getIntent().getStringExtra("depId"),
                            "id", getIntent().getStringExtra("orderId"));
                    return super.onOptionsItemSelected(item);
                }
                startActivity(AddStockApplyAddActivity.class,
                        "num", getIntent().getStringExtra("num"),
                        "orderType", getIntent().getStringExtra("orderType"),
                        "productId", getIntent().getStringExtra("productId"),
                        "orderItemId", getIntent().getStringExtra("orderItemId"),
                        "simpleCode", getIntent().getStringExtra("simpleCode"),
                        "id", getIntent().getStringExtra("orderId"));
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
