package com.gx.obe.evaluation.step.form;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.TextHintListener;
import org.eclipse.swt.core.listener.VerifyRequiredListener;
import org.eclipse.swt.core.utils.WidgetUtils;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.gx.obe.ColorConstants;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.widget.button.Tips;
import com.gx.obe.components.core.I18NEnum;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.evaluation.EvaluationStepService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 商务步骤配置界面
 * @author chenxw
 */
public class BusinessEvaluationStepDialog extends EDialog {
	
	private final String ROLE_TYPE = "roleType";
	private final EvaluationStepService evaluationStepService = new EvaluationStepService();
	
	private TenderProjectEntity tenderProject;
	private EvaluationStep evaluationStep;
	
	private Text stepNameText;
	private Composite roleTypeCpe;
	private Text stepMemoText;
	private EButton save_btn;
	
	private CallBack callBack;
	
	{
		LogUtils.logClass(BusinessEvaluationStepDialog.class);
	}
	
	/**
	 * @author chenxw
	 * @param parentShell
	 * @param tenderProject
	 * @param evaluationStep
	 */
	public BusinessEvaluationStepDialog(Shell parentShell, TenderProjectEntity tenderProject, EvaluationStep evaluationStep) {
		super(parentShell);
		this.setOperator(SWT.MAX | SWT.CLOSE);
		this.tenderProject = tenderProject;
		this.evaluationStep = evaluationStep;
	}
	
