package com.tenlionsoft.oamodule.activity.car;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.text.Html;
import android.text.TextUtils;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hjq.toast.ToastUtils;
import com.tenlionsoft.baselib.constant.PathConfig;
import com.tenlionsoft.baselib.core.beans.BaseSuccessBean;
import com.tenlionsoft.baselib.core.retrofit_net.RetrofitManager;
import com.tenlionsoft.baselib.core.retrofit_net.conver.RxTransformer;
import com.tenlionsoft.baselib.core.widget.base.BaseActivity;
import com.tenlionsoft.baselib.utils.ExceptionHandler;
import com.tenlionsoft.baselib.utils.UIUtil;
import com.tenlionsoft.baselib.utils.UserLgUtils;
import com.tenlionsoft.oamodule.R;
import com.tenlionsoft.oamodule.R2;
import com.tenlionsoft.oamodule.beans.ByCarRecordBean;
import com.tenlionsoft.oamodule.net.OAApi;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * 作者: Adam
 * 日期: 2022年04月14日10:26:43
 * 邮箱: itgaojian@163.com
 * 描述: 用车申请详情
 */
@Route(path = PathConfig.PATH_MODULE_OA_ACTIVITY_CAR_RECORD_DETAIL)
public class ByCarDetailActivity extends BaseActivity {

    @BindView(R2.id.tv_name)
    TextView mTvName;
    @BindView(R2.id.tv_dept)
    TextView mTvDept;
    @BindView(R2.id.tv_apply_date)
    TextView mTvApplyDate;
    @BindView(R2.id.tv_fare)
    TextView mTvFare;
    @BindView(R2.id.tv_reason)
    TextView mTvReason;
    @BindView(R2.id.tv_start_date)
    TextView mTvStartDate;
    @BindView(R2.id.tv_end_date)
    TextView mTvEndDate;
    @BindView(R2.id.tv_leave)
    TextView mTvLeave;
    @BindView(R2.id.tv_des)
    TextView mTvDes;
    @BindView(R2.id.tv_car)
    TextView mTvCar;
    @BindView(R2.id.tv_driver)
    TextView mTvDriver;
    @BindView(R2.id.tv_before)
    TextView mTvBefore;
    @BindView(R2.id.tv_after)
    TextView mTvAfter;
    @BindView(R2.id.tv_mileage)
    TextView mTvMileage;
    @BindView(R2.id.tv_remark)
    TextView mTvRemark;
    private String mId;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_by_car_detail;
    }

    @Override
    public void initData() {
        ButterKnife.bind(this);
        mTvBaseTitle.setText("用车申请");
        mId = getIntent().getStringExtra("id");
        if (TextUtils.isEmpty(mId)) {
            ToastUtils.show("数据有误");
            finish();
        } else {
            getRecordDetail();
        }
    }

    /**
     * 获取详情
     */
    private void getRecordDetail() {
        RetrofitManager.getInstance()
            .create(OAApi.class)
            .getByCarRecordDetail(mId, UserLgUtils.getToken())
            .compose(RxTransformer.getTransformer())
            .subscribe(new Observer<ByCarRecordBean>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull ByCarRecordBean byCarRecordBean) {
                    setDataToView(byCarRecordBean);
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    refreshView(STATE_LOAD_ERROR);
                    ExceptionHandler.handleException(e);
                }

                @Override
                public void onComplete() {

                }
            });
    }

    private void showConfirmDialog() {
        new AlertDialog.Builder(mActivity)
            .setTitle("提示")
            .setMessage("确定要删除该申请吗?")
            .setPositiveButton("确定", (dialog, which) -> {
                dialog.dismiss();
                doDelRecord();
            })
            .setNegativeButton("取消", (dialog, which) -> dialog.dismiss())
            .create()
            .show();
    }

    /**
     * 删除用车申请
     */
    private void doDelRecord() {
        ProgressDialog dialog = UIUtil.initDialog(mActivity, "删除中...");
        dialog.show();
        RetrofitManager.getInstance()
            .create(OAApi.class)
            .doDelByCarRecord(mId, UserLgUtils.getToken())
            .compose(RxTransformer.getTransformer())
            .subscribe(new Observer<BaseSuccessBean>() {
                @Override
                public void onSubscribe(@NonNull Disposable d) {

                }

                @Override
                public void onNext(@NonNull BaseSuccessBean baseSuccessBean) {
                    if (dialog.isShowing()) dialog.dismiss();
                    ToastUtils.show("删除成功");
                    setResult(RESULT_OK);
                    finish();
                }

                @Override
                public void onError(@NonNull Throwable e) {
                    if (dialog.isShowing()) dialog.dismiss();
                    ExceptionHandler.handleException(e);
                }

                @Override
                public void onComplete() {

                }
            });
    }

    private void setDataToView(ByCarRecordBean bean) {
        refreshView(STATE_LOAD_SUCCESS);
        mTvName.setText(bean.getCarApplyUserName());
        mTvDept.setText(bean.getCarApplyUserDeptName());
        mTvFare.setText(bean.getCarApplyPassengerNames());
        mTvReason.setText(Html.fromHtml(bean.getCarApplyReason()));
        mTvStartDate.setText(bean.getCarApplyStartTime());
        mTvEndDate.setText(bean.getCarApplyEndTime());
        mTvLeave.setText(bean.getCarApplyStartPlace());
        mTvDes.setText(bean.getCarApplyEndPlace());
        mTvCar.setText(bean.getCarApplyCarName());
        mTvDriver.setText(bean.getCarApplyDriverName());
        mTvBefore.setText(bean.getCarApplyStartMileage() + "");
        mTvAfter.setText(bean.getCarApplyEndMileage() + "");
        mTvMileage.setText(bean.getCarApplyMileage() + "");
        mTvRemark.setText(bean.getCarApplyRemark());
    }

    @Override
    protected void doSearchByTitle() {

    }

    @Override
    protected void clearSearch() {

    }
}