package com.qhclh.ytzh.work.productiondaily;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.base.BaseActivity;
import com.qhclh.ytzh.bean.ProductionDailyBean;
import com.qhclh.ytzh.home.RpcUrl;
import com.qhclh.ytzh.tasks.ViewOnClickTask;
import com.qhclh.ytzh.ui.CalendarDialogFragment;
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.Date;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2017/12/4.
 */
///< 生产日报
public class ProductiondailyActivity extends BaseActivity implements CalendarDialogFragment.CalendarClickListener, RefreshLayout.OnLoadListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.lv_productiondaily)
    ListView lv_productiondaily;
    @BindView(R.id.rfl_productiondaily)
    RefreshLayout rfl_productiondaily;
    private int page =0;
    private List<ProductionDailyBean> productionDailyBeanList = null;
    private ProductiondailyAdapter productiondailyAdapter;
    @Override
    protected int setLayoutId() {
        return R.layout.act_productiondaily;
    }

    @Override
    protected void initView() {
        initToolbar(mToolbar, getString(R.string.Productiondaily), new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        rfl_productiondaily.setEnabled(false);
    }

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

    }

    @Override
    protected void initOper() {
        lv_productiondaily.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Intent intent = new Intent(ProductiondailyActivity.this,ProductiondailyInfoActivity.class);
                Intent intent = new Intent(ProductiondailyActivity.this,ShowInfoActivity.class);
                intent.putExtra("id",l);
                startActivity(intent);
            }
        });
        rfl_productiondaily.setOnLoadListener(this);
        rfl_productiondaily.setOnRefreshListener(this);
    }

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

        page =0;
        DayProductRpcQuery();
    }

    private void DayProductRpcQuery() {
        ViewOnClickTask DayProductRpcQueryTask = new ViewOnClickTask(this,"加载中...") {
            List<RpcObject> rpcobj;

            @Override
            protected void successUI() {
                int size = rpcobj.size();
                productionDailyBeanList.clear();
                if (size<=0){
                    return;
                }
                for (int i=0;i<size;i++){
                    productionDailyBeanList.add(new ProductionDailyBean(rpcobj.get(i).getLong("ID")
                            ,rpcobj.get(i).getString("Batch_Name"),rpcobj.get(i).getString("BreedFactory_Name")
                            ,DateTimeUtil.getYMD2(rpcobj.get(i).getDate("Date")),rpcobj.get(i).getNamedValue("BillState").getName()));
                }
                productiondailyAdapter = new ProductiondailyAdapter(ProductiondailyActivity.this,productionDailyBeanList);
                lv_productiondaily.setAdapter(productiondailyAdapter);
            }

            @Override
            public Object call() throws Exception {
                RpcObject queryObj = RpcObject.create(RpcUrl.DayProductRpc_Query_canshu);
                queryObj.setInt("PageSize",10);
                queryObj.setInt("CurrentPageIndex",page);
                List<Object> select = queryObj.getList("Select");
                select.add("ID");
//                select.add("CreateTime");
//                select.add("CreateUser_Name");
                select.add("Batch_ID");
                select.add("Batch_Name");
                select.add("BreedFactory_ID");
                select.add("BreedFactory_Name");
                select.add("Date");
                select.add("BillState");
//                select.add("Remark");
//                select.add("AccountingUnit_ID");
//                select.add("AccountingUnit_Name");

//                Map<Object, Object> where = queryObj.getMap("Where");
//                where.put("MobileToDo",true);

//                List<Object> orderList = queryObj.getList("OrderBy");
//                RpcObject orderBy1 = RpcObject.create(RpcUrl.paixu);
//                orderBy1.setString("Name", "CreateTime");
//                orderBy1.setBoolean("Desc", true);
//                orderList.add(orderBy1);
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.DayProductRpc_Query,queryObj);
                rpcobj = result.getRpcObjectList(RpcUrl.DayProductRpc_Query_fanhui);
                return null;
            }
        };
        DayProductRpcQueryTask.execute();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.chosetime, menu);
        menu.findItem(R.id.chose_time).setVisible(true);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.chose_time:
                    new CalendarDialogFragment().show(getSupportFragmentManager(), "start");
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onStartCalendarClick(Date date) {
        showToast(DateTimeUtil.FormatDate(date));
//        DayProductRpcQueryByt(date);
    }

    private void DayProductRpcQueryByt(Date time) {
        ViewOnClickTask DayProductRpcQuerytTask = new ViewOnClickTask(this,"加载中...") {
            List<RpcObject> rpcobj;

            @Override
            protected void successUI() {
                int size = rpcobj.size();
                productionDailyBeanList.clear();
                if (size<=0){
                    return;
                }
                for (int i=0;i<size;i++){
                    productionDailyBeanList.add(new ProductionDailyBean(rpcobj.get(i).getLong("ID")
                            ,rpcobj.get(i).getString("Batch_Name"),rpcobj.get(i).getString("BreedFactory_Name")
                            ,DateTimeUtil.getYMD2(rpcobj.get(i).getDate("Date"))));
                }
                productiondailyAdapter = new ProductiondailyAdapter(ProductiondailyActivity.this,productionDailyBeanList);
                lv_productiondaily.setAdapter(productiondailyAdapter);
            }

            @Override
            public Object call() throws Exception {
                RpcObject queryObj = RpcObject.create(RpcUrl.DayProductRpc_Query_canshu);
//                queryObj.setInt("PageSize",10);
//                queryObj.setInt("CurrentPageIndex",0);
                List<Object> select = queryObj.getList("Select");
                select.add("ID");
//                select.add("CreateTime");
//                select.add("CreateUser_Name");
                select.add("Batch_ID");
                select.add("Batch_Name");
                select.add("BreedFactory_ID");
                select.add("BreedFactory_Name");
                select.add("Date");
//                select.add("Remark");
//                select.add("AccountingUnit_ID");
//                select.add("AccountingUnit_Name");

//                Map<Object, Object> where = queryObj.getMap("Where");
//                where.put("",time);

//                List<Object> orderList = queryObj.getList("OrderBy");
//                RpcObject orderBy1 = RpcObject.create(RpcUrl.DayProductRpc_Query_order);
//                orderBy1.setString("Name", "CreateTime");
//                orderBy1.setBoolean("Desc", true);
//                orderList.add(orderBy1);
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.DayProductRpc_Query,queryObj);
                rpcobj = result.getRpcObjectList(RpcUrl.DayProductRpc_Query_fanhui);
                return null;
            }
        };
        DayProductRpcQuerytTask.execute();
    }

    @Override
    public void onEndCalendarClick(Date date) {
        showToast(DateTimeUtil.FormatDate(date));
    }

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

    private void DayProductRpcQueryplus() {
        page++;
        ViewOnClickTask DayProductRpcQuerypTask = new ViewOnClickTask(this,"加载中...") {
            List<RpcObject> rpcobj;

            @Override
            protected void successUI() {
                int size = rpcobj.size();
                for (int i=0;i<size;i++){
                    productionDailyBeanList.add(new ProductionDailyBean(rpcobj.get(i).getLong("ID")
                            ,rpcobj.get(i).getString("Batch_Name"),rpcobj.get(i).getString("BreedFactory_Name")
                            ,DateTimeUtil.getYMD2(rpcobj.get(i).getDate("Date"))));
                }
                productiondailyAdapter.notifyDataSetChanged();
            }

            @Override
            public Object call() throws Exception {
                RpcObject queryObj = RpcObject.create(RpcUrl.DayProductRpc_Query_canshu);
                queryObj.setInt("PageSize",10);
                queryObj.setInt("CurrentPageIndex",page);
                List<Object> select = queryObj.getList("Select");
                select.add("ID");
//                select.add("CreateTime");
//                select.add("CreateUser_Name");
                select.add("Batch_ID");
                select.add("Batch_Name");
                select.add("BreedFactory_ID");
                select.add("BreedFactory_Name");
                select.add("Date");
//                select.add("Remark");
//                select.add("AccountingUnit_ID");
//                select.add("AccountingUnit_Name");

//                Map<Object, Object> where = queryObj.getMap("Where");
//                where.put("MobileToDo",true);

//                List<Object> orderList = queryObj.getList("OrderBy");
//                RpcObject orderBy1 = RpcObject.create(RpcUrl.paixu);
//                orderBy1.setString("Name", "CreateTime");
//                orderBy1.setBoolean("Desc", true);
//                orderList.add(orderBy1);
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.DayProductRpc_Query,queryObj);
                rpcobj = result.getRpcObjectList(RpcUrl.DayProductRpc_Query_fanhui);
                return null;
            }
        };
        DayProductRpcQuerypTask.execute();
    }

    @Override
    public void onRefresh() {
        rfl_productiondaily.setRefreshing(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        page =0;
        if (productionDailyBeanList!=null){
            productionDailyBeanList.clear();
            productionDailyBeanList = null;
        }
    }
}
