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

import java.text.MessageFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
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.Combo;
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.components.core.enumeration.EvaluationStepEnum;
import com.gx.obe.components.core.enumeration.TenderProjectEnum;
import com.gx.obe.evaluation.step.listener.BidRegulationsListener;
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.StringUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.SupplierEvaluationResult;
import com.gx.obe.web.entity.auth.AuthUser;
import com.gx.obe.web.entity.evaluation.EvaluationStep;
import com.gx.obe.web.entity.expert.ExpertEvaluationResult;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.SupplierEvaluationResultService;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;
import com.gx.obe.web.service.evaluation.EvaluationStepService;

/**
 * @Description: 复议原因
 * @author guoyr
 */
public class ReReasonDialog extends EDialog {
	private ExpertEvaluationResult expertEvaluationResult;
	private SupplierEvaluationResultService supplierEvaluationResultService;
	private Composite evaluateGroup;
//	private EButton saveBtn;
	private TenderProjectEntity tenderProject;
	private Supplier supplier;
	private AuthUser curUser;
	private Text requirementText;
	private boolean applyFlag;
	private boolean isUnqualified = false;
	private SupplierEvaluationResult supplierEvaluationResult;
	private EButton submitBtn;
	private EvaluationStep evaluationStep;
	private EvaluationStepService evaluationStepService = new EvaluationStepService();
	private boolean isNull = false;
	private boolean isSave = false;
	{
		LogUtils.logClass(ReReasonDialog.class);
	}
	
	/**
	 * @param parentShell
	 * @param tenderProjectEntity
	 * @param supplier
	 * @param evaluationFactor
	 * @param expertEvaluationResult
	 * @param evalFactorResult
	 * @param callBack
	 */
	public ReReasonDialog(Shell parentShell, TenderProjectEntity tenderProject,EvaluationStep evaluationStep ) {
		super(parentShell);
		setTitle("请填写得分修正原因");
		this.tenderProject = tenderProject;
		this.evaluationStep = evaluationStep;
		supplierEvaluationResultService = new SupplierEvaluationResultService();
	}

	/** 
	 * @Description: 
	 * @param parent
	 * @return
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite top = (Composite) super.createDialogArea(parent);
		GridLayout gl_top = new GridLayout(1, false);
		gl_top.verticalSpacing = 0;
		gl_top.horizontalSpacing = 0;
		gl_top.marginHeight = 0;
		gl_top.marginWidth = 0;
		top.setLayout(gl_top);
		
		Composite container = new Composite(top, SWT.NONE);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		GridLayout gl_container = new GridLayout(1, false);
		container.setLayout(gl_container);
		
		evaluateGroup = new Composite(container, SWT.NONE);
		evaluateGroup.setFont(SWTResourceManager.getFont("宋体", 9, SWT.BOLD));
		GridLayout gl_evaluateGroup = new GridLayout(1, false);
		evaluateGroup.setLayout(gl_evaluateGroup);
		evaluateGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite composite_3 = new Composite(evaluateGroup, SWT.NONE);
		GridData gd_composite_3 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_composite_3.heightHint = 50;
		composite_3.setLayoutData(gd_composite_3);
		GridLayout gl_composite_3 = new GridLayout(1, false);
		gl_composite_3.verticalSpacing = 1;
		gl_composite_3.horizontalSpacing = 1;
		gl_composite_3.marginHeight = 1;
		gl_composite_3.marginWidth = 1;
		composite_3.setLayout(gl_composite_3);
		composite_3.setBackground(ColorConstants.BORDER_LINE_COLOR);
		Composite composite_1 = new Composite(composite_3, SWT.NONE);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		composite_1.setLayout(new GridLayout(1, false));
		
		Label label_2 = new Label(composite_1, SWT.NONE);
		label_2.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.BOLD));
		label_2.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label_2.setText("得分修正原因");
		
//		ESepator req_sep = new ESepator(composite_3, SWT.NONE);
//		req_sep.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
//		req_sep.setBackground(ColorConstants.BORDER_LINE_COLOR);
//		req_sep.setLineStyle(SWT.LINE_DOT);
//		req_sep.setLineWidth(2);
		
		requirementText = new Text(composite_3, SWT.V_SCROLL | SWT.MULTI | SWT.WRAP);
		requirementText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		requirementText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		requirementText.setTextLimit(500);
		
		ESepator label_7 = new ESepator(top, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_7.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label_7.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite = new Composite(top, SWT.NONE);
		composite.setBackgroundMode(SWT.INHERIT_FORCE);
		composite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		GridLayout gl_composite = new GridLayout(3, false);
		composite.setLayout(gl_composite);
		
//		saveBtn = new EButton(composite, SWT.NONE);
//		saveBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
//		saveBtn.addMouseListener(new MouseAdapter() {
//			@Override
//			public void mouseUp(MouseEvent e) {
//				saveEvaluationAction(false);
//			}
//		});
//		GridData gd_confirmBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
//		gd_confirmBtn.widthHint = 90;
//		saveBtn.setLayoutData(gd_confirmBtn);
//		saveBtn.setText(Messages.Button_save);
		
		submitBtn = new EButton(composite, SWT.NONE);
		submitBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/submit.png"));
		submitBtn.setMinWidth(90);
		submitBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				saveEvaluationAction(true);
			}
		});
		submitBtn.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1));
		submitBtn.setText(Messages.Button_save);
		
		EButton cancel_btn = new EButton(composite, SWT.NONE);
		cancel_btn.setMinWidth(90);
		cancel_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		cancel_btn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				getShell().close();
			}
		});
		cancel_btn.setText(Messages.Button_close);
		new Label(composite, SWT.NONE);

		return container;
	}

	
	/**
	 * @Description: 保存得分修正原因
	 * @author guoyr
	 */
	private void saveEvaluationAction(boolean isSubmit){
		if(StringUtils.isEmpty(requirementText.getText())){
			MessageDialog.openShortMessage("请先填写原因再保存！");
			isNull = true;
			return;
		}
		evaluationStep.setEvaluationOpinion(requirementText.getText());
		evaluationStepService.updateAssignProperty(evaluationStep, new String[] {"evaluationOpinion"});
		isSave = true;
		close();
	}
	
	
	/**
	 * @Description: 判断原因是否为空
	 * @author guoyr
	 */
	public boolean isNull(){
		return isNull;
	}
	
	/**
	 * @Description: 判断点击的按钮是否为保存
	 * @author guoyr
	 */
	public boolean isSave(){
		return isSave;
	}
	
	
	@Override
	public boolean close() {
		isNull = true;
		return super.close();
	}

	@Override
	protected Point getInitialSize() {
		return new Point(700, 500);
	}
}
