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.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.Constants;
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.json.JsonUtil;
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.expert.Expert;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.gx.obe.web.service.TenderProjectService;
import com.gx.obe.web.service.UserRoleService;
import com.gx.obe.web.service.UserService;
import com.gx.obe.web.service.expert.ExpertService;
import com.gx.obe.config.utils.InputStreamUtil;
import com.gx.utils.ActionLogUtils;
import com.gx.utils.NowTimeUtils;
import org.eclipse.swt.core.utils.WidgetUtils;
import com.swtdesigner.ResourceManager;

/**
 * @Description:
 * @author guoyr
 */
public class EditExpertDialog extends EDialog {
	
	private ImageResolver imageResolver = new ImageResolver();
	
	private final UserService userService = new UserService();
	private final ExpertService expertService = new ExpertService();
	private final UserRoleService userRoleService = new UserRoleService();
	private final TenderProjectService tenderProjectService = new TenderProjectService();
	
//	private final Image defaultAvatorImage = ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/male_96.png");
//	private final Image defaultAvatorImage = ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/idCard.png");
	
	private final String DISPLAY_PASSWORD = "00000000";
	private final String DEFAULT_PASSWORD = PropertiesUtils.getProperty(ConfigConstants.EXPERT_DEFAULT_PASSWORD, "123456");
	
	private TenderProjectEntity tenderProjectEntity;
	private Expert expert;
	
	private Composite compContent;
	private Text userNameText;
	private ImageButton userAvatorLabel;
	private Text userAccountText;
	private Text passwordText;
	private Text passwordAgainText;
	private Button initialPasswordButton;
	private Composite genderComposite;
	private Text IDCardText;
	private Text phoneText;
	private Text professionalTitleText;
	private Text specialtyText;
	private Text areaCodeText;
	private Text companyText;
	private Composite expertTypeComposite;
	private Button busButton;
	private Button tecButton;
	private Button priceButton;
	private String avatar;
	private boolean isInEvaluationProgress;
	private boolean isAdd;
	private boolean changeAvatorImage = false;
	private ELink link;
	private ELink createAccountLink;
	{
		LogUtils.logClass(EditExpertDialog.class);
	}
	
	public EditExpertDialog(Shell parentShell, TenderProjectEntity tenderProjectEntity, Expert expert) {
		super(parentShell);
		isAdd = expert == null;
		if (isAdd) {
			this.setTitle(Messages.Button_add + Messages.Title_expertInfo);
		} else {
			this.setTitle(Messages.Button_modify + Messages.Title_expertInfo);
		}
		this.tenderProjectEntity = tenderProjectEntity;
		this.expert = expert;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite content = (Composite) super.createDialogArea(parent);
		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);
		content.setBackgroundMode(SWT.INHERIT_FORCE);
		
		Composite compAll = new Composite(content, SWT.NONE);
		GridLayout gl_compAll = new GridLayout(1, false);
		gl_compAll.marginHeight = 0;
		gl_compAll.marginWidth = 0;
		compAll.setLayout(gl_compAll);
		compAll.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
		
		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(passwordAgainText, "确认密码不能为空！"));
		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);
		GridData gd_specialtyText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_specialtyText.widthHint = 300;
		specialtyText.setLayoutData(gd_specialtyText);
