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

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.wd.workoffice.R;
import com.wd.workoffice.app.BaseBean;
import com.wd.workoffice.app.WorkToolBarActivity;
import com.wd.workoffice.bean.ClientBean;
import com.wd.workoffice.bean.MarkBean;
import com.wd.workoffice.bean.ProductBean;
import com.wd.workoffice.bean.event.ModifyCarEvent;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.bat.work.DeviceBuyCarActivity;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.WorkUtils;

import org.greenrobot.eventbus.EventBus;

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

import butterknife.BindView;
import butterknife.ButterKnife;
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 ChooseHeadActivity extends WorkToolBarActivity {

    @BindView(R.id.et_company)
    EditText etCompany;
    @BindView(R.id.et_factory)
    EditText etFactory;
    @BindView(R.id.et_machine)
    EditText etMachine;
    @BindView(R.id.et_pro_name)
    EditText etProName;
    @BindView(R.id.et_img_code)
    EditText etImgCode;
    @BindView(R.id.et_spec)
    EditText etSpec;
    @BindView(R.id.et_send_company)
    EditText etSendCompany;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.et_box)
    EditText etBox;
    @BindView(R.id.et_supplies_code)
    EditText etSuppliesCode;
    @BindView(R.id.et_contract)
    EditText etContract;
    @BindView(R.id.et_country_company)
    EditText etCountryCompany;
    @BindView(R.id.et_app_name)
    EditText etAppName;
    @BindView(R.id.et_use)
    EditText etUse;
    private MarkBean markBean;
    private ProductBean.RecordsBean product;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initData() {
        product = JSON.parseObject(getIntent().getStringExtra("product"), ProductBean.RecordsBean.class);
        if (null == markBean) {
            markBean = new MarkBean();
        }
        markBean.setProductId(product.getId());
        markBean.setCustomerId(Integer.valueOf(getIntent().getStringExtra("customerId")));
        etSpec.setText(product.getSpec());
        etProName.setText(product.getName());
        etImgCode.setText(product.getSimpleCode());
        addMarkData();
        if (null == product.getMarkOrderRel()) {
            getTemplate();
        } else {
            markBean = product.getMarkOrderRel();
            changeView();
        }

    }


    @Override
    protected void initEvent() {
    }


    private void getTemplate() {
        Map<String, Object> param = WorkUtils.simpleParam();
        param.put("productId", product.getId());
        param.put("customerId", getIntent().getStringExtra("customerId"));
        RtfUtils.getRtf().getTemplate(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;
                }
                if (null != data.getData()) {
                    markBean = JSON.parseObject(data.getData().toString(), MarkBean.class);
                    changeView();
                }
            }
        });
    }

    private void changeView() {
        if (null == markBean) {
            return;
        }
        etCompany.setText(markBean.getReceiveCompany());
        etFactory.setText(markBean.getCustomerBranchFactory());
        etMachine.setText(markBean.getContinuousCastingMachine());
        etProName.setText(markBean.getProductName());
        etImgCode.setText(markBean.getProductSimpleCode());
        etSpec.setText(markBean.getProductSpec());
        etSendCompany.setText(markBean.getShipCompany());
        etCode.setText(markBean.getMaterialCode());
        etBox.setText(markBean.getBoxCode());
        etSuppliesCode.setText(markBean.getMaterialCodeOther());
        etContract.setText(markBean.getForeignTradeContractNumber());
        etCountryCompany.setText(markBean.getDomesticShipCompany());
        etAppName.setText(markBean.getProjectName());
        etUse.setText(markBean.getUseLocation());
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_finish, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.finish:
                // 收货单位  产品的名称，图号规格是必须填写项
                String company = etCompany.getText().toString();
                String proName = etProName.getText().toString();
                String imgCode = etImgCode.getText().toString();
                String spec = etSpec.getText().toString();
                if (TextUtils.isEmpty(company)) {
                    toast("请输入收货单位");
                    return super.onOptionsItemSelected(item);
                }
                if (TextUtils.isEmpty(proName)) {
                    toast("请输入产品名称");
                    return super.onOptionsItemSelected(item);
                }
                if (TextUtils.isEmpty(imgCode)) {
                    toast("请输入图号");
                    return super.onOptionsItemSelected(item);
                }
                if (TextUtils.isEmpty(spec)) {
                    toast("请输入产品规格");
                    return super.onOptionsItemSelected(item);
                }
                addMarkData();
                QMUIDialog.MessageDialogBuilder dialog = DialogUtils.okAndCancelDialog(ChooseHeadActivity.this);
                dialog.setMessage("是否将唛头同步到模板").addAction("否", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        Intent intent = new Intent();
                        intent.putExtra("mark", JSON.toJSONString(markBean));
                        intent.putExtra("position", getIntent().getStringExtra("position"));
                        setResult(10003, intent);
                        finish();
                    }
                }).addAction("是", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        showLoading();
                        updateTemplate();
                    }
                }).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void addMarkData() {
        markBean.setReceiveCompany(etCompany.getText().toString());
        markBean.setCustomerBranchFactory(etFactory.getText().toString());
        markBean.setContinuousCastingMachine(etMachine.getText().toString());
        markBean.setProductName(etProName.getText().toString());
        markBean.setProductSimpleCode(etImgCode.getText().toString());
        markBean.setProductSpec(etSpec.getText().toString());
        markBean.setShipCompany(etSendCompany.getText().toString());
        markBean.setMaterialCode(etCode.getText().toString());
        markBean.setBoxCode(etBox.getText().toString());
        markBean.setMaterialCodeOther(etSuppliesCode.getText().toString());
        markBean.setForeignTradeContractNumber(etContract.getText().toString());
        markBean.setDomesticShipCompany(etCountryCompany.getText().toString());
        markBean.setProjectName(etAppName.getText().toString());
        markBean.setUseLocation(etUse.getText().toString());
    }

    private void updateTemplate() {
        Map param = JSON.parseObject(JSON.toJSONString(markBean), Map.class);
        RtfUtils.getRtf().setTemplate(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;
                }

                Intent intent = new Intent();
                intent.putExtra("mark", JSON.toJSONString(markBean));
                intent.putExtra("position", getIntent().getStringExtra("position"));
                setResult(10003, intent);
                finish();
            }
        });
    }

}
