package com.unis.sxzcl.ui.person;

import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.unis.sxzcl.R;
import com.unis.sxzcl.base.BaseListFragment;
import com.unis.sxzcl.base.base_detail.BaseDetail;
import com.unis.sxzcl.base.base_detail.BaseDetailAdapter;
import com.unis.sxzcl.base.base_detail.BaseDetailBean;
import com.unis.sxzcl.databinding.FragmentBaseListBinding;


/**
 * 包名: com.unis.sxzcl.ui.person
 * 类名: PersonInfoFragment
 * 作用描述:
 * 创建人: mQ
 * 创建时间: 2020/12/2 14:35
 * 更新人:
 * 更新时间: 2020/12/2 14:35
 * 更新说明:
 * 版本号: 1.0
 */
public class PersonInfoFragment extends BaseListFragment<FragmentBaseListBinding> implements OnItemClickListener {

    private String mData;
    private BaseDetailAdapter mAdapter;

    /**
     * 创建实例
     */
    public static PersonInfoFragment newFragment(String extra) {
        Bundle bundle = new Bundle();
        bundle.putString("data", extra);
        PersonInfoFragment fragment = new PersonInfoFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onRefreshData() {
        getData(true);
    }

    @Override
    protected FragmentBaseListBinding getViewBinding(LayoutInflater inflater, ViewGroup container) {
        return FragmentBaseListBinding.inflate(inflater, container, false);
    }

    @Override
    protected void init() {
        Bundle bundle = getArguments();
        mData = bundle.getString("data");

        mAdapter = new BaseDetailAdapter();
        mAdapter.setOnItemClickListener(this);
        initBaseListView(mBinding.refreshLayout, mBinding.recyclerView, mAdapter, false);

    }

    @Override
    protected void onFirstShow() {
        onRefresh();
    }

    private void getData(boolean refresh) {
        new Handler().postDelayed(() -> {
            if (refresh) {
                mAdapter.getData().clear();
                mAdapter.notifyDataSetChanged();
            }
            mAdapter.addData(new BaseDetailBean(BaseDetail.TITLE_IMAGE, "头像", ""));
            mAdapter.addData(new BaseDetailBean(BaseDetail.TITLE_CONTENT, "姓名", "张三"));
            mAdapter.addData(new BaseDetailBean(BaseDetail.TITLE_CONTENT, "所属公司", "太原直属库公司"));
            mAdapter.addData(new BaseDetailBean(BaseDetail.TITLE_CONTENT, "部门", "仓储管理处"));
            mAdapter.addData(new BaseDetailBean(BaseDetail.TITLE_CONTENT, "职位", "保管员"));
            mAdapter.addData(new BaseDetailBean(BaseDetail.TITLE_CONTENT, "更多").setNextIcon(true));
            loadComplete();
        }, 500);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        BaseDetailBean bean = mAdapter.getData().get(position);
        if (bean.getTitle().equals("更多")) {
            Bundle bundle = new Bundle();
            bundle.putString("data", getGson().toJson(bean));
            navigation(R.id.personDetailFragment, bundle);
        }
    }
}
