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

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
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.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.DeviceBean;
import com.wd.workoffice.bean.ProductBean;
import com.wd.workoffice.bean.event.AddDeviceEvent;
import com.wd.workoffice.bean.workEnum.PermissionType;
import com.wd.workoffice.retrofit.ApiService;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.bat.store.StoreClientFactoryActivity;
import com.wd.workoffice.ui.activity.bat.store.StoreOrderSuccessActivity;
import com.wd.workoffice.ui.adapter.DeviceConfirmAdapter;
import com.wd.workoffice.ui.adapter.StoreCarAdapter;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.Glide4Engine;
import com.wd.workoffice.util.MathUtils;
import com.wd.workoffice.util.PickTimeUtils;
import com.wd.workoffice.util.UserKeeper;
import com.wd.workoffice.util.WorkUtils;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.Nullable;
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.RtfHelper;
import flexible.xd.android_base.network.rtfhttp.Transformer;
import flexible.xd.android_base.utils.TimeUtils;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 采购-确认订单
 * author : flexible
 * email : lgd19940421@163.com
 * github: https://github.com/FlexibleXd
 **/
public class DeviceBuyConfirmActivity 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.rl_set)
    RelativeLayout rlSet;
    @BindView(R.id.rv_pro)
    RecyclerView rvPro;
    @BindView(R.id.et_input5)
    EditText etInput5;
    @BindView(R.id.tv_input5)
    TextView tvInput5;
    private List<DeviceBean.RecordsBean> proList;
    private DeviceConfirmAdapter proAdapter;
    private Map<String, Object> param;
    private Map<String, Object> expandInfo;
    private List<JSONObject> orderItems;

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


    @Override
    protected void initData() {
        tvDep.setText(getIntent().getStringExtra("depName"));
        proList = new ArrayList<>();
        proAdapter = new DeviceConfirmAdapter(R.layout.item_device_confirm, proList);
        proAdapter.bindToRecyclerView(rvPro);
        proList.addAll(JSON.parseArray(getIntent().getStringExtra("pro"), DeviceBean.RecordsBean.class));
        proAdapter.notifyDataSetChanged();
        changeNumAndPrice();
        param = new HashMap<>();
        param.put("receiveDeptId", getIntent().getStringExtra("depId"));
        expandInfo = new HashMap<>();
    }

    /**
     * 转换pro，生成上传参数
     *
     * @param proList
     * @return
     */
    private List<JSONObject> coverProInfo(List<DeviceBean.RecordsBean> proList) {
        List<JSONObject> orderItems = new ArrayList<>();
        for (DeviceBean.RecordsBean recordsBean : proList) {
            JSONObject data = new JSONObject();
            data.put("equipmentId", recordsBean.getId());
            data.put("quantity", recordsBean.getCarNum());
            data.put("unitPrice", recordsBean.getCarPrice());
            orderItems.add(data);
        }
        return orderItems;
    }


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

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


    @OnClick({R.id.tv_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_ok:
                expandInfo.put("remark", etInput5.getText().toString());
                orderItems = coverProInfo(proList);
                submitOrder();
                break;
        }
    }

    /**
     * 下单
     */
    private void submitOrder() {
        param.put("equipmentPurchaseItem", orderItems);
        showLoading();
        RtfUtils.getRtf().addEquipmentPurchase(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();
            }

        });
    }

    private void changeNumAndPrice() {
        tvNum.setText("共 ".concat(proList.size() + "").concat(" 件"));
        String allPrice = "";
        for (DeviceBean.RecordsBean recordsBean : proList) {
            allPrice = MathUtils.add(allPrice, MathUtils.multiply(recordsBean.getCarNum() + "", recordsBean.getCarPrice() + "", 2), 2);
        }
        tvPrice.setText("总计: ¥ ".concat(allPrice));
    }


}
