package com.gx.obe.management.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.CallBack;
import org.eclipse.swt.core.listener.VerifyEnNameListener;
import org.eclipse.swt.core.listener.VerifyRequiredListener;
import org.eclipse.swt.core.utils.SwtUtils;
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.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.dialog.MessageViewDialog;
import com.gx.obe.message.Messages;
import com.gx.obe.thread.CheckDownExpertThread;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.util.utils.StringUtil;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;
/**
* @Description: 验证专家
* @author wangxiang
*/
public class CheckExpertDialog extends EDialog {

	private Text userNameText;
	private Text passwordText;
	private Text stageIdText;
	private EButton saveUserBtn;
	private TenderProjectEntity tenderProjectEntity;
	boolean isNew = true;
	private String expertName;
	private String password;
	private CallBack callback;
//	private Text idCardText;
//	private Text resonText;
//	private Combo resonCombo;
//	private CLabel idCardLabel;
//	private CLabel resonLabel;
	private String idCardString;
	private String resonString;
	private String expertStatus = "00";
	private Composite compContent;
	{
		LogUtils.logClass(CheckExpertDialog.class);
	}

	public CheckExpertDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity,CallBack callback) {
		super(parentShell);
		this.tenderProjectEntity = tenderProjectEntity;
		setTitle(Messages.Verify_expert_info);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite content = (Composite) super.createDialogArea(parent);
		content.setBackgroundMode(SWT.INHERIT_FORCE);
		GridLayout gl_content = new GridLayout(1, false);
		gl_content.verticalSpacing = 0;
		gl_content.horizontalSpacing = 0;
		gl_content.marginHeight = 0;
		gl_content.marginWidth = 0;
		content.setLayout(gl_content);

		Composite compAll = new Composite(content, SWT.NONE);
		compAll.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		GridLayout gl_compAll = new GridLayout(1, false);
		gl_compAll.marginHeight = 0;
		gl_compAll.marginWidth = 0;
		compAll.setLayout(gl_compAll);

		compContent = new Composite(compAll, SWT.NONE);
		GridLayout gl_compContent = new GridLayout(2, false);
		gl_compContent.verticalSpacing = 30;
		gl_compContent.marginHeight = 50;
		gl_compContent.marginWidth = 100;
		compContent.setLayout(gl_compContent);
		GridData gd_compContent = new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1);
		gd_compContent.widthHint = 600;
		compContent.setLayoutData(gd_compContent);

		CLabel usLoginAccountLabel = new CLabel(compContent, SWT.NONE);
		usLoginAccountLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		usLoginAccountLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		usLoginAccountLabel.setText(Messages.TenderPackage + Messages.Config_platformCode + " :");

		stageIdText = new Text(compContent, SWT.BORDER);
		stageIdText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		stageIdText.setEnabled(false);
		GridData gd_stageIdText = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);
		gd_stageIdText.widthHint = 229;
		stageIdText.setLayoutData(gd_stageIdText);
//		stageIdText.addVerifyListener(new VerifyRequiredListener(stageIdText, "编码不能为空!"));
//		stageIdText.addVerifyListener(new VerifyEnNameListener());
//		stageIdText.setTextLimit(20);

//		CLabel recordLabel = new CLabel(compContent, SWT.NONE);
//		recordLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
//		recordLabel.setText("备案：");
//		
//		resonCombo = new Combo(compContent,SWT.NONE);
//		resonCombo.addSelectionListener(new SelectionAdapter() {
//			@Override
//			public void widgetSelected(SelectionEvent e) {
//				System.out.println(resonCombo.getSelectionIndex());
//				refreshInfo();
//			}
//		});
//		GridData gd_inquireEnvelopCombo = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
//		gd_inquireEnvelopCombo.widthHint = 251;
//		resonCombo.setLayoutData(gd_inquireEnvelopCombo);
//		resonCombo.add("-正常-", 0);
//		resonCombo.add("-备案-", 1);
//		resonCombo.select(0);
//		resonCombo.setEnabled(false);
//		
//		idCardLabel = new CLabel(compContent, SWT.NONE);
//		idCardLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
//		idCardLabel.setText("身份证：");
		
