package com.gx.obe.evaluation.step.composite.struct;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.core.MessageDialog;
import org.eclipse.swt.core.widgets.ELink;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import com.gx.obe.common.icons.IconsActivator;
import com.gx.obe.components.core.vo.PerformanceDetails;
import com.gx.obe.components.core.vo.PerformanceDetails.Detail;
import com.gx.obe.evaluation.step.dialog.PerDetailsDialog;
import com.gx.obe.message.Messages;
import com.gx.obe.util.utils.ObjectUtils;
import com.swtdesigner.ResourceManager;
import com.swtdesigner.SWTResourceManager;

/**
 * @Description: 业绩明细
 * @author chenxw
 */
public class PerDetailsComposite extends Composite {
	
	private Label titleLabel;
	private Grid detailGrid;
	private ELink addLink, editLink, deleteLink;
	private PerformanceDetails performanceDetails;
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public PerDetailsComposite(Composite parent, int style) {
		super(parent, style);
		GridLayout gridLayout = new GridLayout(1, false);
		gridLayout.marginHeight = 0;
		gridLayout.marginWidth = 0;
		setLayout(gridLayout);
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(220, 220, 220));
		GridLayout gl_composite = new GridLayout(1, false);
		gl_composite.verticalSpacing = 1;
		gl_composite.marginHeight = 1;
		gl_composite.marginWidth = 1;
		composite.setLayout(gl_composite);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_composite_1 = new GridLayout(4, false);
		gl_composite_1.marginHeight = 0;
		gl_composite_1.marginWidth = 0;
		composite_1.setLayout(gl_composite_1);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		titleLabel = new Label(composite_1, SWT.NONE);
		titleLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		addLink = new ELink(composite_1, SWT.NONE);
		addLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/add.png"));
		addLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				addAction();
			}
		});
		addLink.setText(Messages.Button_addTo);
		
		editLink = new ELink(composite_1, SWT.NONE);
		editLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/edit.png"));
		editLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				editAction();
			}
		});
		editLink.setText("编辑");
		
		deleteLink = new ELink(composite_1, SWT.NONE);
		deleteLink.setImage(ResourceManager.getPluginImage(IconsActivator.PLUGIN_ID, "icons/btn/blue/delete.png"));
		deleteLink.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				deleteAction();
			}
		});
		deleteLink.setText(Messages.Button_delete);
		
		Composite detailsTableComposite = new Composite(composite, SWT.NONE);
		detailsTableComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		GridLayout gl_detailsTableComposite = new GridLayout(1, false);
		gl_detailsTableComposite.marginHeight = 0;
		gl_detailsTableComposite.marginWidth = 0;
		detailsTableComposite.setLayout(gl_detailsTableComposite);
		detailsTableComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		detailGrid = new Grid(detailsTableComposite, SWT.V_SCROLL);
		detailGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		detailGrid.setHeaderVisible(true);
		
		GridColumn gridColumn = new GridColumn(detailGrid, SWT.LEFT | SWT.WRAP);
		gridColumn.setText(Messages.Name);
		gridColumn.setWidth(150);
		
		GridColumn gridColumn_1 = new GridColumn(detailGrid, SWT.RIGHT);
		gridColumn_1.setText("数量");
		gridColumn_1.setWidth(50);
		
		GridColumn gridColumn_2 = new GridColumn(detailGrid, SWT.CENTER);
		gridColumn_2.setText("单位");
		gridColumn_2.setWidth(50);
		
	}
	
	/**
	 * @Description: 刷新
	 * @author chenxw
	 * @param titleName
	 */
	public void refresh(PerformanceDetails performanceDetails) {
		this.performanceDetails = performanceDetails;
		
		titleLabel.setText(performanceDetails.getMemo());
		refreshDetailsGrid();
	}
	
	/**
	 * @Description: 刷新明细列表
	 * @author chenxw
	 */
	private void refreshDetailsGrid() {
		detailGrid.removeAll();
		for (PerformanceDetails.Detail detail : performanceDetails.getDetailList()) {
			newDetailItem(detail);
		}
	}
	
	/**
	 * @Description: 添加
	 * @author chenxw
	 */
	private void addAction() {
		if (!addLink.getEnabled()) {
			return;
		}
		Detail detail = performanceDetails.newDetail();
		PerDetailsDialog detailsDialog = new PerDetailsDialog(getShell(), detail);
		detailsDialog.setTitle(Messages.Button_addTo);
		detailsDialog.open();
		if (!detailsDialog.isConfirm()) {
			return;
		}
		performanceDetails.getDetailList().add(detail);
		newDetailItem(detail);
	}
	
	private void newDetailItem(Detail detail) {
		GridItem gridItem = new GridItem(detailGrid, SWT.NONE);
		gridItem.setText(0, ObjectUtils.getObjString(detail.getGoodsName()));
		gridItem.setText(1, ObjectUtils.getObjString(detail.getGoodsQuantity()));
		gridItem.setText(2, ObjectUtils.getObjString(detail.getGoodsUnit()));
		gridItem.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		gridItem.setData("detail", detail);
	}
	
	/**
	 * @Description: 编辑
	 * @author chenxw
	 */
	private void editAction() {
		if (!editLink.getEnabled()) {
			return;
		}
		if (detailGrid.getSelectionCount() == 0) {
			MessageDialog.openWarning(getShell(), "请选择一条明细！");
			return;
		}
		GridItem gridItem = detailGrid.getItem(detailGrid.getSelectionIndex());
		PerformanceDetails.Detail detail = (Detail) gridItem.getData("detail");
		PerDetailsDialog detailsDialog = new PerDetailsDialog(getShell(), detail);
		detailsDialog.setTitle("编辑");
		detailsDialog.open();
		if (!detailsDialog.isConfirm()) {
			return;
		}
		gridItem.setText(0, ObjectUtils.getObjString(detail.getGoodsName()));
		gridItem.setText(1, ObjectUtils.getObjString(detail.getGoodsQuantity()));
		gridItem.setText(2, ObjectUtils.getObjString(detail.getGoodsUnit()));
	}
	
	/**
	 * @Description: 删除
	 * @author chenxw
	 */
	private void deleteAction() {
		if (!deleteLink.getEnabled()) {
			return;
		}
		if (detailGrid.getSelectionCount() == 0) {
			MessageDialog.openWarning(getShell(), "请选择一条明细！");
			return;
		}
		if (!MessageDialog.openConfirm(getShell(), "删除后将不参与得分计算，是否确定删除？")) {
			return;
		}
		int selectionIndex = detailGrid.getSelectionIndex();
		performanceDetails.getDetailList().remove(selectionIndex);
		detailGrid.remove(selectionIndex);
		if (detailGrid.getItemCount() > selectionIndex) detailGrid.setSelection(selectionIndex);
		else if (detailGrid.getItemCount() > 0) detailGrid.setSelection(selectionIndex - 1);
	}
	
	/**
	 * @Description: 设置是否编辑
	 * @author chenxw
	 * @param isEdit
	 */
	public void setEdit(boolean isEdit) {
		addLink.setEnabled(isEdit);
		editLink.setEnabled(isEdit);
		deleteLink.setEnabled(isEdit);
	}
	
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	
}
