package com.qhclh.ytzh.work.carsmannage;

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

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.bean.MaojiScanBean;
import com.qhclh.ytzh.bean.ScanBean;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.tasks.ViewOnClickTask;
import com.qhclh.ytzh.utils.DateTimeUtil;
import com.qhclh.ytzh.work.scan.MaojiScanAdapter;
import com.qhclh.ytzh.work.scan.ScanAdapter;

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

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2018/1/22.
 */

public class MaojiCarsinfoActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.lv_infos)
    ListView lv_infos;

    private long id;
    private List<MaojiScanBean> scanBeanList;
    private MaojiScanAdapter scanAdapter;
    @Override
    protected int setLayoutId() {
        return R.layout.act_scanresult;
    }

    @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);
        scanBeanList = new ArrayList<>();
    }

    @Override
    protected void initOper() {

    }

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

    private void load() {
        ViewOnClickTask loadTask = new ViewOnClickTask(this,"加载中...") {
            RpcObject rpcObj;
            @Override
            protected void successUI() {
                scanBeanList.clear();
                MaojiScanBean scanBean = new MaojiScanBean();
                scanBean.setId(rpcObj.getLong("ID"));
                scanBean.setInfos_carname(rpcObj.getString("Car_Name"));
                scanBean.setInfos_driver(rpcObj.getString("Employee_Name"));
                scanBean.setInfos_yangzhichang(rpcObj.getString("BreedFactory_Name"));
                scanBean.setShipingchang(rpcObj.getString("FoodFactory_Name"));

                if (rpcObj.getDate("InBreedFactoryTime")!=null) {
                    scanBean.setDaodayangzhichang(DateTimeUtil.getYMD4(rpcObj.getDate("InBreedFactoryTime")));
                }else {
                    scanBean.setDaodayangzhichang("");
                }
                if (rpcObj.getDate("OutBreedFactoryTime")!=null) {
                    scanBean.setLikaiyangzhichang(DateTimeUtil.getYMD4(rpcObj.getDate("OutBreedFactoryTime")));
                }else {
                    scanBean.setLikaiyangzhichang("");
                }
                if (rpcObj.getDate("ArriveFoodFactoryTime")!=null) {
                    scanBean.setDaodashipingchang(DateTimeUtil.getYMD4(rpcObj.getDate("ArriveFoodFactoryTime")));
                }else {
                    scanBean.setDaodashipingchang("");
                }
                if (rpcObj.getDate("ArriveUnloadingPointTime")!=null) {
                    scanBean.setDaodaguajipingtai(DateTimeUtil.getYMD4(rpcObj.getDate("ArriveUnloadingPointTime")));
                }else {
                    scanBean.setDaodaguajipingtai("");
                }

                if (rpcObj.getString("Employee_HandPhone")!=null) {
                    scanBean.setInfos_phone(rpcObj.getString("Employee_HandPhone"));
                }else {
                    scanBean.setInfos_phone("");
                }
                scanBeanList.add(scanBean);

                scanAdapter = new MaojiScanAdapter(MaojiCarsinfoActivity.this,scanBeanList);
                lv_infos.setAdapter(scanAdapter);
            }

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.ToFoodFactoryTransportLogRpc_Load,id);
                rpcObj = result.getRpcObject(RpcUrl.ToFoodFactoryTransportLogRpc_fanhui);
                return null;
            }
        };
        loadTask.execute();
    }
}
