package nckd.jimin.jyyy.fi.plugin.form.travelcheck;

import kd.bos.bill.BillOperationStatus;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.filter.FilterColumn;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.control.Control;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeCreateListDataProviderArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.FilterContainerInitArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import nckd.jimin.jyyy.fi.common.constant.travelcheck.FinanceBillConfirmConstant;
import nckd.jimin.jyyy.fi.common.constant.travelcheck.TravelFinanDraftConstant;
import nckd.jimin.jyyy.fi.common.util.CommonUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class TravelFinanDraftList extends AbstractListPlugin {

    @Override
    public void filterContainerInit(FilterContainerInitArgs args) {
        super.filterContainerInit(args);
        List<FilterColumn> commonFilterColumns = args.getCommonFilterColumns();
        FormShowParameter parameter = getView().getFormShowParameter();
        Map<String, Object> customParams = parameter.getCustomParams();
        if(customParams.containsKey("showdetail")){
            for(FilterColumn column: commonFilterColumns){
                column.setDefaultValue(null);
            }
        }
    }

    @Override
    public void beforeCreateListDataProvider(BeforeCreateListDataProviderArgs args) {
        super.beforeCreateListDataProvider(args);
        args.setListDataProvider(new PersonPositionListProvider());
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate operate = (FormOperate)args.getSource();
        String key = operate.getOperateKey();
        if(TravelFinanDraftConstant.OperateTypes.DRAFTCONFIRM.equals(key)){
            String period = CommonUtils.parsePeriod(new Date());
            // 判断当前期间是否已经存在
            if(ORM.create().exists(FinanceBillConfirmConstant.ENTITYID, new QFilter(FinanceBillConfirmConstant.NCKD_CHECKMONTH, QCP.equals, period).toArray())){
                this.getView().showTipNotification(String.format("期间[%s]已经存在商旅确认单，请勿重复创建！", period));
                args.setCancel(true);
                return;
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs args) {
        super.afterDoOperation(args);
        String operateKey = args.getOperateKey();
        if(args.getOperationResult() == null || !args.getOperationResult().isSuccess() ){
            return;
        }

        if(TravelFinanDraftConstant.OperateTypes.DRAFTCONFIRM.equals(operateKey)){
            BillShowParameter parameter = new BillShowParameter();
            parameter.setFormId(FinanceBillConfirmConstant.ENTITYID);
            parameter.setBillStatus(BillOperationStatus.ADDNEW);
            parameter.setStatus(OperationStatus.ADDNEW);
            parameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
            this.getView().showForm(parameter);
        }
    }
}
