package com.gx.obe.management.dialog;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.ImageResolver;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.listener.VerifyCardIdListener;
import org.eclipse.swt.core.listener.VerifyEnNameListener;
import org.eclipse.swt.core.listener.VerifyPasswordListener;
import org.eclipse.swt.core.listener.VerifyPhoneListener;
import org.eclipse.swt.core.listener.VerifyRequiredListener;
import org.eclipse.swt.core.utils.FileDialogUtils;
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.ELink;
import org.eclipse.swt.core.widgets.ESepator;
import org.eclipse.swt.core.widgets.ImageButton;
import org.eclipse.swt.custom.CLabel;
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.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
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.business.entity.Worker;
import com.gx.obe.business.enumeration.ExpertEnum;
import com.gx.obe.business.enumeration.UserEnum;
import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.common.dialog.SelectUserCommonDialog;
import com.gx.obe.components.core.enumeration.CommonEnum;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;
import com.gx.obe.http.util.Base64;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.util.utils.LogUtils;
import com.gx.obe.util.utils.MD5Utils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.SpellHelper;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.util.utils.UuidUtils;
import com.gx.obe.web.entity.auth.AuthUser;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.UserRoleService;
import com.gx.obe.web.service.UserService;
import com.gx.obe.web.service.WorkerService;
import com.gx.obe.config.utils.InputStreamUtil;
import com.gx.utils.NowTimeUtils;
import org.eclipse.swt.core.utils.WidgetUtils;
import com.swtdesigner.ResourceManager;

/**
 * @Description: 新增工作人员
 * @author wangxiang
 */
public class EditWorkerDialog extends EDialog {
	
	private UserService userService ;
	private UserRoleService userRoleService;
	private WorkerService workerService;
	private Worker worker;
	private AuthUser user;
	private boolean changeAvatorImage = false;
	private ImageResolver imageResolver = new ImageResolver();
	private Image defaultAvatorImage;
	private Composite genderComposite;
	private ImageButton userAvatorLabel;
	private Text userNameText;
	private Text passwordText;
	private Text passwordAgainText;
	private Text IDCardText;
	private Text professionalTitleText;
	private Text companyText;
	private Text specialtyText;
	private Text phoneText;
	private Text userAccountText;
	private Text areaCodeText;
	private EButton saveUserBtn;
	private TenderProjectEntity tenderProjectEntity;
	private String workerType;
	private ELink link;
	private ELink createAccountLink;
	private Button initialPasswordButton;
	private final String DEFAULT_PASSWORD = PropertiesUtils.getProperty(ConfigConstants.EXPERT_DEFAULT_PASSWORD, "123456");
	
	{
		LogUtils.logClass(EditWorkerDialog.class);
	}
	
