package com.qhclh.ytzh.work.productiondailyw;

import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.bean.DailyDetailsBean;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.tasks.ViewOnClickTask;

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.List;

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2017/12/7.
 */
///< 重新写 生产日报
public class WdailyActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.wdaily_lv)
    ListView wdaily_lv;

    private List<DailyDetailsBean> list;
    private DailyDetailsAdapter adapter;

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

    @Override
    protected void initView() {
        initToolbar(mToolbar, "生产日报", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

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

    @Override
    protected void initOper() {

    }

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

    private RpcObject rpcObj;

    private String houseid;
    private String housename;
    private String days;
    private String dienumber;
    private String obs;
    private String water;
    private String weight;
    private void LoadOrCreateEditingDayProduct() {
        ViewOnClickTask LoadOrCreateEditingDayProductTask = new ViewOnClickTask(this, "加载中...") {
            @Override
            protected void successUI() {
                List<RpcObject> detailsList = rpcObj.getManyList("Details");
                for (int i = 0; i < detailsList.size(); i++) {
                    if (detailsList.get(i).getLong("House_ID")!=null) {
                         houseid = detailsList.get(i).getLong("House_ID")+"";
                    }else {
                        houseid = "";
                    }

                    if (detailsList.get(i).getString("House_Name")!=null) {
                        housename = detailsList.get(i).getString("House_Name");
                    }else {
                        housename = "";
                    }

                    if (detailsList.get(i).getInt("Days")!=null) {
                        days = detailsList.get(i).getInt("Days")+"";
                    }else {
                        days = "";
                    }

                    if (detailsList.get(i).getInt("DieNumber")!=null) {
                        dienumber = detailsList.get(i).getInt("DieNumber")+"";
                    }else {
                        dienumber = "";
                    }

                    if (detailsList.get(i).getInt("Obsolete")!=null) {
                        obs = detailsList.get(i).getInt("Obsolete")+"";
                    }else {
                        obs = "";
                    }

                    if (detailsList.get(i).getDecimal("WaterIntake")!=null) {
                        water = detailsList.get(i).getDecimal("WaterIntake")+"";
                    }else {
                        water ="";
                    }

                    if (detailsList.get(i).getDecimal("Weight")!=null) {
                        weight = detailsList.get(i).getDecimal("Weight")+"";
                    }else {
                        weight = "";
                    }
                    list.add(new DailyDetailsBean(houseid, housename
                            , days, dienumber
                            , obs, water
                            , weight, "单据明细" + (i+1)));
                }
                adapter = new DailyDetailsAdapter(WdailyActivity.this, list);
                wdaily_lv.setAdapter(adapter);

            }

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.LoadOrCreateEditingDayProduct, new Object[]{null});
                rpcObj = result.getRpcObject(RpcUrl.LoadOrCreateEditingDayProduct_fanhui);
                return null;
            }
        };
        LoadOrCreateEditingDayProductTask.execute();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.commit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.commit_dailys:
                commit();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void commit() {
        ViewOnClickTask updateTask = new ViewOnClickTask(this,"加载中...") {
            RpcObject rpcobj;
            @Override
            protected void successUI() {
                showToast("提交成功");
                finish();
            }
            @Override
            public Object call() throws Exception {

                ///< 带多样参数的集合
                RpcObject parmObject = rpcObj;

                try {
                    for (int i = 0; i < list.size(); i++) {
                        RpcObject setDetails = parmObject.getManyList("Details").get(i);

//                        if (list.get(i).getDays()!=null && !"".equals(list.get(i).getDays())) {
//                            setDetails.setInt("Days", Integer.parseInt(list.get(i).getDays()));
//                        }else {
//                            setDetails.setInt("Days",null);
//                        }

                        if (list.get(i).getDieNumber()!=null && !"".equals(list.get(i).getDieNumber())) {
                            setDetails.setInt("DieNumber", Integer.parseInt(list.get(i).getDieNumber()));
                        }else {
                            setDetails.setInt("DieNumber",null);
                        }

                        if (list.get(i).getObsolete()!=null && !"".equals(list.get(i).getObsolete())) {
                            setDetails.setInt("Obsolete", Integer.parseInt(list.get(i).getObsolete()));
                        }else {
                            setDetails.setInt("Obsolete",null);
                        }

                        if (list.get(i).getWaterIntake()!=null && !"".equals(list.get(i).getWaterIntake())) {
                            setDetails.setDecimal("WaterIntake", new BigDecimal(list.get(i).getWaterIntake()));
                        }else {
                            setDetails.setDecimal("WaterIntake",null);
                        }

                        if (list.get(i).getWeight()!=null && !"".equals(list.get(i).getWeight())) {
                            setDetails.setDecimal("Weight", new BigDecimal(list.get(i).getWeight()));
                        }else {
                            setDetails.setDecimal("Weight",null);
                        }
                    }

                    JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.DayProductRpc_Update, parmObject);
                    rpcobj = result.getRpcObject(RpcUrl.DayProductRpc_Update_canshu_fanhui);
                }catch (NumberFormatException e)
                {
                    e.printStackTrace();
                }
                return null;
            }
        };
        updateTask.execute();
    }
}
