package com.qhclh.ytzh.work.weighingsingle;

import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
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.List;

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2018/3/12.
 */

public class WeightInfoActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_toolbar_title)
    TextView tv_toolbar_title;
    @BindView(R.id.et_grossweight)
    EditText et_grossweight;
    @BindView(R.id.et_tare)
    EditText et_tare;
    @BindView(R.id.tv_commit_check)
    TextView tv_commit_check;

    private long id;
    @Override
    protected int setLayoutId() {
        return R.layout.act_weightinfos;
    }

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

    @Override
    protected void initData() {
        id = getIntent().getLongExtra("id",0);
        tv_toolbar_title.setText(getString(R.string.guobandanluru)+"No."+id);
    }

    private String grossweight = "";
    private String tare = "";
    @Override
    protected void initOper() {
        tv_commit_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                grossweight = et_grossweight.getText().toString().trim();
                tare = et_tare.getText().toString().trim();
                if ("".equals(grossweight) || grossweight==null){
                    showToast("请输入毛重");
                    return;
                }
                if ("".equals(tare) || tare==null){
                    showToast("请输入皮重");
                    return;
                }
                update();
            }
        });
    }

    private void update() {
        ViewOnClickTask updateTask = new ViewOnClickTask(this,"加载中...") {
            @Override
            protected void successUI() {
                check();
            }
            @Override
            public Object call() throws Exception {
                ///< 带多样参数的集合
                RpcObject parmObject = rpcObj;
                RpcObject details = parmObject.getManyList("Detail").get(0);
                details.setMoney("GrossWeight",new BigDecimal(grossweight));
                details.setMoney("Tare",new BigDecimal(tare));
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.WeighRpc_update,parmObject);
                return null;
            }
        };
        updateTask.execute();
    }

    private void check() {
        ViewOnClickTask checkTask = new ViewOnClickTask(this,"加载中...") {
            @Override
            protected void successUI() {
                showToast("审核成功");
                finish();
            }
            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.WeighRpc_check,id);
                return null;
            }
        };
        checkTask.execute();
    }

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

    private RpcObject rpcObj;
    private void load() {
        ViewOnClickTask loadTask = new ViewOnClickTask(this,"加载中...") {
            @Override
            protected void successUI() {
                List<RpcObject> detailsList = rpcObj.getManyList("Detail");
                if (detailsList!=null && detailsList.size()>0) {
                    if (detailsList.get(0).getMoney("GrossWeight") != null) {
                        et_grossweight.setText(detailsList.get(0).getMoney("GrossWeight").toString());
                    }
                    if (detailsList.get(0).getMoney("Tare") != null) {
                        et_tare.setText(detailsList.get(0).getMoney("Tare").toString());
                    }
                }
            }
            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.WeighRpc_load,id);
                rpcObj = result.getRpcObject(RpcUrl.WeightAppList_fanhui);
                return null;
            }
        };
        loadTask.execute();
    }
}
