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.ScanBean;
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 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 2017/11/30.
 */
///< 车辆管理鸡苗
public class ChicksFragment extends BaseFragment implements RefreshLayout.OnLoadListener {
    @BindView(R.id.lv_infos_chicks)
    ListView lv_infos_chicks;
    private List<ScanBean> scanBeanList;
    private CarsInfosAdapter adapter;
    @BindView(R.id.rfl_chicks)
    RefreshLayout rfl_chicks;
    private int page =0;
    @Override
    protected int setLayout() {
        return R.layout.fragment_chicks;
    }

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

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

    @Override
    protected void initOper() {
        lv_infos_chicks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(),FumudaiCarsinfoActivity.class);
                intent.putExtra("id",l);
                startActivity(intent);
            }
        });
        rfl_chicks.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++) {
                    ScanBean scanBean = new ScanBean();
                    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 CarsInfosAdapter(getActivity(),scanBeanList);
                lv_infos_chicks.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.ChickTransportLogRpc_Query,queryObj);
                rpcObj = result.getRpcObjectList(RpcUrl.ChickTransportLogRpc_fanhui);
                return null;
            }
        };
        queryTask.execute();
    }

    @Override
    public void onLoad() {
        rfl_chicks.postDelayed(new Runnable() {
            @Override
            public void run() {
                rfl_chicks.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++) {
                    ScanBean scanBean = new ScanBean();
                    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.ChickTransportLogRpc_Query,queryObj);
                rpcObj = result.getRpcObjectList(RpcUrl.ChickTransportLogRpc_fanhui);
                return null;
            }
        };
        queryTask.execute();
    }
}
