package com.wd.workoffice.ui.activity.bat.work;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.event.AddDeviceEvent;
import com.wd.workoffice.bean.vo.DeviceRepairVo;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.bat.store.StoreOrderSuccessActivity;
import com.wd.workoffice.ui.adapter.DeviceRepairProductAdapter;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import io.reactivex.disposables.Disposable;

/**
 * 设备维修-确认订单
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class DeviceBuyRepairConfirmActivity extends WorkToolBarActivity {

    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_ok)
    TextView tvOk;
    @BindView(R.id.ll_bottom)
    RelativeLayout llBottom;
    @BindView(R.id.tv_dep)
    TextView tvDep;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.rv_pro)
    RecyclerView rvPro;
    @BindView(R.id.et_input5)
    EditText etInput5;
    @BindView(R.id.tv_input5)
    TextView tvInput5;
    private List<DeviceRepairVo> proList;
    private DeviceRepairProductAdapter proAdapter;
    private Map<String, Object> param;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        rvPro.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
    }


    @Override
    protected void initData() {
        tvDep.setText(getIntent().getStringExtra("depName"));
        tvPrice.setText(getIntent().getStringExtra("amount"));
        tvTime.setText(getIntent().getStringExtra("repairDateTime"));
        proList = new ArrayList<>();
        proAdapter = new DeviceRepairProductAdapter(R.layout.item_device_product_repair, proList, 1);
        proAdapter.bindToRecyclerView(rvPro);
        proList.addAll(JSON.parseArray(getIntent().getStringExtra("pro"), DeviceRepairVo.class));
        proAdapter.notifyDataSetChanged();
        param = new HashMap<>();
        param.put("receiveDeptId", getIntent().getStringExtra("depId"));
        param.put("launchUserId", UserKeeper.getInstance().getUserId());
        param.put("launchDeptId", UserKeeper.getInstance().getUserDepId());
        param.put("repairDateTime", getIntent().getStringExtra("repairDateTime"));
        param.put("amount", getIntent().getStringExtra("amount"));
    }


    @Override
    protected void initEvent() {
        addNumListener(etInput5, tvInput5);
    }

    /**
     * 添加字数监听器
     *
     * @param et
     * @param tv
     */
    private void addNumListener(EditText et, TextView tv) {
        WorkUtils.addNumListener(et,tv);
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_device_buy_repair_confirm;
    }


    /**
     * 下单
     */
    private void submitOrder() {
        param.put("equipmentRepairItem", proList);
        showLoading();
        RtfUtils.getRtf().addEquipmentRepair(WorkUtils.convertMapToBody(param)).compose(Transformer.schedule()).subscribe(new WorkObserver<BaseBean>() {
            @Override
            public void doOnSubscribe(Disposable d) {
            }

            @Override
            public void onFail(String errorMsg) {
                hideLoading();
                toast(errorMsg);
            }

            @Override
            public void onSuccess(BaseBean data) {
                hideLoading();
                if (data.getCode() != 0) {
                    toast(data.getMessage());
                    return;
                }
                toast("提交成功");
                EventBus.getDefault().post(new AddDeviceEvent());
                finish();
            }
        });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_report, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.report:
                param.put("remark", etInput5.getText().toString());
                submitOrder();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
