package com.tjzhxx.union.activity;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import androidx.lifecycle.Lifecycle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.tjzhxx.union.R;
import com.tjzhxx.union.adapter.RescuestyleAdapter;
import com.tjzhxx.union.entity.BaseResponse;
import com.tjzhxx.union.entity.Doclist;
import com.tjzhxx.union.entity.Rescuestyle;
import com.tjzhxx.union.entity.request.DataXcjzRequest;
import com.tjzhxx.union.public_store.ConstDefine;
import com.tjzhxx.union.public_store.retrofit.RetrofitUtils;
import com.tjzhxx.union.public_store.retrofit.UnionServices;
import com.tjzhxx.union.public_store.rx.DefaultSubscriber;
import com.tjzhxx.union.public_store.rx.ProgressSubscriber;
import com.tjzhxx.union.system.BaseActivity;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 描述:
 *
 * @author yhx
 * @create 2021/9/7  11:45 PM
 */
public class DataEditeXcjzActivity extends BaseActivity {

    @BindView(R.id.name)
    EditText name;
    @BindView(R.id.unit)
    EditText unit;
    @BindView(R.id.price)
    EditText price;
    @BindView(R.id.income)
    EditText income;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.submit)
    TextView submit;
    private List<Rescuestyle> list = new ArrayList<>();
    private List<Doclist> doclist = new ArrayList<>();
    private RescuestyleAdapter adapter;
    private UnionServices services;

    @Override
    public int getContentViewLayoutID() {
        return R.layout.activity_data_edite_xcjz;
    }

    @Override
    public void bindViewAndEvent(Bundle savedInstanceState) {
        initTitle("现场救助资料填写");
        services = RetrofitUtils.createApi(UnionServices.class, ConstDefine.HttpAdress);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RescuestyleAdapter(this, list);
        recyclerView.setAdapter(adapter);
        queryrescuelistforapp();
    }
    @OnClick(R.id.submit)
    public void onClick() {
        doclist.clear();
        for (Rescuestyle rescuestyle:list){
            if (rescuestyle.getIsmust().equals("1") && TextUtils.isEmpty(rescuestyle.getImageUrl())){
                showSnackBar("请上传"+rescuestyle.getDockind());
                return;
            }else {
                Doclist bean = new Doclist();
                bean.setDocstyle(rescuestyle.getDockind());
                bean.setDocurl(rescuestyle.getImageUrl());
                doclist.add(bean);
            }
        }
        DataXcjzRequest request = new DataXcjzRequest();
        request.setApplytelno(ConstDefine.userInfo.getTelno());
        request.setWid(ConstDefine.userInfo.getWid());
        request.setRescuestyle("1");
        request.setDoclist(new Gson().toJson(doclist));
        subapplywithdocbywidforapp(request);
    }
    private void queryrescuelistforapp() {
        services.queryrescuelistforapp(1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse<List<Rescuestyle>>>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new DefaultSubscriber<BaseResponse<List<Rescuestyle>>>(this) {
                    @Override
                    public void success(BaseResponse<List<Rescuestyle>> value) {
                        if (value.getData() != null) {
                            list.clear();
                            list.addAll(value.getData());
                            adapter.notifyDataSetChanged();
                        }
                    }

                });
    }

    private void subapplywithdocbywidforapp(DataXcjzRequest request) {
        services.subapplywithdocbywidforapp(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .as(AutoDispose.<BaseResponse>autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(new ProgressSubscriber<BaseResponse>(this) {
                    @Override
                    public void success(BaseResponse value) {
                        showSnackBar("提交成功");
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                finish();
                            }
                        }, 1500);
                    }

                });
    }


}