//		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);
		GridData gd_companyText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		gd_companyText.widthHint = 400;
		companyText.setLayoutData(gd_companyText);
		companyText.setTextLimit(100);
		
		CLabel label_6 = new CLabel(compContent, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_6.setText(Messages.ExpertType + "：");
		
		expertTypeComposite = new Composite(compContent, SWT.NONE);
		expertTypeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1));
		GridLayout gl_expertTypeComposite = new GridLayout(2, false);
		gl_expertTypeComposite.marginHeight = 0;
		expertTypeComposite.setLayout(gl_expertTypeComposite);
		
		Button buttonOPerator = new Button(expertTypeComposite, SWT.RADIO);
		buttonOPerator.setData(ExpertEnum.BIDDING);
		buttonOPerator.setText(Messages.BiddingLeader);
		
		Button buttonExpert = new Button(expertTypeComposite, SWT.RADIO);
		buttonExpert.setSelection(true);
		buttonExpert.setData(ExpertEnum.EXPERT);
		buttonExpert.setText(Messages.Expert);
		
		CLabel label_8 = new CLabel(compContent, SWT.NONE);
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_8.setText(Messages.SpecialCategory + "：");
		
		Composite typeComposite = new Composite(compContent, SWT.NONE);
		GridLayout gl_typecomposite = new GridLayout(3, false);
		gl_typecomposite.marginHeight = 0;
		gl_typecomposite.marginWidth = 0;
		typeComposite.setLayout(gl_typecomposite);
		typeComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));
		
		busButton = new Button(typeComposite, SWT.CHECK);
		busButton.setText(ExpertEnum.getEvalBidTypeCN(ExpertEnum.EXPERT_BUS));
		
		tecButton = new Button(typeComposite, SWT.CHECK);
		tecButton.setText(ExpertEnum.getEvalBidTypeCN(ExpertEnum.EXPERT_TEC));
		
		priceButton = new Button(typeComposite, SWT.CHECK);
		priceButton.setText(ExpertEnum.getEvalBidTypeCN(ExpertEnum.EXPERT_PRICE));
		
		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));
		
		EButton saveUserBtn = new EButton(operateComposite, SWT.NONE);
		saveUserBtn.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				saveUserAction();
			}
		});
		GridData gd_saveUserBtn = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_saveUserBtn.widthHint = 90;
		saveUserBtn.setLayoutData(gd_saveUserBtn);
		saveUserBtn.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/save.png"));
		saveUserBtn.setText(Messages.Button_save);
		
		EButton cancel_btn = new EButton(operateComposite, SWT.NONE);
		cancel_btn.addMouseListener(new MouseAdapter() {
			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.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/close.png"));
		cancel_btn.setText(Messages.Button_close);
		
		init();
		
		return content;
	}
	
	/**
	 * @Description: 初始化界面
	 */
	private void init() {
		isInEvaluationProgress = tenderProjectService.isInProgress(tenderProjectEntity.getId(), null);
		if (isInEvaluationProgress) {
			busButton.setEnabled(false);
			tecButton.setEnabled(false);
			priceButton.setEnabled(false);
		}
		
		refreshForm();
		
		getShell().pack();
		
		SwtUtils.setCenter(getShell());
	}
	
	/**
	 * @Description: 刷新表单
	 * @author guoyr
	 */
	private void refreshForm() {
		if (isAdd) {
			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);
				}
			});
			busButton.setSelection(true);
			tecButton.setSelection(true);
			priceButton.setSelection(true);
			return;
		}
		
		AuthUser user = expert.getUser();
		if (user == null) {
			return;
		}
		userNameText.setEditable(false);
		userAccountText.setEditable(false);
		WidgetUtils.setControlVisible(link, false);
		WidgetUtils.setControlVisible(createAccountLink, false);
		WidgetUtils.setControlVisible(initialPasswordButton, false);
		
		userNameText.setText(ObjectUtils.getObjString(user.getUserName()));
		
		avatar = user.getAvatar();
		userAvatorLabel.setImage(imageResolver.getImage(Base64.decode(avatar)), 102, 126);
		
		userAccountText.setText(ObjectUtils.getObjString(user.getUserAccount()));
		
		initialPasswordButton.setSelection(CommonEnum.YES.equals(user.getDefaultPassword()));
		if(initialPasswordButton.getSelection()) {
			passwordText.setText(DEFAULT_PASSWORD);
			passwordAgainText.setText(DEFAULT_PASSWORD);
		}else {
			passwordText.setText(DISPLAY_PASSWORD);
			passwordAgainText.setText(DISPLAY_PASSWORD);
		}
		passwordText.setEnabled(false);
		passwordAgainText.setEnabled(false);
		WidgetUtils.setRadioChecked(genderComposite, user.getGender());
		
		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()));
		
		WidgetUtils.setRadioChecked(expertTypeComposite, expert.getExpertType());
		
		int evalBidType = expert.getEvalBidTypeNotNull();
		busButton.setSelection((evalBidType & ExpertEnum.EXPERT_BUS) != 0);
		tecButton.setSelection((evalBidType & ExpertEnum.EXPERT_TEC) != 0);
		priceButton.setSelection((evalBidType & ExpertEnum.EXPERT_PRICE) != 0);
	}
	
	/**
	 * @Description: 产生账号
	 * @author guoyr
	 */
	private void createAccountAction() {
		if (createAccountLink.getEnabled() && userNameText.getText().trim().length() > 1) {
			userAccountText.setText(ObjectUtils.getObjString(SpellHelper.getEname(userNameText.getText().trim())));
		}
	}
	/**
	 * @Description: 查询用户
	 * @author guoyr
	 */
	private void queryUserAction() {
		if(!link.getEnabled())return;
		String userName = userNameText.getText().trim();
		if (StringUtils.isEmpty(userName)) {
			return;
		}
		List<AuthUser> userList = userService.getUserListByUserName(null, userName);
		if (CollectionUtils.isNull(userList)) {
			return;
		}
		
		String userId = initUserId();
		
		SelectUserCommonDialog selectUserDialog = new SelectUserCommonDialog(getShell(), userId, userList);
		selectUserDialog.open();
		AuthUser selectUser = selectUserDialog.getSelectUser();
		if (selectUser == null) {
			return;
		}
		
		// 待优化
		List<Expert> expertList = expertService.getExpertList(tenderProjectEntity.getId(), ExpertEnum.ALL);
		if(null != expertList) {
			for(Expert expert : expertList) {
				if(expert.getUserId().contains(selectUser.getId())) {
					MessageDialog.openError(getShell(), "该用户已经存在！");
					return;
				}
			}
		}
		initExpert();
		
		expert.setUser(selectUser);
		expert.setUserId(selectUser.getId());
		
		ActionLogUtils.addServerLog(tenderProjectEntity.getId(), "通过选择已有用户添加专家", JsonUtil.jsonObjectToStr(selectUser));
		if (isAdd) {
			expert = expertService.saveOrUpdateReturn(expert);
		} else {
			expertService.updateAssignProperty(expert, new String[] { "userId" });
		}
		
		if (null != this.getCallBack()) {
			this.getCallBack().callBackObject(expert);
		}
		getShell().close();
		
	}
	
	/**
	 * @Description: 上传头像
	 * @author guoyr
	 */
	private void uploadUserAvatarAction() {
		String imagePath = FileDialogUtils.getFileNames(SWT.SINGLE, "选择头像", new String[] { "*.png;*.jpg", "*.png", "*.jpg" });
		if (StringUtils.isEmpty(imagePath)) {
			return;
		}
		File imageFile = new File(imagePath);
		if(imageFile.length() > 1024000*1) {
			MessageDialog.openWarning(getShell(), "请上传1M以内的头像！");
			return;
		}
		Image menuImage = imageResolver.loadImage(imageFile, 102, 126);
		if (menuImage == null) {
			return;
		}
		FileInputStream in = null;
		try {
			in = new FileInputStream(imageFile);
			userAvatorLabel.setImage(menuImage, 102, 126);
			avatar = Base64.encode(InputStreamUtil.inputStreamTOByte(in));
			changeAvatorImage = true;
		} catch (FileNotFoundException e) {} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {}
			}
		}
	}
	
	/**
	 * @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 chenxw
	 */
	private void saveUserAction() {
		if (!ValidateUtils.checkInput(compContent)) {
			return;
		}
		
		String password = passwordText.getText().trim();
		String account = userAccountText.getText().trim();
		if(isAdd) {
			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;
			}
		}
		
		
		String userId = initUserId();
		
		if (userService.valAccountAndIdCard(account, userId, null) > 0) {
			MessageDialog.openWarning(getShell(), account + Messages.Tips_AlreadyExistsPleaseRetype);
			userAccountText.setFocus();
			return;
		}
		
		String IDCard = IDCardText.getText().trim();
		
		if (userService.valAccountAndIdCard(null, userId, IDCard) > 0) {
			MessageDialog.openWarning(getShell(), IDCard + "已经存在，每个身份证号只能对应一个账号！");
			IDCardText.setFocus();
			return;
		}
		
		initExpert();
		
		AuthUser user = expert.getUser();
		if (user == null) {
			user = new AuthUser();
			user.setUserType(UserEnum.EXPERT);
			user.setUseStatus(CommonEnum.REAL_STATUS);
			user.setCreateTime(NowTimeUtils.getNowDate());
			expert.setUser(user);
		}
		
		user.setUserName(userNameText.getText().trim());
		user.setAvatar(avatar);
		user.setUserAccount(account);
		
		user.setUserPassword(MD5Utils.getMD5String(password));
		user.setDefaultPassword(initialPasswordButton.getSelection() ? CommonEnum.YES : CommonEnum.NO);
		user.setGender(ObjectUtils.getObjString(WidgetUtils.getRadioVal(genderComposite)));
		user.setUserIdcard(IDCard);
		user.setPhone(phoneText.getText().trim());
		user.setProfessionalTitle(professionalTitleText.getText().trim());
		user.setSpecialty(specialtyText.getText().trim());
		user.setAreaCode(areaCodeText.getText().trim());
		user.setCompany(companyText.getText().trim());
		
		expert.setExpertType(ObjectUtils.getObjString(WidgetUtils.getRadioVal(expertTypeComposite)));
		
		int busType = busButton.getSelection() ? ExpertEnum.EXPERT_BUS : 0;
		int tecType = tecButton.getSelection() ? ExpertEnum.EXPERT_TEC : 0;
		int priceType = priceButton.getSelection() ? ExpertEnum.EXPERT_PRICE : 0;
		expert.setEvalBidType(busType | tecType | priceType);
		
		if (isAdd) {
			user.setSource(UserEnum.USER_SOURCE_LOCAL);
			ActionLogUtils.addServerLog(tenderProjectEntity.getId(), "新增专家", JsonUtil.jsonObjectToStr(user)+JsonUtil.jsonObjectToStr(expert));
			user = userService.saveOrUpdateReturn(user);
			expert.setUserId(user.getId());
			expert = expertService.saveOrUpdateReturn(expert);
			userRoleService.initUserRole(user.getId(), UserEnum.ROLE_TYPE_EXPERT);
		} else {
			String[] updateProperties;
			if (changeAvatorImage) {
				updateProperties = new String[] { "userName", "userAccount",  "gender", "userIdcard", "phone", "professionalTitle", "specialty", "areaCode", "company", "avatar" };
			} else {
				updateProperties = new String[] { "userName", "userAccount",  "gender", "userIdcard", "phone", "professionalTitle", "specialty", "areaCode", "company" };
			}
			ActionLogUtils.addServerLog(tenderProjectEntity.getId(), "修改专家信息", JsonUtil.jsonObjectToStr(user)+JsonUtil.jsonObjectToStr(expert));
			userService.updateAssignProperty(user, updateProperties);
			expertService.updateAssignProperty(expert, new String[] { "expertType", "evalBidType" });
		}
		
		if (null != this.getCallBack()) {
			this.getCallBack().callBackObject(expert);
		}
		getShell().close();
	}
	
	private void initExpert() {
		if (isAdd) {
			expert = new Expert();
			expert.setId(UuidUtils.getUUID());
			expert.setTenderId(tenderProjectEntity.getId());
			expert.setProjectId(null != tenderProjectEntity.getParentId() ? tenderProjectEntity.getParentId() : tenderProjectEntity.getId());
			expert.setCreateUserId(Constants.USER.getId());
			expert.setCreateTime(NowTimeUtils.getNowDate());
			expert.setIsLeader(ExpertEnum.LEADER_NO);
			expert.setVotes(0);
			expert.setVotesNum(0);
		}
	}
	
	/**
	 * @Description: 取消
	 * @author chenxw
	 */
	private void cancelAction() {
		getShell().close();
	}
	
	/**
	 * @Description:
	 * @author chenxw
	 * @return
	 */
	private String initUserId() {
		return isAdd ? null : expert.getUserId();
	}
	
	@Override
	protected Point getInitialSize() {
		return new Point(560, 500);
	}
	
}