	public EditWorkerDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity, Worker worker,String workerType) {
		super(parentShell);
		this.tenderProjectEntity = tenderProjectEntity;
		this.worker = worker;
		this.workerType = workerType;
		userService = new UserService();
		userRoleService = new UserRoleService();
	}

	@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);
		
		Composite compContent = new Composite(compAll, SWT.NONE);
		GridLayout gl_compContent = new GridLayout(3, false);
		gl_compContent.marginWidth = 20;
		compContent.setLayout(gl_compContent);
		compContent.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1));
		
		CLabel userNameLabel = new CLabel(compContent, SWT.NONE);
		userNameLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		userNameLabel.setText(Messages.FullName + "：");
		
		Composite userNameComposite = new Composite(compContent, SWT.NONE);
		userNameComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		GridLayout gl_userNameComposite = new GridLayout(2, false);
		gl_userNameComposite.horizontalSpacing = 0;
		gl_userNameComposite.verticalSpacing = 0;
		gl_userNameComposite.marginWidth = 0;
		gl_userNameComposite.marginHeight = 0;
		userNameComposite.setLayout(gl_userNameComposite);
		
		userNameText = new Text(userNameComposite, SWT.BORDER | SWT.DRAW_TAB);
		userNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		userNameText.setTextLimit(20);
		userNameText.addVerifyListener(new VerifyRequiredListener(userNameText, "姓名不能为空！"));
		
		link = new ELink(userNameComposite, SWT.NONE);
		link.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				queryUserAction();
			}
		});
		link.setText("检测");
		link.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/query.png"));
		
		Composite composite_1 = new Composite(compContent, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(1, false);
		gl_composite_1.marginHeight = 0;
		composite_1.setLayout(gl_composite_1);
		GridData gd_composite_1 = new GridData(SWT.FILL, SWT.TOP, false, true, 1, 6);
		gd_composite_1.widthHint = 150;
		gd_composite_1.heightHint = 180;
		composite_1.setLayoutData(gd_composite_1);
		
		userAvatorLabel = new ImageButton(composite_1, SWT.BORDER | SWT.BOTTOM);
		userAvatorLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		userAvatorLabel.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/male_96.png"));
		userAvatorLabel.setBorderColor(ColorConstants.BORDER_LINE_COLOR);
		
		EButton button_2 = new EButton(composite_1, SWT.NONE);
		button_2.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				uploadUserAvatarAction();
			}
		});
		button_2.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/upload.png"));
		button_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		button_2.setText(Messages.UploadPicture);
		
		CLabel usLoginAccountLabel = new CLabel(compContent, SWT.NONE);
		usLoginAccountLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		usLoginAccountLabel.setText(Messages.Account + "：");
		
		Composite composite = new Composite(compContent, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		GridLayout gl_composite = new GridLayout(2, false);
		gl_composite.marginWidth = 0;
		gl_composite.marginHeight = 0;
		gl_composite.horizontalSpacing = 0;
		gl_composite.verticalSpacing = 0;
		composite.setLayout(gl_composite);
		
		userAccountText = new Text(composite, SWT.BORDER);
		userAccountText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		userAccountText.setTextLimit(20);
		userAccountText.addVerifyListener(new VerifyEnNameListener());
		userAccountText.addVerifyListener(new VerifyRequiredListener(userAccountText, "账号不能为空！"));
		
		createAccountLink = new ELink(composite, SWT.NONE);
		createAccountLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				createAccountAction();
			}
		});
		createAccountLink.setText("生成账号");
		createAccountLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/pen.png"));
		
		CLabel passwordLabel = new CLabel(compContent, SWT.NONE);
		passwordLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		passwordLabel.setText(Messages.Password + "：");
		
		Composite passwordCmposite = new Composite(compContent, SWT.NONE);
		GridLayout gl_passwordCmposite = new GridLayout(2, false);
		gl_passwordCmposite.horizontalSpacing = 0;
		gl_passwordCmposite.marginHeight = 0;
		gl_passwordCmposite.marginWidth = 0;
		passwordCmposite.setLayout(gl_passwordCmposite);
		passwordCmposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1));
		
		passwordText = new Text(passwordCmposite, SWT.BORDER | SWT.PASSWORD | SWT.DRAW_TAB);
		passwordText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		passwordText.setTextLimit(32);
		passwordText.addVerifyListener(new VerifyRequiredListener(passwordText, MessageFormat.format(Messages.Tips_required, Messages.Password)));
		
		CLabel passwordAgainLabel = new CLabel(compContent, SWT.NONE);
		passwordAgainLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		passwordAgainLabel.setText(Messages.PasswordConfirm + "：");
		
		passwordAgainText = new Text(compContent, SWT.BORDER | SWT.PASSWORD | SWT.DRAW_TAB);
		passwordAgainText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		passwordAgainText.addVerifyListener(new VerifyRequiredListener(passwordText, "确认密码不能为空！"));
		passwordAgainText.setTextLimit(32);
		
		Composite initialPasswordComposite = new Composite(passwordCmposite, SWT.NONE);
		GridLayout gl_initialPasswordComposite = new GridLayout(1, false);
		gl_initialPasswordComposite.marginHeight = 0;
		initialPasswordComposite.setLayout(gl_initialPasswordComposite);
		initialPasswordComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true, 1, 1));
		
		initialPasswordButton = new Button(initialPasswordComposite, SWT.CHECK);
		initialPasswordButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				initialPasswordSelectedAction();
			}
		});
		initialPasswordButton.setText(Messages.DefaultPassword);
		
		Label passwordTips = new Label(passwordCmposite, SWT.WRAP);
		GridData gd_passwordTips = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_passwordTips.widthHint = 300;
		passwordTips.setLayoutData(gd_passwordTips);
		passwordTips.setText(VerifyPasswordListener.PASSWOR_TIPS);
		passwordTips.setForeground(ColorConstants.NOTE_TIPS_COLOR);
		
		CLabel label = new CLabel(compContent, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText(Messages.Gender + "：");
		
		genderComposite = new Composite(compContent, SWT.NONE);
		genderComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		GridLayout gl_genderComposite = new GridLayout(2, false);
		gl_genderComposite.marginHeight = 0;
		genderComposite.setLayout(gl_genderComposite);
		
		Button button = new Button(genderComposite, SWT.RADIO);
		button.setSelection(true);
		button.setData(UserEnum.MALE);
		button.setText(Messages.Male);
		
		Button button_1 = new Button(genderComposite, SWT.RADIO);
		button_1.setData(UserEnum.FEMALE);
		button_1.setText(Messages.Female);
		
		CLabel IDCardLabel = new CLabel(compContent, SWT.NONE);
		IDCardLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		IDCardLabel.setText(Messages.IDCard + "：");
		
		IDCardText = new Text(compContent, SWT.BORDER | SWT.DRAW_TAB);
		IDCardText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
		IDCardText.setTextLimit(18);
		IDCardText.addVerifyListener(new VerifyRequiredListener(IDCardText, MessageFormat.format(Messages.Tips_required, Messages.IDCard)));
		IDCardText.addVerifyListener(new VerifyCardIdListener(MessageFormat.format(Messages.Tips_pleaseEnterTheCorrect, Messages.IDCard)));
		
		CLabel label_5 = new CLabel(compContent, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_5.setText(Messages.LinkerTel + "：");
		
		phoneText = new Text(compContent, SWT.BORDER | SWT.DRAW_TAB);
		phoneText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		phoneText.setTextLimit(20);
//		phoneText.addVerifyListener(new VerifyRequiredListener(phoneText,"联系电话不能为空！"));
		phoneText.addVerifyListener(new VerifyPhoneListener(MessageFormat.format(Messages.Tips_pleaseEnterTheValid, Messages.LinkerTel)));
		new Label(compContent, SWT.NONE);
		
		CLabel label_1 = new CLabel(compContent, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText(Messages.ProfessionalTitle);
		
		professionalTitleText = new Text(compContent, SWT.BORDER | SWT.DRAW_TAB);
		GridData gd_professionalTitleText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_professionalTitleText.widthHint = 200;
		professionalTitleText.setLayoutData(gd_professionalTitleText);
//		professionalTitleText.addVerifyListener(new VerifyRequiredListener(professionalTitleText,"等级/职称不能为空！"));
		professionalTitleText.setTextLimit(50);
		
		CLabel label_3 = new CLabel(compContent, SWT.NONE);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_3.setText(Messages.Specialty+"：");
		
		specialtyText = new Text(compContent, SWT.BORDER | SWT.DRAW_TAB);
		specialtyText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
//		specialtyText.addVerifyListener(new VerifyRequiredListener(specialtyText,"专业不能为空！"));
		specialtyText.setTextLimit(100);
		
		CLabel label_7 = new CLabel(compContent, SWT.NONE);
		label_7.setText(Messages.AdministrativeCode);
		
		areaCodeText = new Text(compContent, SWT.BORDER | SWT.DRAW_TAB);
		areaCodeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		areaCodeText.setTextLimit(50);
		
		CLabel label_4 = new CLabel(compContent, SWT.NONE);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_4.setText(Messages.WorkCompany+"：");
		
		companyText = new Text(compContent, SWT.BORDER | SWT.WRAP | SWT.DRAW_TAB);
		companyText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		companyText.setTextLimit(100);
		
		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) {
				saveWorkerAction();
			}
		});
		GridData gd_saveUserBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_saveUserBtn.widthHint = 90;
		saveUserBtn.setLayoutData(gd_saveUserBtn);
		saveUserBtn.setText(Messages.Button_save);
		
		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);
		gd_cancelBtn.widthHint = 90;
		cancel_btn.setLayoutData(gd_cancelBtn);
		cancel_btn.setText(Messages.Button_close);
		
		init();
		return content;
	}

	@Override
	protected Point getInitialSize() {
		return new Point(542, 474);
	}
	
	/**
	 * @Description: 初始化界面
	 */
	private void init(){
		workerService = new WorkerService();
		defaultAvatorImage = ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/male_96.png");
//		initComboBox();
		if(null != worker && null != worker.getUserId()){
			user = (AuthUser) userService.findById(worker.getUserId());
		}
		refreshForm();
		getShell().pack();
		SwtUtils.setCenter(getShell());
	}
	/**
	 * @Description: 初始密码按钮勾选事件
	 * @author chenxw
	 */
	private void initialPasswordSelectedAction() {
		boolean isDefaultPassword = initialPasswordButton.getSelection();
		if (isDefaultPassword) {
			passwordText.setText(DEFAULT_PASSWORD);
			passwordAgainText.setText(DEFAULT_PASSWORD);
		} else {
			passwordText.setEnabled(!isDefaultPassword);
			passwordAgainText.setEnabled(!isDefaultPassword);
		}
	}
	/**
	 * @Description: 刷新表单
	 * @author guoyr
	 */
	private void refreshForm(){
		if(null == worker){
			worker = new Worker();
		}
		if(null != user && null != user.getId()){
			// 账号输入框不可编辑，可复制操作。
			userNameText.setEditable(false);
			userAccountText.setEditable(false);
			passwordText.setEnabled(false);
			passwordAgainText.setEnabled(false);
			WidgetUtils.setControlVisible(link, false);
			WidgetUtils.setControlVisible(createAccountLink, false);
			WidgetUtils.setControlVisible(initialPasswordButton, false);
		}else {
			passwordText.addVerifyListener(new VerifyRequiredListener(passwordText,MessageFormat.format(Messages.Tips_required, Messages.Password)));
			user = new AuthUser();
			user.setGender(UserEnum.MALE);
			link.setEnabled(false);
			userAccountText.setEnabled(false);
			createAccountLink.setEnabled(false);
			userNameText.addModifyListener(new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					link.setEnabled(userNameText.getText().length() > 1);
					userAccountText.setEnabled(userNameText.getText().length() > 1);
					createAccountLink.setEnabled(userNameText.getText().length() > 1);
				}
			});
		}
		if(ExpertEnum.MONITOR.equals(workerType)){
			user.setUserType(UserEnum.MONITOR);
			setTitle((null == user.getId() ? Messages.Button_add : Messages.Button_modify) + Messages.Supervisor);
		}else if(ExpertEnum.WORKER.equals(workerType)){
			setTitle((null == user.getId() ? Messages.Button_add : Messages.Button_modify) + Messages.Worker);
			user.setUserType(UserEnum.WORKER);
		}

		userAccountText.setText(ObjectUtils.getObjString(user.getUserAccount()));
		userNameText.setText(ObjectUtils.getObjString(user.getUserName()));
		IDCardText.setText(ObjectUtils.getObjString(user.getUserIdcard()));
		phoneText.setText(ObjectUtils.getObjString(user.getPhone()));
		professionalTitleText.setText(ObjectUtils.getObjString(user.getProfessionalTitle()));
		specialtyText.setText(ObjectUtils.getObjString(user.getSpecialty()));
		areaCodeText.setText(ObjectUtils.getObjString(user.getAreaCode()));
		companyText.setText(ObjectUtils.getObjString(user.getCompany()));
		passwordText.setText(ObjectUtils.getObjString(user.getUserPassword()));
		passwordAgainText.setText(ObjectUtils.getObjString(user.getUserPassword()));
