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

import android.content.Intent;
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.MarkBean;
import com.wd.workoffice.retrofit.RtfUtils;
import com.wd.workoffice.retrofit.WorkObserver;
import com.wd.workoffice.ui.activity.bat.store.ChooseHeadActivity;
import com.wd.workoffice.util.DialogUtils;
import com.wd.workoffice.util.WorkUtils;

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 OrderMarkActivity 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 int type;// 0 编辑 1 完成

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

    private void changeCanInput(boolean canInput) {
        if (!canInput){
            etCompany.setHint("");
            etFactory.setHint("");
            etMachine.setHint("");
            etProName.setHint("");
            etImgCode.setHint("");
            etSpec.setHint("");
            etSendCompany.setHint("");
            etCode.setHint("");
            etBox.setHint("");
            etSuppliesCode.setHint("");
            etContract.setHint("");
            etCountryCompany.setHint("");
            etAppName.setHint("");
            etUse.setHint("");
        }else {
            etCompany.setHint("请输入");
            etFactory.setHint("请输入");
            etMachine.setHint("请输入");
            etProName.setHint("请输入");
            etImgCode.setHint("请输入");
            etSpec.setHint("请输入");
            etSendCompany.setHint("请输入");
            etCode.setHint("请输入");
            etBox.setHint("请输入");
            etSuppliesCode.setHint("请输入");
            etContract.setHint("请输入");
            etCountryCompany.setHint("请输入");
            etAppName.setHint("请输入");
            etUse.setHint("请输入");
        }
        etCompany.setEnabled(canInput);
        etFactory.setEnabled(canInput);
        etMachine.setEnabled(canInput);
        etProName.setEnabled(canInput);
        etImgCode.setEnabled(canInput);
        etSpec.setEnabled(canInput);
        etSendCompany.setEnabled(canInput);
        etCode.setEnabled(canInput);
        etBox.setEnabled(canInput);
        etSuppliesCode.setEnabled(canInput);
        etContract.setEnabled(canInput);
        etCountryCompany.setEnabled(canInput);
        etAppName.setEnabled(canInput);
        etUse.setEnabled(canInput);
    }

    @Override
    protected void initData() {
        getOrderMark();
    }


    @Override
    protected void initEvent() {
    }


    private void getOrderMark() {
        Map<String, Object> param = WorkUtils.simpleParam();
        param.put("productId", getIntent().getStringExtra("productId"));
        param.put("customerId", getIntent().getStringExtra("customerId"));
        param.put("orderId", getIntent().getStringExtra("orderId"));
        RtfUtils.getRtf().getOrderMark(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_modify_finish, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // 动态设置ToolBar状态
        switch (type) {
            case 0:
                menu.findItem(R.id.modify).setVisible(true);
                menu.findItem(R.id.finish).setVisible(false);
                break;
            case 1:
                menu.findItem(R.id.modify).setVisible(false);
                menu.findItem(R.id.finish).setVisible(true);
                break;
        }
        return super.onPrepareOptionsMenu(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(OrderMarkActivity.this);
                dialog.setMessage("是否将唛头同步到模板").addAction("否", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        updateOrderMark();
                    }
                }).addAction("是", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        showLoading();
                        updateTemplate();
                    }
                }).show();
                break;
            case R.id.modify:
                type =1;
                changeCanInput(true);
                invalidateOptionsMenu();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    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;
                }

                updateOrderMark();
            }
        });
    }


    private void updateOrderMark() {
        Map param = JSON.parseObject(JSON.toJSONString(markBean), Map.class);
        RtfUtils.getRtf().changeOrderMark(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("更新成功");
                finish();
            }
        });
    }

    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());
    }
}
