package com.gx.obe.management.dialog;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.VerifyRequiredListener;
import org.eclipse.swt.core.utils.ValidateUtils;
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.custom.CLabel;
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.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.Constants;
import com.gx.obe.components.core.enumeration.CommonEnum;
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.evaluation.EvaluationRoom;
import com.gx.obe.web.service.evaluation.EvaluationRoomService;
import com.gx.utils.NowTimeUtils;
import com.swtdesigner.ResourceManager;

/**
 * @Description: 
 * @author guoyr
 */
public class EvaluationRoomDialog extends EDialog {
	
	private EvaluationRoomService evaluationRoomService ;
	private EvaluationRoom evaluationRoom;
	private String evaluationAreaId;
	private CallBack callBack;
	private CLabel evalAreaNameText;
	private Text evalRoomNameText;
	private Text evalRoomAddressText;
	private EButton save_btn;
	
	{
		LogUtils.logClass(EvaluationRoomDialog.class);
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public EvaluationRoomDialog(Shell parentShell, String evalAreaId, EvaluationRoom evaluationRoom, CallBack callBack) {
		super(parentShell);
		this.callBack = callBack;
		this.evaluationRoom = evaluationRoom;
		this.evaluationAreaId = evalAreaId;
		evaluationRoomService = new EvaluationRoomService();
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gl_container = new GridLayout(1, false);
		gl_container.verticalSpacing = 0;
		gl_container.horizontalSpacing = 0;
		gl_container.marginHeight = 0;
		gl_container.marginWidth = 0;
		container.setLayout(gl_container);
		
		Composite evaluationRoomFormComposite = new Composite(container, SWT.NONE);
		evaluationRoomFormComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		GridLayout gl_evaluationRoomFormComposite = new GridLayout(1, false);
		gl_evaluationRoomFormComposite.marginHeight = 0;
		gl_evaluationRoomFormComposite.marginWidth = 0;
		gl_evaluationRoomFormComposite.verticalSpacing = 0;
		evaluationRoomFormComposite.setLayout(gl_evaluationRoomFormComposite);
		
		Composite composite_3 = new Composite(evaluationRoomFormComposite, SWT.NONE);
		composite_3.setLayout(new GridLayout(2, false));
		GridData gd_composite_3 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		composite_3.setLayoutData(gd_composite_3);
		
		Label label = new Label(composite_3, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("所属评标区：");
		
		evalAreaNameText = new CLabel(composite_3, SWT.NONE);
		evalAreaNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		
		Label label_1 = new Label(composite_3, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText("评标室名称：");
		
		evalRoomNameText = new Text(composite_3, SWT.BORDER);
		evalRoomNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		evalRoomNameText.addVerifyListener(new VerifyRequiredListener(evalRoomNameText, "评标室名称不能为空！"));
		evalRoomNameText.setTextLimit(50);
		
		Label label_2 = new Label(composite_3, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.FILL, false, true, 1, 1));
		label_2.setText("评标室地址：");
		
		evalRoomAddressText = new Text(composite_3, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		GridData gd_text_1 = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		gd_text_1.heightHint = 43;
		evalRoomAddressText.setLayoutData(gd_text_1);
		evalRoomAddressText.addVerifyListener(new VerifyRequiredListener(evalRoomAddressText, "评标室地址不能为空！"));
		evalRoomAddressText.setTextLimit(200);
		
		ESepator label_3 = new ESepator(container, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		label_3.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		
		Composite composite_1 = new Composite(container, 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.FILL, true, false, 1, 1));
		composite_1.setLayout(new GridLayout(2, false));
		
		save_btn = new EButton(composite_1, SWT.NONE );
		save_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		GridData gd_saveBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_saveBtn.widthHint = 90;
		save_btn.setLayoutData(gd_saveBtn);
		save_btn.setText(Messages.Button_save);
		save_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				saveEvalBidRoomAction();
			}
		});
		
		EButton close_btn = new EButton(composite_1, SWT.NONE);
		close_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		GridData gd_button = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
		gd_button.widthHint = 90;
		close_btn.setLayoutData(gd_button);
		close_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				getShell().close();
			}
		});
		close_btn.setText(Messages.Button_close);

		init();
		return container;
	}
	
	/**
	 * @Description: 初始化
	 * @author guoyr
	 */
	private void init(){
		if(null == evaluationRoom){
			evaluationRoom = new EvaluationRoom();
		}
		refreshEvalBidRoomForm();
	}
	
	/**
	 * @Description: 刷新表单
	 * @author guoyr
	 */
	private void refreshEvalBidRoomForm(){
		evalRoomAddressText.setText(ObjectUtils.getObjString(evaluationRoom.getEvalRoomAddress()));
		evalRoomNameText.setText(ObjectUtils.getObjString(evaluationRoom.getEvalRoomName()));
	}
	
	/**
	 * @Description: 保存评标室
	 * @author guoyr
	 */
	private void saveEvalBidRoomAction(){
		if(!save_btn.getEnabled()) return ;
		if(!ValidateUtils.checkInput(evalRoomNameText.getParent())) return ;
		
		String evaluationRoomName = evalRoomNameText.getText().trim();
		String id = null;
		if(StringUtils.isEmpty(evaluationRoom.getId())){
			evaluationRoom.setCreateTime(NowTimeUtils.getNowDate());
			evaluationRoom.setCreateUserId(Constants.USER.getId());
			evaluationRoom.setUseStatus(CommonEnum.REAL_STATUS);
		}else {
			id = evaluationRoom.getId();
		}
 
		List<Object> objIds = evaluationRoomService.getEvaluationRoomIdList(id,evaluationRoomName,evaluationAreaId);
		if(null != objIds && objIds.size() > 0){
			MessageDialog.openWarning(getShell(), evaluationRoomName+Messages.Tips_AlreadyExistsPleaseRetype);
			evalRoomNameText.setFocus();
			return ;
		}
		evaluationRoom.setEvalRoomName(evaluationRoomName);
		evaluationRoom.setEvalRoomAddress(evalRoomAddressText.getText().trim());
		// 评标室类型：默认为本地
		evaluationRoom.setEvalRoomType("01");
		evaluationRoom.setEvaluationAreaId(evaluationAreaId);
		evaluationRoom.setModifyTime(NowTimeUtils.getNowDate());
		evaluationRoom.setModifyUserId(Constants.USER.getId());
		evaluationRoom = evaluationRoomService.saveOrUpdateReturn(evaluationRoom);
		if(null != callBack && null != evaluationRoom){
			callBack.callBackObject(evaluationRoom);
		}
		getShell().close();
	}
	
	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(398, 237);
	}
}
