package com.yem.wm.bc.companytitle.form;

import com.yem.wm.bc.companytitle.util.TitleFuncUtil;
import com.yem.wm.utils.DynamicObjectUtil;
import com.yem.wm.utils.YEM;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.CloseCallBack;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.control.Label;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.fi.ap.enums.BillStatusEnum;

import java.util.EventObject;

/**
 * @author xwudd
 * @date 18:11
 * @Description
 **/
public class CompanyTitleEdit extends AbstractBillPlugIn {
    private static Log log = LogFactory.getLog(CompanyTitleEdit.class);
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addClickListeners("yem_flex_addco","yem_addco1","yem_addco2","yem_card_addco","yem_changeco");
        this.addClickListeners("yem_flex_addbuyer","yem_card_addkh","yem_addpayer","yem_addpayer1","yem_changepayer");
    }


    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        setDefaultCompanyInfo();
        setCoLabel();
        setCusLabel();
    }

    @Override
    public void click(EventObject e) {
        IDataModel model = this.getModel();
        Control control = (Control)e.getSource();
        String key = control.getKey();
        DynamicObject org = (DynamicObject) model.getValue("org");
        switch (key){
            case "yem_flex_addco" :
            case "yem_addco1" :
            case "yem_addco2" :
            case "yem_card_addco" :
            case "yem_changeco" :
                showFormDynamic("yem_addcompanytitle",org);
                break;
            case "yem_flex_addbuyer" :
            case "yem_card_addkh" :
            case "yem_addpayer" :
            case "yem_addpayer1" :
            case "yem_changepayer" :
                showFormDynamic("yem_addcustomertitle",org);
                break;
        }
    }


    @Override
    public void closedCallBack(ClosedCallBackEvent e) {
        String id = e.getActionId();
        switch (id) {
            case "yem_addcompanytitle" :
                setCoLabel();
                break;
            case "yem_addcustomertitle" :
                setCusLabel();
                break;
        }
    }


    public void showFormDynamic(String formId,DynamicObject obj){
        IDataModel model = this.getModel();
        if (model.getValue("billstatus").equals(BillStatusEnum.SAVE.getValue())) {
            IFormView view = this.getView();
            FormShowParameter fsp = new FormShowParameter();
            fsp.getOpenStyle().setShowType(ShowType.Modal);
            fsp.setFormId(formId);
            fsp.setCustomParam("obj",obj);
            fsp.setCloseCallBack(new CloseCallBack(this, formId));
            view.showForm(fsp);
        }
    }



    /**
     * @description 给公司抬头页签赋值
     * @author xwudd
     * return
     * @time 2023/7/26 18:38
     */

    public void setCoLabel(){
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        try {
            if(YEM.isEmpty(model.getValue("yem_company"))){
                view.setVisible(Boolean.TRUE, new String[]{"yem_flex_addco"});
                view.setVisible(Boolean.FALSE, new String[]{"yem_flex_coinfo"});
            }else{
                ((Label) view.getControl("yem_conamelabel")).setText((String) model.getValue("yem_companyname"));//公司名称
                ((Label) view.getControl("yem_conamelabeleng")).setText((String) model.getValue("yem_companynameeng"));//公司英文名称
                ((Label) view.getControl("yem_coaddress")).setText((String) model.getValue("yem_companyaddr"));//地址(公司)
                ((Label) view.getControl("yem_coaddresseng")).setText((String) model.getValue("yem_companyaddreng"));//英文地址（公司）
                ((Label) view.getControl("yem_colinkman")).setText((String) model.getValue("yem_companylinkman"));//联系人(公司)
                ((Label) view.getControl("yem_cophone")).setText((String) model.getValue("yem_companytel"));//电话(公司)
                ((Label) view.getControl("yem_cofax")).setText((String) model.getValue("yem_companyfax"));//传真(公司)
                ((Label) view.getControl("yem_cobankaccr")).setText((String) model.getValue("yem_companybankacc"));//银行账号
                ((Label) view.getControl("yem_coopenbank")).setText((String) model.getValue("yem_companyopenbank"));//开户银行
                ((Label) view.getControl("yem_coaccname")).setText((String) model.getValue("yem_companyaccname"));//账户名称
                ((Label) view.getControl("yem_coswift")).setText((String) model.getValue("yem_companyswift"));//银行Swift号
                ((Label) view.getControl("yem_cobankaddress")).setText((String) model.getValue("yem_companybankaddress"));//银行地址
                ((Label) view.getControl("yem_coopenbankeng")).setText((String) model.getValue("yem_openbankengaddress"));//开户行英文名称
                ((Label) view.getControl("yem_cobankaddresseng")).setText((String) model.getValue("yem_bankengaddress"));//银行英文地址
                ((Label) view.getControl("yem_companyemail")).setText((String) model.getValue("yem_coemail"));//邮箱

                view.setVisible(Boolean.TRUE, new String[]{"yem_flex_coinfo"});
                view.setVisible(Boolean.FALSE, new String[]{"yem_flex_addco"});
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e);
        }

    }


    /**
     * @description 给客户抬头页签赋值
     * @author xwudd
     * return
     * @time 2023/7/26 18:38
     */
    public void setCusLabel(){
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        try {
            if(YEM.isEmpty(model.getValue("yem_customer"))){
                view.setVisible(Boolean.TRUE, new String[]{"yem_flex_addbuyer"});
                view.setVisible(Boolean.FALSE, new String[]{"yem_flex_buyerinfo1"});
            }else{
                ((Label) view.getControl("yem_payername")).setText((String) model.getValue("yem_customername"));//客户名称
                ((Label) view.getControl("yem_payeraddress")).setText((String) model.getValue("yem_customeraddr"));//地址(客户)
                ((Label) view.getControl("yem_payerlinkman")).setText((String) model.getValue("yem_customerlinkman"));//联系人(客户)
                ((Label) view.getControl("yem_payerphone")).setText((String) model.getValue("yem_customertel"));//电话(客户)
                ((Label) view.getControl("yem_payerfax")).setText((String) model.getValue("yem_customerfax"));//传真(客户)
                ((Label) view.getControl("yem_payerbankacc")).setText((String) model.getValue("yem_customerbankacc"));//银行账号
                ((Label) view.getControl("yem_payeropenbank")).setText((String) model.getValue("yem_customeropenbank"));//开户银行
                ((Label) view.getControl("yem_payeraccname")).setText((String) model.getValue("yem_customeraccname"));//账户名称
//                ((Label) view.getControl("yem_payeremail")).setText((String) model.getValue("yem_customemail"));//邮箱
                DynamicObject customerAcCurrency = (DynamicObject)model.getValue("yem_customeraccurrency");
                String customerAcCurrencyName = customerAcCurrency != null?customerAcCurrency.getString("name"):"";
                ((Label) view.getControl("yem_payercuurreny")).setText(customerAcCurrencyName);//账号币别
                DynamicObject customerAccCountry = (DynamicObject)model.getValue("yem_customeracccountry");
                String customerAccCountryName = customerAccCountry != null?customerAccCountry.getString("name"):"";
                ((Label) view.getControl("yem_payercountry")).setText(customerAccCountryName);//开户国家
                ((Label) view.getControl("yem_payeremail")).setText((String) model.getValue("yem_customemail"));//邮箱
                ((Label) view.getControl("yem_groupcuslabel")).setText((String) model.getValue("yem_groupcusfields"));//邮箱
                view.setVisible(Boolean.TRUE, new String[]{"yem_flex_buyerinfo1"});
                view.setVisible(Boolean.FALSE, new String[]{"yem_flex_addbuyer"});
            }
        }catch(Exception e){
            e.printStackTrace();
            log.error(e);
        }
    }

    /**
     * @description 单据打开默认赋值公司抬头信息
     * @author xwudd
     * return
     * @time 2023/7/27 16:36
     */

    public void setDefaultCompanyInfo(){
        IDataModel model = this.getModel();
        DynamicObject org;
        if ("yem_es_storagetrans_wl".equals(this.getModel().getDataEntityType().getName())) {
            org = (DynamicObject) model.getValue("yem_org");
        } else {
            org = (DynamicObject) model.getValue("org");
        }
        DynamicObject dataEntity = model.getDataEntity();
        DynamicObject company = dataEntity.getDynamicObject("yem_company");
        if(org != null && company == null){
            QFilter qFilter = new QFilter("yem_erporg", QCP.equals, org.getPkValue());
            String selectfields = DynamicObjectUtil.getSelectfields("yem_bd_companyinfo", false);
            selectfields = DynamicObjectUtil.getEntrySelectfields(selectfields,"yem_bd_companyinfo","yem_bankinfo",false);
            DynamicObject[] companyInfos = BusinessDataServiceHelper.load("yem_bd_companyinfo", selectfields, qFilter.toArray());
            if(companyInfos.length > 0){
                DynamicObject companyInfo = companyInfos[0];
                TitleFuncUtil.setCompanyInfo(model,companyInfo);
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e)
    {
        super.afterDoOperation(e);
        IDataModel model = this.getModel();
        String operateKey = e.getOperateKey();
        DynamicObject dataEntity = model.getDataEntity();
        if ("yem_es_storagetrans_wl".equals(model.getDataEntityType().getName())) {
            String billno = dataEntity.getString("yem_billno");
            QFilter qFilter = new QFilter("billno", QCP.equals, billno);
            if ("bookingspace".equals(operateKey)) {
                DynamicObject[] result = BusinessDataServiceHelper.load("yem_es_contactbook", "id, billno, yem_bookingspacestatus", qFilter.toArray());
                for (DynamicObject dynamicObject : result) {
                    dynamicObject.set("yem_bookingspacestatus", "B");
                }
                SaveServiceHelper.update(result);
            }

            if ("unbookingspace".equals(operateKey)) {
                DynamicObject[] result = BusinessDataServiceHelper.load("yem_es_contactbook", "id, billno, yem_bookingspacestatus", qFilter.toArray());
                for (DynamicObject dynamicObject : result) {
                    dynamicObject.set("yem_bookingspacestatus", "A");
                }
                SaveServiceHelper.update(result);
            }
        }
    }
}
