package nckd.jxccl.hr.hstu.plugin.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.constant.StatusEnum;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.CloseCallBack;
import kd.bos.form.ConfirmCallBackListener;
import kd.bos.form.ConfirmTypes;
import kd.bos.form.FormShowParameter;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.ShowType;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.ClosedCallBackEvent;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.portal.util.SerializationUtils;
import kd.bos.schedule.api.TaskInfo;
import kd.bos.schedule.api.TaskStatusConstant;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.base.common.utils.StrFormatter;
import nckd.jxccl.hr.hstu.common.HonorStudentConstant;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
* 综合测评问卷模板
* @author W.Y.C
* @date 2025/6/30 16:17
* @version 1.0
*/
public class EvalQuestTplListPlugin extends AbstractListPlugin implements Plugin {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        String operateKey = ((FormOperate) args.getSource()).getOperateKey();
        if(HonorStudentConstant.OP_CREATEEVAL.equals(operateKey)) {
            ListSelectedRowCollection selectedRows = this.getSelectedRows();
            /*if (selectedRows.size() != 1) {
                this.getView().showErrorNotification("请选择一条数据！");
                args.setCancel(true);
                return;
            }*/
            if(!selectedRows.isEmpty()) {
                DynamicObject basedata = BusinessDataServiceHelper.loadSingle(
                        selectedRows.get(0).getPrimaryKeyValue(),
                        HonorStudentConstant.NCKD_EVALQUESTTPL_ENTITYID,
                        HonorStudentConstant.STATUS + "," + HonorStudentConstant.ENABLE
                );
                if (basedata != null) {
                    String status = basedata.getString(HonorStudentConstant.STATUS);
                    Boolean enable = basedata.getBoolean(HonorStudentConstant.ENABLE);

                    // 是否可用
                    boolean isAvailable = StatusEnum.C.toString().equals(status) && Boolean.TRUE.equals(enable);
                    if (!isAvailable) {
                        this.getView().showErrorNotification(StrFormatter.format("【{}】该问卷模板状态不可用", selectedRows.get(0).getName()));
                        args.setCancel(true);
                        return;
                    }
                }
            }
        }
    }



    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        String operateKey = e.getOperateKey();
        if(HonorStudentConstant.OP_CREATEEVAL.equals(operateKey)){
            ListSelectedRowCollection selectedRows = this.getSelectedRows();
            FormShowParameter showParameter = new FormShowParameter();
            showParameter.setFormId(HonorStudentConstant.NCKD_CREATEEVALQUEST_ENTITYID);
            showParameter.getOpenStyle().setShowType(ShowType.Modal);
            showParameter.setCloseCallBack(new CloseCallBack(this, HonorStudentConstant.NCKD_CREATEEVALQUEST_ENTITYID));
            if(!selectedRows.isEmpty()) {
                showParameter.setCustomParam(HonorStudentConstant.NCKD_EVALQUESTTPLID, selectedRows.get(0).getPrimaryKeyValue());
            }
            showParameter.setCaption("生成综合测评问卷");
            this.getView().showForm(showParameter);
        }
    }


    @Override
    public void closedCallBack(ClosedCallBackEvent closedCallBackEvent) {
        String actionId = closedCallBackEvent.getActionId();
        if(HonorStudentConstant.TASK_CREATEEVAL.equals(actionId)){
            //生成评价问卷任务完成回调
            Map<String, Object> result = (Map<String, Object>)closedCallBackEvent.getReturnData();
            if(result != null) {
                String taskInfoStr = (String) result.get("taskinfo");
                if (StringUtils.isNotBlank(taskInfoStr)) {
                    TaskInfo taskInfo = SerializationUtils.fromJsonString(taskInfoStr, TaskInfo.class);

                    if (taskInfo.isTaskEnd()) {
                        String status = taskInfo.getStatus();
                        if (TaskStatusConstant.COMPLETED.equals(status)) {
                            this.getView().showConfirm(
                                    "成功提示",
                                    "生成综合评测问卷完成，可在【查看评价列表】中查看",
                                    MessageBoxOptions.OK,
                                    ConfirmTypes.Save,
                                    new ConfirmCallBackListener()
                            );
                        }
                    }
                }
            }
        }

    }
}