package com.example.administrator.zhapp.activity.ydckgl;

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

import com.example.administrator.zhapp.BaseListViewAdapter.CommonAdapter;
import com.example.administrator.zhapp.BaseListViewAdapter.ViewHolder;
import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.activity.BaseActivity;
import com.example.administrator.zhapp.contans.Contans;
import com.example.administrator.zhapp.entity.PdjhInfo;
import com.example.administrator.zhapp.utils.GsonUtils;
import com.example.administrator.zhapp.utils.HttpListener;
import com.example.administrator.zhapp.utils.NohttpUtils;
import com.example.administrator.zhapp.utils.SPUtils;
import com.example.administrator.zhapp.utils.UiUtlis;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.RequestQueue;
import com.yolanda.nohttp.rest.Response;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 移动仓库管理-盘点计划列表
 */
public class Kcpd2Activity extends BaseActivity {


    ListView lv_pdjh;
    private PdjhInfo pdjhInfo;
    private List<PdjhInfo.Pdjh> list;
    private CommonAdapter<PdjhInfo.Pdjh> cAdapter;

    @Override
    public int getContentViewID() {
        return R.layout.activity_kcpd2;
    }

    protected void initView() {
        lv_pdjh = findViewById(R.id.lv_pdjh);
    }

    public void initData() {
        setTitle("库存盘点计划列表");
        setBack();
        RequestData();
    }

    private void RequestData() {
        Request<String> request = NoHttp.createStringRequest(Contans.IP + Contans.PDJH, RequestMethod.GET);
        request.set("tsruserid", (String) SPUtils.get(context, Contans.USERNAME, ""));
        NohttpUtils.getInstance().add(Kcpd2Activity.this, 0, request, callback, true, true, "加载计划中...");
    }

    private HttpListener<String> callback = new HttpListener<String>() {
        @Override
        public void onSucceed(int what, Response<String> response) {

            pdjhInfo = GsonUtils.GsonToBean(response.get(), PdjhInfo.class);
            if (pdjhInfo != null) {
                if (pdjhInfo.getTotal() != 0) {
                    list = pdjhInfo.getRows();
                    setList();
                } else {
                    showToast("没有盘点计划");
                }
            } else {
                showToast(UiUtlis.getString(context, R.string.data_error));
            }
        }

        @Override
        public void onFailed(int what, Response<String> response) {

        }
    };

    private void setList() {
        if (cAdapter == null) {
            cAdapter = new CommonAdapter<PdjhInfo.Pdjh>(context, R.layout.pdjh_item, list) {
                @Override
                protected void convert(ViewHolder viewHolder, final PdjhInfo.Pdjh item, int position) {
                    viewHolder.setText(R.id.tv_pch, "批次号：" + item.getPch());
                    viewHolder.setText(R.id.tv_time, DateFormat(item.getPdkssj()) + "至" + DateFormat(item.getPdjssj()));

                    viewHolder.setOnClickListener(R.id.btn_cx, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(Kcpd2Activity.this, KcpdCxActivity.class);
                            intent.putExtra(Contans.KEY_PDID, item.getPdid());
                            intent.putExtra("pch", item.getPch());
                            startActivity(intent);
                        }
                    });

                    viewHolder.setOnClickListener(R.id.btn_pd, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(Kcpd2Activity.this, KcpdActivity.class);
                            intent.putExtra(Contans.KEY_PDID, item.getPdid());
                            startActivity(intent);
                        }
                    });

                }
            };

            lv_pdjh.setAdapter(cAdapter);
        } else {
            cAdapter.notifyDataSetChanged();
        }
    }

    //转化日期格式
    private String DateFormat(String str) {

        String result = "";
        SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd");
        Date date = null;
        try {
            date = sf.parse(str);
            result = sf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}