package com.qhclh.ytzh.work.Hatchery;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
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.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 青花瓷 on 2018/4/2.
 */

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

    private List<HatcheryBean> list;
    private HatcheryAdapter adapter;
    @Override
    protected int setLayoutId() {
        return R.layout.act_hatchery;
    }

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

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

    @Override
    protected void initOper() {
        lv_hatchery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                System.out.println("aaa+++id+++"+l);
                Intent intent = new Intent(HatcheryActivity.this,HatcheryReportActivity.class);
                intent.putExtra("hatchingStoreID",l);
                intent.putExtra("HatchName",list.get(i).getName());
                startActivity(intent);
            }
        });
    }

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

    private void GetChoiceBoxWords() {
        ViewOnClickTask hatcheryTask = new ViewOnClickTask(this,"加载中...") {
            List<RpcObject> rpcobj;
            @Override
            protected void successUI() {
                list.clear();
                for (int i =0;i<rpcobj.size();i++){
                    list.add(new HatcheryBean(Long.parseLong(rpcobj.get(i).getString("Item1")),rpcobj.get(i).getString("Item2")));
                }
                adapter = new HatcheryAdapter(HatcheryActivity.this,list);
                lv_hatchery.setAdapter(adapter);
            }

            @Override
            public Object call() throws Exception {
                JsonRpcResult result = RpcFacade.rpcCall(RpcUrl.GetChoiceBoxWords,"B3ProduceUnitedInfos_属性是孵化厂的受权限树控制的生产单位","","");
                rpcobj = result.getRpcObjectList(RpcUrl.GetChoiceBoxWords_fanhui);
                return null;
            }
        };
        hatcheryTask.execute();
    }
}
