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

import java.util.List;

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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import com.gx.obe.evaluation.step.inter.IDwgcDetail;
import com.gx.obe.util.utils.CollectionUtils;
import com.gx.obe.util.utils.NumberFormatUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.web.entity.boq.ConstructProject;
import com.gx.obe.web.entity.boq.SupplyMaterialDetail;
import com.gx.obe.web.service.boq.SupplyMaterialDetailService;

/**
 * @Description: 招标人供应材料设备明细界面
 * @author chenxw
 */
public class SupplyMaterialDetailComposite extends Composite implements IDwgcDetail {
    
    private SupplyMaterialDetailService supplyMaterialDetailService = new SupplyMaterialDetailService();
    
    private Grid detailGrid;
    
    /**
     * @author chenxw
     * @param parent
     * @param style
     */
    public SupplyMaterialDetailComposite(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);
        GridLayout gl_composite = new GridLayout(1, false);
        gl_composite.marginHeight = 0;
        gl_composite.marginWidth = 0;
        composite.setLayout(gl_composite);
        composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        
        detailGrid = new Grid(composite, SWT.H_SCROLL | SWT.V_SCROLL);
        detailGrid.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
        detailGrid.setHeaderVisible(true);
        detailGrid.setAutoHeight(true);
        
        GridColumn gridColumn = new GridColumn(detailGrid, SWT.CENTER);
        gridColumn.setText("序号");
        gridColumn.setWidth(50);
        
        GridColumn gridColumn_1 = new GridColumn(detailGrid, SWT.NONE);
        gridColumn_1.setText("名称");
        gridColumn_1.setWidth(150);
        
        GridColumn gridColumn_2 = new GridColumn(detailGrid, SWT.NONE);
        gridColumn_2.setText("规格型号");
        gridColumn_2.setWidth(100);
        gridColumn_2.setWordWrap(true);
        
        GridColumn gridColumn_3 = new GridColumn(detailGrid, SWT.CENTER);
        gridColumn_3.setText("单位");
        gridColumn_3.setWidth(50);
        
        GridColumn gridColumn_4 = new GridColumn(detailGrid, SWT.RIGHT);
        gridColumn_4.setText("数量");
        gridColumn_4.setWidth(80);
        
        GridColumn gridColumn_5 = new GridColumn(detailGrid, SWT.RIGHT);
        gridColumn_5.setText("单价");
        gridColumn_5.setWidth(80);
        
        GridColumn gridColumn_6 = new GridColumn(detailGrid, SWT.RIGHT);
        gridColumn_6.setText("合价");
        gridColumn_6.setWidth(80);
        
        GridColumn gridColumn_7 = new GridColumn(detailGrid, SWT.NONE);
        gridColumn_7.setText("质量等级");
        gridColumn_7.setWidth(100);
        
        GridColumn gridColumn_8 = new GridColumn(detailGrid, SWT.CENTER);
        gridColumn_8.setText("供应时间");
        gridColumn_8.setWidth(100);
        
        GridColumn gridColumn_9 = new GridColumn(detailGrid, SWT.NONE);
        gridColumn_9.setText("送达地点");
        gridColumn_9.setWidth(150);
        
        GridColumn gridColumn_10 = new GridColumn(detailGrid, SWT.NONE);
        gridColumn_10.setText("备注");
        gridColumn_10.setWidth(150);
        
    }
    
    public void refreshDetail(final ConstructProject unitConstructProject) {
        detailGrid.removeAll();
        detailGrid.layout();
        detailGrid.getParent().layout();
        new Thread(new Runnable() {
            public void run() {
                final List<SupplyMaterialDetail> supplyMaterialDetailList = supplyMaterialDetailService.getSupplyMaterialDetailListByProjectId(unitConstructProject.getId());
                if (CollectionUtils.isNull(supplyMaterialDetailList)) {
                    return;
                }
                Display.getDefault().syncExec(new Runnable() {
                    public void run() {
                        if (detailGrid.isDisposed()) {
                            return;
                        }
                        for (SupplyMaterialDetail supplyMaterialDetail : supplyMaterialDetailList) {
                            GridItem gridItem = new GridItem(detailGrid, SWT.NONE);
                            gridItem.setText(0, ObjectUtils.getObjString(supplyMaterialDetail.getNumber()));
                            gridItem.setText(1, ObjectUtils.getObjString(supplyMaterialDetail.getName()));
                            gridItem.setText(2, ObjectUtils.getObjString(supplyMaterialDetail.getSpecification()));
                            gridItem.setText(3, ObjectUtils.getObjString(supplyMaterialDetail.getUnit()));
                            gridItem.setText(4, NumberFormatUtils.format(supplyMaterialDetail.getQuantity()));
                            gridItem.setText(5, ObjectUtils.getObjString(supplyMaterialDetail.getPrice()));
                            gridItem.setText(6, ObjectUtils.getObjString(supplyMaterialDetail.getTotal()));
                            gridItem.setText(7, ObjectUtils.getObjString(supplyMaterialDetail.getQualityGrade()));
                            gridItem.setText(8, ObjectUtils.getObjString(supplyMaterialDetail.getSupplyTimeString()));
                            gridItem.setText(9, ObjectUtils.getObjString(supplyMaterialDetail.getServiceAddress()));
                            gridItem.setText(10, ObjectUtils.getObjString(supplyMaterialDetail.getRemark()));
                        }
                        detailGrid.layout();
                        detailGrid.getParent().layout();
                    }
                });
            }
        }).start();
    }
    
    public Composite getComposite() {
        return this;
    }
    
    @Override
    protected void checkSubclass() {}
}
