package com.qhclh.ytzh.work.productiondailyw;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.bean.ProductionDailyDetailBean;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.tasks.ViewOnClickTask;
import com.qhclh.ytzh.ui.CalendarDialogFragment;
import com.qhclh.ytzh.utils.DateTimeUtil;

import org.forks.jsonrpc.JsonRpcResult;
import org.forks.jsonrpc.RpcFacade;
import org.forks.jsonrpc.RpcObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2017/12/4.
 */
///< 写生产日报
public class ProductiondailywActivity extends BaseActivity implements CalendarDialogFragment.CalendarClickListener{
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tabLayout_productiondailyw)
    TabLayout mTabLayout;
    @BindView(R.id.viewPager_productiondailyw)
    ViewPager mViewPager;

    private List<Fragment> mFragmentList = new ArrayList<>();
    private List<String> mTitleList = new ArrayList<>();
    private TabViewPagerAdapter mViewPagerAdapter;

    private long farmId;
    private long batchId;
    private long accountId;
    private Date time;

    private long houseid;
    private int days;
    private BigDecimal cunhuolv;
    private int dienum;
    private int taotai;
    private BigDecimal water;
    private BigDecimal weight;

    public long getFarmId() {
        return farmId;
    }

    public void setFarmId(long farmId) {
        this.farmId = farmId;
    }

    public long getBatchId() {
        return batchId;
    }

    public void setBatchId(long batchId) {
        this.batchId = batchId;
    }

    public long getAccountId() {
        return accountId;
    }

    public void setAccountId(long accountId) {
        this.accountId = accountId;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public long getHouseid() {
        return houseid;
    }

    public void setHouseid(long houseid) {
        this.houseid = houseid;
    }

    public int getDays() {
        return days;
    }

    public void setDays(int days) {
        this.days = days;
    }

    public BigDecimal getCunhuolv() {
        return cunhuolv;
    }

    public void setCunhuolv(BigDecimal cunhuolv) {
        this.cunhuolv = cunhuolv;
    }

    public int getDienum() {
        return dienum;
    }

    public void setDienum(int dienum) {
        this.dienum = dienum;
    }

    public int getTaotai() {
        return taotai;
    }

    public void setTaotai(int taotai) {
        this.taotai = taotai;
    }

    public BigDecimal getWater() {
        return water;
    }

    public void setWater(BigDecimal water) {
        this.water = water;
    }

    public BigDecimal getWeight() {
        return weight;
    }

    public void setWeight(BigDecimal weight) {
        this.weight = weight;
    }

    private List<ProductionDailyDetailBean> productionList;

    @Override
    protected int setLayoutId() {
        return R.layout.act_productiondailyw;
    }

    @Override
    protected void initView() {
        initToolbar(mToolbar, getString(R.string.Productiondaily_w), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        initTabLayout();
        initViewPager();
    }

    @Override
    protected void initData() {
        productionList = new ArrayList<>();
    }

    @Override
    protected void initOper() {

    }

    private void initViewPager() {
            mFragmentList.add(new BasicattributesFragment());
            mFragmentList.add(new DetailsofdocumentsFragment());


        mViewPagerAdapter = new TabViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mViewPagerAdapter);
        mViewPager.setOffscreenPageLimit(2);
        mTabLayout.setupWithViewPager(mViewPager);
    }


    private void initTabLayout() {
            mTitleList.add(getString(R.string.Basicattributes));
            mTitleList.add(getString(R.string.Detailsofdocuments));


        for (String title : mTitleList) {
            mTabLayout.addTab(mTabLayout.newTab().setText(title));
        }

        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
    }

    @Override
    public void onStartCalendarClick(Date date) {
        setTime(date);
        BasicattributesFragment.basicattr_date.setText(DateTimeUtil.getYMD2(date));
    }

    @Override
    public void onEndCalendarClick(Date date) {

    }

    private class TabViewPagerAdapter extends FragmentPagerAdapter {
        public TabViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        DayProductRpcNew();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.save, menu);
//        menu.findItem(R.id.save_info).setVisible(true);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.save_info:
//                System.out.println("aaa1--"+getFarmId());
//                System.out.println("aaa2---"+getBatchId());
//                System.out.println("aaa3---"+getAccountId());
//                System.out.println("aaa4---"+getTime());
//                System.out.println("aaa5---"+getHouseid());
//                System.out.println("aaa6---"+getDays());
//                System.out.println("aaa7---"+getCunhuolv());
//                System.out.println("aaa8---"+getDienum());
//                System.out.println("aaa9---"+getTaotai());
//                System.out.println("aaa10---"+getWater());
//                System.out.println("aaa11---"+getWeight());

                ProductionDailyDetailBean productionBean = new ProductionDailyDetailBean();
                productionBean.setHouse_ID(getHouseid());
                productionBean.setDays(getDays());
                productionBean.setCunhuolv(getCunhuolv());
                productionBean.setDieNumber(getDienum());
                productionBean.setObsolete(getTaotai());
                productionBean.setWaterIntake(getWater());
                productionBean.setWeight(getWeight());
                productionList.add(productionBean);
                showToast("保存一条明细成功");
                break;
            case R.id.commit_info:
                DayProductRpcInsert();
                break;
        }
        return super.onOptionsItemSelected(item);
    }



    RpcObject bill;

    private void DayProductRpcNew() {
        ViewOnClickTask DayProductRpcNewTask = new ViewOnClickTask(this,"加载中...") {
            @Override
            protected void successUI() {
            }

            @Override
            public Object call() throws Exception {

                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.DayProductRpc_New);
                bill = result.getRpcObject(RpcUrl.DayProductRpc_Query_fanhui);
                return null;
            }
        };
        DayProductRpcNewTask.execute();
    }

    private void DayProductRpcInsert() {
        ViewOnClickTask DayProductRpcInsertTask = new ViewOnClickTask(this,"加载中...") {
            RpcObject rpcobj;

            @Override
            protected void successUI() {
                showToast("提交成功");
                finish();
            }

            @Override
            public Object call() throws Exception {
                ///< 带多样参数的集合
                RpcObject parmObject = bill;
                parmObject.setLong("BreedFactory_ID",getFarmId());
                parmObject.setLong("Batch_ID",getBatchId());
                parmObject.setLong("AccountingUnit_ID",getAccountId());
                parmObject.setDate("Date",getTime());

                for (int i=0;i<productionList.size();i++) {
                    RpcObject setDetails = RpcObject.create(RpcUrl.DayProductRpc_Insert_detail);
                    setDetails.setLong("House_ID", productionList.get(i).getHouse_ID());
                    setDetails.setInt("Days", productionList.get(i).getDays());
//                setDetails.setDecimal("cunhuolv",getCunhuolv());
                    setDetails.setInt("DieNumber", productionList.get(i).getDieNumber());
                    setDetails.setInt("Obsolete", productionList.get(i).getObsolete());
                    setDetails.setDecimal("WaterIntake", productionList.get(i).getWaterIntake());
                    setDetails.setDecimal("Weight", productionList.get(i).getWeight());

                    parmObject.getManyList("Details").add(setDetails);
                }

                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.DayProductRpc_Insert,parmObject);
                rpcobj = result.getRpcObject(RpcUrl.DayProductRpc_Insert_canshufanhui);
                return null;
            }
        };
        DayProductRpcInsertTask.execute();
    }
}
