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

import java.math.BigDecimal;

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.listener.NumberModifyListener;
import org.eclipse.swt.core.widgets.EButton;
import org.eclipse.swt.core.widgets.EDialog;
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.components.core.vo.PerformanceDetails;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.ObjectUtils;

/** 
 * @Description: 业绩明细编辑界面
 * @author chenxw 
 */
public class PerDetailsDialog extends EDialog {
	private Text nameText, numberText, unitText;
	private final PerformanceDetails.Detail detail;
	private boolean isConfirm = false;
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public PerDetailsDialog(Shell parentShell, PerformanceDetails.Detail detail) {
		super(parentShell);
		this.detail = detail;
	}
	
	/**
	 * 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.marginHeight = 0;
		gl_container.marginWidth = 0;
		container.setLayout(gl_container);
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setLayout(new GridLayout(1, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		GridLayout gl_composite_1 = new GridLayout(2, false);
		gl_composite_1.marginHeight = 20;
		gl_composite_1.marginWidth = 20;
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Label label = new Label(composite_1, SWT.NONE);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label.setText("名称：");
		
		nameText = new Text(composite_1, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		nameText.setTextLimit(10);
		
		Label label_1 = new Label(composite_1, SWT.NONE);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_1.setText("数量：");
		
		numberText = new Text(composite_1, SWT.BORDER | SWT.RIGHT);
		numberText.addModifyListener(new NumberModifyListener(BigDecimal.class, "0", "0"));
		numberText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		numberText.setTextLimit(10);
		
		Label label_2 = new Label(composite_1, SWT.NONE);
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_2.setText("单位：");
		
		unitText = new Text(composite_1, SWT.BORDER);
		unitText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		unitText.setTextLimit(10);
		
		Composite composite_2 = new Composite(composite, SWT.NONE);
		composite_2.setLayout(new GridLayout(1, false));
		composite_2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		EButton button = new EButton(composite_2, SWT.NONE);
		button.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				confirmAction();
			}
		});
		GridData gd_button = new GridData(SWT.RIGHT, SWT.CENTER, true, false, 1, 1);
		gd_button.widthHint = 70;
		button.setLayoutData(gd_button);
		button.setText(Messages.Button_confirm);
		
		init();
		return container;
	}
	
	/**
	 * @Description: 初始化
	 * @author chenxw 
	 */
	private void init() {
		if (null == detail) {
			return;
		}
		nameText.setText(ObjectUtils.getObjString(detail.getGoodsName()));
		numberText.setText(ObjectUtils.getObjString(detail.getGoodsQuantity()));
		unitText.setText(ObjectUtils.getObjString(detail.getGoodsUnit()));
	}
	
	/**
	 * @Description: 确认事件
	 * @author chenxw 
	 */
	private void confirmAction() {
		if (null == detail) {
			return;
		}
		detail.setGoodsName(nameText.getText().trim());
		detail.setGoodsQuantity(ObjectUtils.getObjBigDecimal(numberText.getText().trim()));
		detail.setGoodsUnit(unitText.getText().trim());
		isConfirm = true;
		close();
	}
	
	public boolean isConfirm() {
		return isConfirm;
	}
	
	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(300, 200);
	}
}
