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

import org.eclipse.swt.SWT;
import org.eclipse.swt.core.utils.WidgetUtils;
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 com.gx.obe.ColorConstants;
import com.gx.obe.components.core.vo.BusinessLicense;
import com.gx.obe.evaluation.step.inter.IStruct;
import com.gx.obe.util.utils.DateUtils;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.StringUtils;
import com.gx.obe.web.entity.Struct;
import com.gx.obe.web.service.AreaSupplierService;

/** 
 * @Description: 营业执照
 * @author chenxw 
 */
public class BusinessLicenseComposite extends Composite implements IStruct {
	
	private Label registerNumberLabel;
	private Label companyNameLabel;
	private Label companyAddressLabel;
	private Label legalRepresentativeLabel;
	private Label registerCapitalLabel;
	private Label registerInstitutionLabel;
	private Label businessScopeLabel;
	private Label busnissTermLabel;
	private Label establishDateLabel;
	private Composite isInAreaEnterprisesComposite;
	private Label isInAreaEnterprisesLabel;
	private BusinessLicense businessLicense;
	private AreaSupplierService areaSupplierService ;
	private Struct struct;
	
	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public BusinessLicenseComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(2, false));
		
		Label label = new Label(this, SWT.WRAP);
		label.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label.setText("统一社会信用代码：");
		
		registerNumberLabel = new Label(this, SWT.WRAP);
		registerNumberLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_1 = new Label(this, SWT.NONE);
		label_1.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_1.setText("公司名称：");
		
		companyNameLabel = new Label(this, SWT.WRAP);
		companyNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_3 = new Label(this, SWT.NONE);
		label_3.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_3.setText("住所：");
		
		companyAddressLabel = new Label(this, SWT.WRAP);
		companyAddressLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_4 = new Label(this, SWT.NONE);
		label_4.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_4.setText("法定代表人：");
		
		legalRepresentativeLabel = new Label(this, SWT.WRAP);
		legalRepresentativeLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_5 = new Label(this, SWT.NONE);
		label_5.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_5.setText("注册资本（万元）：");
		
		registerCapitalLabel = new Label(this, SWT.WRAP);
		registerCapitalLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_6 = new Label(this, SWT.NONE);
		label_6.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_6.setText("成立日期：");
		
		establishDateLabel = new Label(this, SWT.WRAP);
		establishDateLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_7 = new Label(this, SWT.NONE);
		label_7.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_7.setText("营业期限：");
		
		busnissTermLabel = new Label(this, SWT.WRAP);
		busnissTermLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_8 = new Label(this, SWT.NONE);
		label_8.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_8.setText("登记机关：");
		
		registerInstitutionLabel = new Label(this, SWT.WRAP);
		registerInstitutionLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_9 = new Label(this, SWT.NONE);
		label_9.setForeground(ColorConstants.FONT_BLACK_COLOR);
		label_9.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
		label_9.setText("经营范围：");
		
		businessScopeLabel = new Label(this, SWT.WRAP);
		businessScopeLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Label label_10 = new Label(this, SWT.NONE);
		label_10.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_10.setText("是否区内企业：");
		
		isInAreaEnterprisesComposite = new Composite(this, SWT.NONE);
		GridLayout gl_composite = new GridLayout(3, false);
		gl_composite.marginHeight = 0;
		gl_composite.marginWidth = 0;
		isInAreaEnterprisesComposite.setLayout(gl_composite);
		isInAreaEnterprisesComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Button yesButton = new Button(isInAreaEnterprisesComposite, SWT.RADIO);
		yesButton.setText("是");
		yesButton.setData("isInAreaEnterprises", "true");
		
		Button noButton = new Button(isInAreaEnterprisesComposite, SWT.RADIO);
		noButton.setText("否");
		noButton.setData("isInAreaEnterprises", "false");
		
		isInAreaEnterprisesLabel = new Label(isInAreaEnterprisesComposite, SWT.NONE);
		
	}
	
	/** 
	 * @Description: 刷新
	 * @author chenxw
	 * @param struct 
	 */
	public void refresh(Struct struct) {
		this.struct = struct;
		this.businessLicense = struct.getBusinessLicense();
		
		registerNumberLabel.setText(ObjectUtils.getObjString(businessLicense.getRegisterNumber()));
		companyNameLabel.setText(ObjectUtils.getObjString(businessLicense.getCompanyName()));
		companyAddressLabel.setText(ObjectUtils.getObjString(businessLicense.getCompanyAddress()));
		legalRepresentativeLabel.setText(ObjectUtils.getObjString(businessLicense.getLegalRepresentative()));
		establishDateLabel.setText(ObjectUtils.getObjString(DateUtils.getFormat(businessLicense.getEstablishDate(), "yyyy年MM月dd日")));
		String busnissTermStartDate = DateUtils.getFormat(businessLicense.getBusnissTermStartDate(), "yyyy年MM月dd日");
		String busnissTermEndDate = DateUtils.getFormat(businessLicense.getBusnissTermEndDate(), "yyyy年MM月dd日");
		if (StringUtils.isNotEmpty(busnissTermStartDate) && StringUtils.isNotEmpty(busnissTermEndDate)) {
			busnissTermLabel.setText(busnissTermStartDate + "至" + busnissTermEndDate);
		}
		registerCapitalLabel.setText(ObjectUtils.getObjString(businessLicense.getRegisterCapital()));
		registerInstitutionLabel.setText(ObjectUtils.getObjString(businessLicense.getRegisterInstitution()));
		businessScopeLabel.setText(ObjectUtils.getObjString(businessLicense.getBusinessScope()));
		
		WidgetUtils.setRadioChecked(isInAreaEnterprisesComposite, businessLicense.isInAreaEnterprises() ? "true" : "false", "isInAreaEnterprises");
		if(null == areaSupplierService){
			areaSupplierService = new AreaSupplierService();
		}
		if (areaSupplierService.hasSupplier(ObjectUtils.getObjString(businessLicense.getCompanyName()), null)) {
			isInAreaEnterprisesLabel.setText("（在区内企业名单中）");
		} else {
			isInAreaEnterprisesLabel.setText("（不在区内企业名单中）");
		}
		
	}
	
	/** 
	 * @Description: 设置是否编辑
	 * @author chenxw
	 * @param isEdit 
	 */
	public void setEdit(boolean isEdit) {
		for (Control control : isInAreaEnterprisesComposite.getChildren()) {
			if (control instanceof Button) {
				control.setEnabled(isEdit);
			}
		}
	}
	
	/** 
	 * @Description: 刷新编辑数据
	 * @author chenxw 
	 */
	public void refreshEditData() {
		businessLicense.setInAreaEnterprises("true".equalsIgnoreCase(ObjectUtils.getObjString(WidgetUtils.getRadioVal(isInAreaEnterprisesComposite, "isInAreaEnterprises"))));
		struct.setBusinessLicense(businessLicense);
	}
	
	/** 
	 * @Description: 获取标题名称
	 * @author chenxw
	 * @return 
	 */
	public String getTopName() {
		return ObjectUtils.getObjString(businessLicense.getCompanyName());
	}
	
	protected void checkSubclass() {}
	
}