	/**
	 * @Description:
	 * @author chenxw
	 * @param parent
	 * @return
	 */
	protected Control createDialogArea(Composite parent) {
		Composite topContainer = (Composite) super.createDialogArea(parent);
		topContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_topContainer = new GridLayout(1, false);
		gl_topContainer.verticalSpacing = 0;
		gl_topContainer.horizontalSpacing = 0;
		gl_topContainer.marginHeight = 0;
		gl_topContainer.marginWidth = 0;
		topContainer.setLayout(gl_topContainer);
		
		Composite container = new Composite(topContainer, SWT.NONE);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.marginHeight = 10;
		gl_container.marginWidth = 10;
		container.setLayout(gl_container);
		
		Tips label_6 = new Tips(container, SWT.WRAP);
		label_6.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label_6.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, EvaluationStepEnum.getEvaluationStepIco(EvaluationStepEnum.BILL)));
		label_6.setText("评标时根据评标办法对商务指标进行评审并自动计算得分！");
		label_6.setSurroundColor(ColorConstants.SKIN_BG);
		
		Composite composite = new Composite(container, SWT.NONE);
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.verticalSpacing = 10;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Label step_name = new Label(composite, SWT.NONE);
		step_name.setAlignment(SWT.RIGHT);
		step_name.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		step_name.setText("步骤名称：");
		
		Composite composite_2 = new Composite(composite, SWT.NONE);
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_composite_2 = new GridLayout(1, false);
		gl_composite_2.marginWidth = 0;
		gl_composite_2.verticalSpacing = 1;
		gl_composite_2.marginHeight = 0;
		composite_2.setLayout(gl_composite_2);
		
		stepNameText = new Text(composite_2, SWT.NONE);
		stepNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		stepNameText.addPaintListener(new TextHintListener("请输入评标步骤名称"));
		stepNameText.addVerifyListener(new VerifyRequiredListener(stepNameText, "评标步骤名称不能为空！"));
		stepNameText.setTextLimit(20);
		
		ESepator label = new ESepator(composite_2, SWT.NONE);
		label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Label label_1 = new Label(composite, SWT.NONE);
		label_1.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.NORMAL));
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_1.setText("评标专家：");
		
		roleTypeCpe = new Composite(composite, SWT.NONE);
		roleTypeCpe.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_roleTypeComposite = new GridLayout(2, false);
		gl_roleTypeComposite.marginHeight = 0;
		gl_roleTypeComposite.marginWidth = 0;
		roleTypeCpe.setLayout(gl_roleTypeComposite);
		
		Button expertLeaderBtn = new Button(roleTypeCpe, SWT.RADIO);
		expertLeaderBtn.setText("专家组长");
		expertLeaderBtn.setData(ROLE_TYPE, EvaluationStepEnum.EXPERT_LEADER);
		
		Button operatorBtn = new Button(roleTypeCpe, SWT.RADIO);
		operatorBtn.setData(ROLE_TYPE, EvaluationStepEnum.OPERATOR);
		operatorBtn.setText("项目经理");
		
		Label label_5 = new Label(composite, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, true, 1, 1));
		label_5.setText("备      注：");
		
		stepMemoText = new Text(composite, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		stepMemoText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		stepMemoText.setTextLimit(100);
		
		ESepator but_sep = new ESepator(topContainer, SWT.SEPARATOR | SWT.HORIZONTAL);
		but_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		but_sep.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_1 = new Composite(topContainer, SWT.NONE);
		composite_1.setBackgroundMode(SWT.INHERIT_FORCE);
		composite_1.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_composite_1 = new GridLayout(2, false);
		composite_1.setLayout(gl_composite_1);
		
		save_btn = new EButton(composite_1, SWT.NONE);
		save_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				saveAction();
			}
		});
		GridData gd_confirmBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_confirmBtn.widthHint = 90;
		save_btn.setLayoutData(gd_confirmBtn);
		save_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		save_btn.setText(Messages.Button_save);
		
		EButton cancel_btn = new EButton(composite_1, SWT.NONE);
		cancel_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				close();
			}
		});
		GridData gd_button = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_button.widthHint = 90;
		cancel_btn.setLayoutData(gd_button);
		cancel_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		cancel_btn.setText(Messages.Button_close);
		
		init();
		
		return container;
	}
	
	/**
	 * @Description: 初始化
	 * @author chenxw
	 */
	private void init() {
		if (null == evaluationStep) {
			this.setTitle("新增评标步骤");
			stepNameText.setText("商务评审");
			WidgetUtils.setRadioChecked(roleTypeCpe, EvaluationStepEnum.EXPERT_LEADER, ROLE_TYPE);
		} else {
			this.setTitle("修改评标步骤");
			stepNameText.setText(ObjectUtils.getObjString(evaluationStep.getStepName()));
			WidgetUtils.setRadioChecked(roleTypeCpe, evaluationStep.getRoleType(), ROLE_TYPE);
			stepMemoText.setText(ObjectUtils.getObjString(evaluationStep.getStepMemo()));
			String stepStatus = evaluationStep.getStepStatus();
			boolean enabled = true;
			if (CommonEnum.IN_PROGRESS.equals(stepStatus)) {
				enabled = false;
			} else if (CommonEnum.COMPLETE.equals(stepStatus)) {
				enabled = false;
			}
			stepNameText.setEnabled(enabled);
			stepNameText.setEnabled(enabled);
			save_btn.setEnabled(enabled);
		}
	}
	
	/**
	 * @Description: 保存事件
	 * @author chenxw
	 */
	private void saveAction() {
		if (null == evaluationStep) {
			evaluationStep = new EvaluationStep();
			evaluationStep.setTenderId(tenderProject.getId());
			evaluationStep.setStepType(EvaluationStepEnum.BILL);
			evaluationStep.setFilterType(EvaluationStepEnum.FILTER_YES);
			evaluationStep.setStepStatus(CommonEnum.NOT_START);
		}
		evaluationStep.setStepName(stepNameText.getText().trim());
		evaluationStep.setStepMemo(stepMemoText.getText().trim());
		evaluationStep.setRoleType(ObjectUtils.getObjString(WidgetUtils.getRadioVal(roleTypeCpe, ROLE_TYPE)));
		boolean successCount;
		if (null == evaluationStep.getId()) {
			evaluationStep.setId(UuidUtils.getUUID());
			successCount = evaluationStepService.insert(evaluationStep);
		} else {
			successCount = evaluationStepService.updateAssignProperty(evaluationStep, new String[] { "stepName", "stepMemo", "roleType" });
		}
		if (!successCount) {
			MessageDialog.openError(getShell(), "保存评标步骤失败！");
			return;
		}
		MessageDialog.openShortMessage("保存评标步骤成功！");
		callBack.callBackObject(evaluationStep);
		close();
	}
	
	public CallBack getCallBack() {
		return callBack;
	}
	
	public void setCallBack(CallBack callBack) {
		this.callBack = callBack;
	}
	
	protected Point getInitialSize() {
		return new Point(550, 400);
	}
}