//		WidgetUtil.setComboSelection(typeCombo, user.getUserType());
		WidgetUtils.setRadioChecked(genderComposite, user.getGender());
		Image image = null;
		if(null != user.getAvatar()){
			image = imageResolver.getImage(Base64.decode(user.getAvatar()));
		}
		if(null !=image){
			userAvatorLabel.setImage(image,defaultAvatorImage.getBounds().width, defaultAvatorImage.getBounds().height);
		}else {
			userAvatorLabel.setImage(defaultAvatorImage);
		}
		saveUserBtn.setEnabled(true);
	}
	
	/**
	 * @Description: 保存
	 */
	private void saveWorkerAction(){
		saveUserBtn.forceFocus();
		if(!saveUserBtn.getEnabled())return;
		if(!ValidateUtils.checkInput(userAccountText.getParent())){
			return;
		}
		String password = passwordText.getText().trim();
		String account = userAccountText.getText().trim();
		if(null == user || null == user.getId()) {
			if(!VerifyPasswordListener.matcher(password)) {
				MessageDialog.openWarning(getShell(), VerifyPasswordListener.PASSWOR_TIPS);
				return ;
			}
			if (!passwordAgainText.getText().trim().equals(password)) {
				MessageDialog.openWarning(getShell(), Messages.Tips_matchPassword);
				passwordAgainText.setFocus();
				return;
			}
			if (password.equals(account)) {
				MessageDialog.openWarning(getShell(), "账号和密码不能相同，请重新输入！");
				return;
			}
		}
		user.setUserName(userNameText.getText().trim());
		user.setUserAccount(userAccountText.getText().trim());
		user.setUserIdcard(IDCardText.getText().trim());
		user.setDefaultPassword(initialPasswordButton.getSelection() ? CommonEnum.YES : CommonEnum.NO);
		user.setGender(ObjectUtils.getObjString(WidgetUtils.getRadioVal(genderComposite)));
		user.setSpecialty(specialtyText.getText().trim());
		user.setPhone(phoneText.getText().trim());
		user.setProfessionalTitle(professionalTitleText.getText().trim());
		user.setCompany(companyText.getText().trim());
		user.setAreaCode(areaCodeText.getText().trim());
		
		if(!valAccountAndIdCard()){
			return;
		}
		boolean saveFlag = false;
		worker.setProjectId(null != tenderProjectEntity.getParentId() ? tenderProjectEntity.getParentId() : tenderProjectEntity.getId());
		worker.setTenderId(tenderProjectEntity.getId());
		worker.setMemberType(workerType);
		// 修改
		if(null != user.getId()){
			String[] updateProperties = new String[]{"userName", "gender","userIdcard","phone","professionalTitle","specialty","areaCode","company"};
			// 如果修改了头像才去修改头像，减少修改属性，因为头像是直接保存在数据库中的
			if(changeAvatorImage){
				String[] newUpdateProperties = 	new String[updateProperties.length +1];
				newUpdateProperties[updateProperties.length] = "avatar";
				System.arraycopy(updateProperties, 0, newUpdateProperties, 0, updateProperties.length);
				updateProperties = newUpdateProperties;
			}
			// 修改用户信息
			saveFlag = userService.updateAssignProperty(user, updateProperties);
		}else {// 新增
			user.setCreateTime(NowTimeUtils.getNowDate());
			user.setUserPassword(MD5Utils.getMD5String(passwordText.getText().trim()));
			user.setUseStatus(CommonEnum.REAL_STATUS);
			user.setId(UuidUtils.getUUID());
			saveFlag = null != userService.createUserInfo(user);
//			if(saveFlag){
//				user.setId(null);
//			}
		}
		if(null == user){
			MessageDialog.openError(getShell(), Messages.Dialog_warmTips, Messages.Tips_saveFail);
			return;
		}
		
		// 封装工作人员信息
		worker.setUser(user);
		worker.setMemberType(workerType);
		worker.setTenderId(tenderProjectEntity.getId());
		worker.setUserId(user.getId());
		if(ExpertEnum.MONITOR.equals(workerType)){
			worker.setMonitorType(ExpertEnum.REALMONITOR);
		}
		// 保存工作人员信息
		worker = workerService.saveOrUpdateReturn(worker);
		if(null == worker.getId()){
			worker.setId(UuidUtils.getUUID());
			saveFlag = workerService.insert(worker) > 0;
			if(!saveFlag){
				worker.setId(null);
			}
		}else {
			saveFlag = workerService.saveOrUpdate(worker);
		}
		if(!saveFlag){
			MessageDialog.openError(getShell(), Messages.Dialog_warmTips, Messages.Tips_saveFail);;
			return;
		}
		if(ExpertEnum.MONITOR.equals(workerType)){
			userRoleService.initUserRole(user.getId(), UserEnum.ROLE_TYPE_EVALUATION_MONITOR);
		}else if(ExpertEnum.WORKER.equals(workerType)){
			userRoleService.initUserRole(user.getId(), UserEnum.ROLE_TYPE_EVALUATOIN_WORKER);
		}
		if(null != this.getCallBack()){
			this.getCallBack().callBackObject(worker);
		}
		getShell().close();
	}
	
	/**
	 * @Description: 上传头像
	 * @author guoyr
	 */
	private void uploadUserAvatarAction(){
		String imagePath = FileDialogUtils.getFileNames(SWT.SINGLE, "选择头像", new String[]{"*.png","*.jpg","*.*"});
		File imageFile = new File(imagePath);
		if(imageFile.exists()){
			if(imageFile.length() > 1024000*1) {
				MessageDialog.openWarning(getShell(), "请上传1M以内的头像！");
				return;
			}
			Image menuImage = imageResolver.loadImage(imageFile, 102,126);
			if(null != menuImage){
				try {
					changeAvatorImage = true;
					user.setAvatar(Base64.encode(ImageResolver.getImageBytes(menuImage)));
					FileInputStream in = new FileInputStream(imageFile);
					user.setAvatar(Base64.encode(InputStreamUtil.inputStreamTOByte(in)));
					userAvatorLabel.setImage(menuImage, 102,126);
					in.close();
					return;
				} catch (FileNotFoundException e) {
				} catch (IOException e) {
				}
			}
		}
		if(null != user.getAvatar()){
			userAvatorLabel.setImage(imageResolver.getImage(Base64.decode(user.getAvatar()), 102, 126));
		}else{
			userAvatorLabel.setImage(defaultAvatorImage);
		}
	}
	
	/**
	 * @Description: 产生账号
	 * @author guoyr
	 */
	private void createAccountAction() {
		if (createAccountLink.getEnabled() && userNameText.getText().trim().length() > 1) {
			userAccountText.setText(ObjectUtils.getObjString(SpellHelper.getEname(userNameText.getText().trim())));
		}
	}
	
	private void queryUserAction() {
		String userName = userNameText.getText().trim();
		if (StringUtils.isEmpty(userName)) {
			return;
		}
		List<AuthUser> userList = userService.getUserListByUserName(null, userName);
		if (CollectionUtils.isNull(userList)) {
			return;
		}
		
		String userId = null != user ? user.getId() : null;
		
		SelectUserCommonDialog selectUserDialog = new SelectUserCommonDialog(getShell(), userId, userList);
		selectUserDialog.open();
		AuthUser selectUser = selectUserDialog.getSelectUser();
		if (selectUser == null) {
			return;
		}
		
		List<Worker> workerList = workerService.getWorkerList(false,tenderProjectEntity.getId(), workerType);
		if(null != workerList) {
			for(Worker worker : workerList) {
				if(worker.getUserId().contains(selectUser.getId())) {
					MessageDialog.openError(getShell(), " 该用户已经存在！");
					return;
				}
			}
		}
		if(null == worker) {
			worker = new Worker();
		}
		// 封装工作人员信息
		worker.setUser(selectUser);
		worker.setMemberType(workerType);
		worker.setProjectId(null != tenderProjectEntity.getParentId() ? tenderProjectEntity.getParentId() : tenderProjectEntity.getId());
		worker.setTenderId(tenderProjectEntity.getId());
		worker.setMemberType(workerType);
		worker.setUserId(selectUser.getId());
		if(ExpertEnum.MONITOR.equals(workerType)){
			worker.setMonitorType(ExpertEnum.REALMONITOR);
		}
		if(null == worker.getId()) {
			worker = workerService.saveOrUpdateReturn(worker);
		}else {
			workerService.updateAssignProperty(worker, new String[] { "userId" });
		}
		if(ExpertEnum.MONITOR.equals(workerType)){
			userRoleService.initUserRole(selectUser.getId(), UserEnum.ROLE_TYPE_EVALUATION_MONITOR);
		}else if(ExpertEnum.WORKER.equals(workerType)){
			userRoleService.initUserRole(selectUser.getId(), UserEnum.ROLE_TYPE_EVALUATOIN_WORKER);
		}
		
		if (null != this.getCallBack()) {
			this.getCallBack().callBackObject(worker);
		}
		getShell().close();
	}
		
	/**
	 * @Description: 验证账号和身份证号是否重复
	 * @author wangxiang
	 * @return
	 */
	private boolean valAccountAndIdCard(){
		if(null != user){
			if(userService.valAccountAndIdCard(user.getUserAccount(),user.getId(),null) > 0){
				MessageDialog.openWarning(getShell(), user.getUserAccount()+Messages.Tips_existing_inputAgain);
				userAccountText.setFocus();
				return false;
			}
			if(null != user.getUserIdcard() && user.getUserIdcard().length() > 0){
				if(userService.valAccountAndIdCard(null,user.getId(),user.getUserIdcard()) > 0){
					MessageDialog.openWarning(getShell(), user.getUserIdcard()+"已经存在，每个身份证号只能对应一个账号！");
					IDCardText.setFocus();
					return false;
				}
			}
		}
		return true;
	}

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