package com.qhclh.ytzh.work.carsmannage;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseFragment;
import com.qhclh.ytzh.bean.MaojiScanBean;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.tasks.ViewOnClickTask;
import com.qhclh.ytzh.ui.RefreshLayout;
import com.qhclh.ytzh.utils.DateTimeUtil;

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 2017/11/30.
 */
///< 车辆管理毛鸡
public class GrossChickenFragment extends BaseFragment implements RefreshLayout.OnLoadListener {
    @BindView(R.id.lv_infos_grosschicken)
    ListView lv_infos_grosschicken;

    @BindView(R.id.rfl_gross)
    RefreshLayout rfl_gross;
    private int page =0;

    private List<MaojiScanBean> scanBeanList;
    private CarsInfosGAdapter adapter;
    @Override
    protected int setLayout() {
        return R.layout.fragment_grosschicken;
    }

    @Override
    protected void initView() {
        rfl_gross.setEnabled(false);
    }

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

    @Override
    protected void initOper() {
        lv_infos_grosschicken.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(),MaojiCarsinfoActivity.class);
                intent.putExtra("id",l);
                startActivity(intent);
            }
        });
        rfl_gross.setOnLoadListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 0;
        Query();
    }

    private void Query() {
        ViewOnClickTask queryTask = new ViewOnClickTask(getActivity(),"加载中...") {
            List<RpcObject> rpcObj;
            @Override
            protected void successUI() {
                scanBeanList.clear();
                for (int i=0;i<rpcObj.size();i++) {
                    MaojiScanBean scanBean = new MaojiScanBean();
                    scanBean.setId(rpcObj.get(i).getLong("ID"));
                    scanBean.setInfos_carname(rpcObj.get(i).getString("Car_Name"));
                    scanBean.setInfos_driver(rpcObj.get(i).getString("Employee_Name"));
                    if (rpcObj.get(i).getDate("CreateTime")!=null) {
                        scanBean.setCreateTime(DateTimeUtil.getYMD4(rpcObj.get(i).getDate("CreateTime")));
                    }else {
                        scanBean.setCreateTime("");
                    }
                    scanBeanList.add(scanBean);
                }

                adapter = new CarsInfosGAdapter(getActivity(),scanBeanList);
                lv_infos_grosschicken.setAdapter(adapter);
            }

            @Override
            public Object call() throws Exception {
                RpcObject queryObj = RpcObject.create(RpcUrl.ChickTransportLogRpc_canshu);
                queryObj.setInt("PageSize",10);
                queryObj.setInt("CurrentPageIndex",page);
                List<Object> select = queryObj.getList("Select");
                select.add("ID");
                select.add("CreateTime");
                select.add("Employee_Name");
                select.add("Car_Name");

                List<Object> orderList = queryObj.getList("OrderBy");
                RpcObject orderBy = RpcObject.create(RpcUrl.DayProductRpc_Query_order);
                orderBy.setString("Name", "CreateTime");
                orderBy.setBoolean("Desc", true);
                orderList.add(orderBy);

                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.ToFoodFactoryTransportLogRpc_Query,queryObj);
                rpcObj = result.getRpcObjectList(RpcUrl.ToFoodFactoryTransportLogRpc_fanhui);
                return null;
            }
        };
        queryTask.execute();
    }

    @Override
    public void onLoad() {
        rfl_gross.postDelayed(new Runnable() {
            @Override
            public void run() {
                rfl_gross.setLoading(false);
                querymore();
            }
        },1000);
    }

    private void querymore() {
        page++;
        ViewOnClickTask queryTask = new ViewOnClickTask(getActivity(),"加载中...") {
            List<RpcObject> rpcObj;
            @Override
            protected void successUI() {
                for (int i=0;i<rpcObj.size();i++) {
                    MaojiScanBean scanBean = new MaojiScanBean();
                    scanBean.setId(rpcObj.get(i).getLong("ID"));
                    scanBean.setInfos_carname(rpcObj.get(i).getString("Car_Name"));
                    scanBean.setInfos_driver(rpcObj.get(i).getString("Employee_Name"));
                    if (rpcObj.get(i).getDate("CreateTime")!=null) {
                        scanBean.setCreateTime(DateTimeUtil.getYMD4(rpcObj.get(i).getDate("CreateTime")));
                    }else {
                        scanBean.setCreateTime("");
                    }
                    scanBeanList.add(scanBean);
                }

                adapter.notifyDataSetChanged();
            }

            @Override
            public Object call() throws Exception {
                RpcObject queryObj = RpcObject.create(RpcUrl.ChickTransportLogRpc_canshu);
                queryObj.setInt("PageSize",10);
                queryObj.setInt("CurrentPageIndex",page);
                List<Object> select = queryObj.getList("Select");
                select.add("ID");
                select.add("CreateTime");
                select.add("Employee_Name");
                select.add("Car_Name");

                List<Object> orderList = queryObj.getList("OrderBy");
                RpcObject orderBy = RpcObject.create(RpcUrl.DayProductRpc_Query_order);
                orderBy.setString("Name", "CreateTime");
                orderBy.setBoolean("Desc", true);
                orderList.add(orderBy);

                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.ToFoodFactoryTransportLogRpc_Query,queryObj);
                rpcObj = result.getRpcObjectList(RpcUrl.ToFoodFactoryTransportLogRpc_fanhui);
                return null;
            }
        };
        queryTask.execute();
    }
}
