package com.qhclh.ytzh.work.productiondaily;

import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

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

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2017/12/4.
 */

public class ProductiondailyInfoActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.lv_detail)
    ListView lv_detail;
    private TextView production_info_batch;
    private TextView production_info_farm;
    private TextView production_info_account;
    private TextView production_info_date;
    private long id;

    private List<ProductionDailyDetailBean> productionDailyDetailBeanList;
    private ProductiondailydetailAdapter productiondailydetailAdapter;
    @Override
    protected int setLayoutId() {
        return R.layout.act_productiondaily_info;
    }

    @Override
    protected void initView() {
        initToolbar(mToolbar, "生产日报详细", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        View header = LayoutInflater.from(this).inflate(R.layout.lv_header, null);
        production_info_batch = header.findViewById(R.id.production_info_batch);
        production_info_farm = header.findViewById(R.id.production_info_farm);
        production_info_account = header.findViewById(R.id.production_info_account);
        production_info_date = header.findViewById(R.id.production_info_date);
        lv_detail.addHeaderView(header);
    }

    @Override
    protected void initData() {
        id = getIntent().getLongExtra("id",0);
        productionDailyDetailBeanList = new ArrayList<>();
    }

    @Override
    protected void initOper() {

    }

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

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

            @Override
            protected void successUI() {
                production_info_batch.setText(rpcobj.getString("Batch_Name"));
                production_info_farm.setText(rpcobj.getString("BreedFactory_Name"));
                production_info_account.setText(rpcobj.getString("AccountingUnit_Name"));
                production_info_date.setText(DateTimeUtil.getYMD2(rpcobj.getDate("Date")));
                List<RpcObject> rpcObjectList = rpcobj.getManyList("Details");

                for (int i=0;i<rpcObjectList.size();i++){
                    ProductionDailyDetailBean productionDailyDetailBean = new ProductionDailyDetailBean();
                    if (rpcObjectList.get(i).getLong("House_ID")!=null) {
                        productionDailyDetailBean.setHouse_ID(rpcObjectList.get(i).getLong("House_ID"));
                    }else {
                        productionDailyDetailBean.setHouse_ID(-1000);
                    }
                    if (rpcObjectList.get(i).getString("House_Name")!=null){
                        productionDailyDetailBean.setHouse_Name(rpcObjectList.get(i).getString("House_Name"));
                    }else {
                        productionDailyDetailBean.setHouse_Name("");
                    }
                    if (rpcObjectList.get(i).getInt("Days")!=null){
                        productionDailyDetailBean.setDays(rpcObjectList.get(i).getInt("Days"));
                    }else {
                        productionDailyDetailBean.setDays(-1000);
                    }
                    productionDailyDetailBean.setCunhuolv(new BigDecimal("-1000"));
                    if (rpcObjectList.get(i).getInt("DieNumber")!=null){
                        productionDailyDetailBean.setDieNumber(rpcObjectList.get(i).getInt("DieNumber"));
                    }else {
                        productionDailyDetailBean.setDieNumber(-1000);
                    }
                    if (rpcObjectList.get(i).getInt("Obsolete")!=null){
                        productionDailyDetailBean.setObsolete(rpcObjectList.get(i).getInt("Obsolete"));
                    }else {
                        productionDailyDetailBean.setObsolete(-1000);
                    }
                    if (rpcObjectList.get(i).getDecimal("WaterIntake")!=null){
                        productionDailyDetailBean.setWaterIntake(rpcObjectList.get(i).getDecimal("WaterIntake"));
                    }else {
                        productionDailyDetailBean.setWaterIntake(new BigDecimal(-1000));
                    }
                    if (rpcObjectList.get(i).getDecimal("Weight")!=null){
                        productionDailyDetailBean.setWeight(rpcObjectList.get(i).getDecimal("Weight"));
                    }else {
                        productionDailyDetailBean.setWeight(new BigDecimal(-1000));
                    }
                    productionDailyDetailBeanList.add(productionDailyDetailBean);
                }
                productiondailydetailAdapter = new ProductiondailydetailAdapter(ProductiondailyInfoActivity.this,productionDailyDetailBeanList);
                lv_detail.setAdapter(productiondailydetailAdapter);
            }

            @Override
            public Object call() throws Exception {

                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.DayProductRpc_Load,id);
                rpcobj = result.getRpcObject(RpcUrl.DayProductRpc_Query_fanhui);
                return null;
            }
        };
        DayProductRpcLoadTask.execute();
    }
}