//		idCardText = new Text(compContent, SWT.BORDER | SWT.DRAW_TAB);
//		idCardText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
//		idCardText.setTextLimit(20);
//		
//		resonLabel = new CLabel(compContent, SWT.NONE);
//		resonLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
//		resonLabel.setText("理由：");
//		
//		resonText = new Text(compContent, SWT.BORDER | SWT.DRAW_TAB);
//		resonText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
//		resonText.setTextLimit(20);
		
		CLabel userNameLabel = new CLabel(compContent, SWT.NONE);
		userNameLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		userNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		userNameLabel.setText(Messages.FullName+"：");

		userNameText = new Text(compContent, SWT.BORDER | SWT.DRAW_TAB);
		userNameText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		userNameText.addVerifyListener(new VerifyRequiredListener(userNameText, "姓名不能为空！"));
		userNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		userNameText.setTextLimit(20);

		CLabel passwordLabel = new CLabel(compContent, SWT.NONE);
		passwordLabel.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		passwordLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		passwordLabel.setText(Messages.Password + "：");

		passwordText = new Text(compContent, SWT.BORDER | SWT.PASSWORD | SWT.DRAW_TAB);
		passwordText.setFont(SWTResourceManager.getFont("微软雅黑", 10, SWT.NORMAL));
		GridData gd_passwordText = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_passwordText.widthHint = 160;
		passwordText.setLayoutData(gd_passwordText);
		passwordText.setTextLimit(32);
		passwordText.addVerifyListener(new VerifyRequiredListener(passwordText, "密码不能为空！"));
		passwordText.addVerifyListener(new VerifyEnNameListener("您输入的密码无效！"));

		ESepator label_2 = new ESepator(content, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		label_2.setBackground(ColorConstants.SEP_LIGHT_COLOR);
		Composite operateComposite = new Composite(content, SWT.NONE);
		operateComposite.setBackgroundMode(SWT.INHERIT_FORCE);
		operateComposite.setBackgroundImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/fill_bg.png"));
		GridLayout gl_operateComposite = new GridLayout(2, false);
		operateComposite.setLayout(gl_operateComposite);
		operateComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		saveUserBtn = new EButton(operateComposite, SWT.NONE);
		saveUserBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		saveUserBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				saveUserAction();
			}
		});
		GridData gd_saveUserBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		saveUserBtn.setLayoutData(gd_saveUserBtn);
		saveUserBtn.setText(Messages.Button_validateExpert);

		EButton cancel_btn = new EButton(operateComposite, SWT.NONE);
		cancel_btn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		cancel_btn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				cancelAction();
			}
		});
		GridData gd_cancelBtn = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		cancel_btn.setLayoutData(gd_cancelBtn);
		cancel_btn.setText(Messages.Button_close);

		init();
		return content;
	}

	@Override
	protected Point getInitialSize() {
		return new Point(494, 320);
	}

	/**
	 * @Description: 初始化界面
	 */
	private void init() {
		refreshForm();
		refreshInfo();
		getShell().pack();
		SwtUtils.setCenter(getShell());
	}

	/**
	 * @Description: 身份证和理由
	 * @author wangxiang
	 */
	private void refreshInfo(){
//		if(resonCombo.getSelectionIndex() == 0){
//			expertStatus = "00";
//			idCardText.setEnabled(false);
//			resonText.setEnabled(false);
//		}else if(resonCombo.getSelectionIndex() == 1){
//			expertStatus = "01";
//			idCardText.setEnabled(true);
//			resonText.setEnabled(true);
//		}
//		compContent.layout();
	}
	
	/**
	 * @Description: 刷新表单
	 * @author wangxiang
	 */
	private void refreshForm() {
		if (null != tenderProjectEntity && null != tenderProjectEntity.getId()) {
			if (null != tenderProjectEntity.getStageId()) {
				stageIdText.setText(tenderProjectEntity.getStageId());
			} else {
				return;
			}
		} else {
			return;
		}
	}

	/**
	 * @Description: 保存
	 */
	private void saveUserAction() {
		if (!ValidateUtils.checkInput(userNameText.getParent())) {
			return;
		}
		if (!ValidateUtils.checkInput(passwordText.getParent())) {
			return;
		}
		
		expertName = userNameText.getText();
		password = StringUtil.encodeString(passwordText.getText().trim());
//		idCardString = idCardText.getText();
//		resonString = resonText.getText();
//		if(idCardString.equals("")){
//			idCardString = null;
//		}
//		if(resonString.equals("")){
//			resonString = null;
//		}

		MessageViewDialog dialog = new MessageViewDialog(getShell(), -1);
		dialog.setTitle(Messages.Button_validateExpert);
		new CheckDownExpertThread(tenderProjectEntity, expertName, password,idCardString,resonString,expertStatus, dialog.getCallBack()).start();
		dialog.open();

		getShell().close();
	}


	/**
	 * @Description: 取消
	 */
	private void cancelAction() {
		getShell().close();
	}
}
