package com.gx.obe.evaluation.step.dialog;
import java.awt.Cursor;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.nebula.widgets.grid.GridEditor;
import org.eclipse.nebula.widgets.grid.GridItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.ProgressListener;
import org.eclipse.swt.core.utils.SwtUtils;
import org.eclipse.swt.core.widgets.EDialog;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
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.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import com.gx.obe.business.http.HttpResponse;
import com.gx.obe.business.http.HttpUrl;
import com.gx.obe.business.http.HttpUtils;
import com.gx.obe.evaluation.step.entity.SupplierCreditEntity;
import com.gx.obe.http.HttpUtil;
import com.gx.obe.util.utils.ObjectUtils;
import com.gx.obe.util.utils.ScreenUtils;
import com.gx.obe.web.entity.Supplier;
import com.gx.obe.web.entity.tender.TenderProjectEntity;
import com.swtdesigner.SWTResourceManager;

import de.kupzog.ktable.KTable;
import de.kupzog.ktable.SWTX;
import de.kupzog.ktable.model.CommonKtableCModel;

import org.eclipse.swt.core.MessageDialog;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CreditChinaDialog2 extends EDialog{
	
	private TenderProjectEntity tenderProject;
	private Grid supplier_gird;
	private Table table;
	private List<Supplier> supplierList;
	private Composite composite_1;
	private GridEditor editor;
	private GridColumn gridColumn;
	private GridColumn gridColumn1;
	private GridColumn gridColumn2;
	private GridColumn gridColumn3;
	private Composite composite;
	private Map<String,String> supplierCreditMap =new HashMap<String,String>();
	private ArrayList<SupplierCreditEntity> supplierCreditEntityList=null;
	private static final String URL="https://test.powerbeijing-ec.com/jndzzb/creditChinaController.do?getZhixingList&ids=";
	
	private Object[][] contentArray;
	private KTable resultKTable;
	private CommonKtableCModel commKTableModel;

	/** 
	 * @author guoyr
	 * @param parentShell
	 * @param tenderProject
	 * @param templateType 
	 */
	public CreditChinaDialog2(Shell parentShell, TenderProjectEntity tenderProject,List<Supplier> supplierList) {
		super(parentShell);
		setTitle("失信执行人查询");
		supplierList.addAll(supplierList);
		supplierList.addAll(supplierList);
		supplierList.addAll(supplierList);
		supplierList.addAll(supplierList);
		this.tenderProject = tenderProject;
		this.supplierList=supplierList;
		
	}

	/**
	 * @Description: 
	 * @author guoyr
	 * @param parent
	 * @return 
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(new GridLayout(1,false));
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		container.setData("tenderProject", tenderProject);
		
//		// 创建 Browser 对象
//        Browser browser = new Browser(container.getShell(), SWT.NONE);
//        // 设置要自动点击的页面的 URL
//        browser.setUrl("http://example.com");
//
//        // 添加页面加载完成的事件监听器
//        browser.addProgressListener(new ProgressListener() {
//            @Override
//            public void completed(org.eclipse.swt.browser.ProgressEvent event) {
//                // 在页面加载完成后执行自动点击操作
//                browser.evaluate("document.documentElement.click();");
//            }
//
//            @Override
//            public void changed(org.eclipse.swt.browser.ProgressEvent event) {
//                // 空实现
//            }
//        });
		
		
		
		composite = new Composite(container, SWT.NONE);
		composite.setBackground(SWTResourceManager.getColor(255, 255, 255));
		//composite.setLayout(new GridLayout(1,false));
		composite.setLayout(new GridLayout(2, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		
		
//		composite_1 = new Composite(composite, SWT.NONE);
//		composite_1.setBackground(SWTResourceManager.getColor(255, 255, 255));
//		//composite_1.setLayout(new GridLayout(1,false));
//		GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
//	    gridData.horizontalSpan = 2; // 设置宽度占据两列
//	    composite_1.setLayoutData(gridData);
		
		//初始化Ktable内容
		int rowCount = supplierList.size() + 1;
		int colcount =  3;
		contentArray = new Object[rowCount][colcount];
		// 设置标题
		contentArray[0][0] = "序号";
		
		contentArray[0][1] = "企业名称";
		contentArray[0][2] = "是否为失信被执行人";
		contentArray[0][3] = "查看详情";
		//设置内容
		for(Supplier supplier : supplierList){
			//contentArray()
		}
		
		
		
		//----------------------------
		if(null != resultKTable && !resultKTable.isDisposed()){
			resultKTable.dispose();
		}
		
		resultKTable = new KTable(composite, SWTX.AUTO_SCROLL | SWT.FULL_SELECTION | SWTX.EDIT_ON_KEY);
		commKTableModel = new CommonKtableCModel(resultKTable, 1, 1, supplierList.size(), 4);
		commKTableModel.setContentRowHeight(40);// 设置行高
		commKTableModel.setColumnWidth(commKTableModel.getColCount() - 1, 70);// 设置最后一列宽为70
		commKTableModel.setColumnWidth(commKTableModel.getColCount() - 2, 200);// 设置评审意见列宽为200
		commKTableModel.setColumnMinWidth(commKTableModel.getColCount() - 2, 250);// 设置评审意见列宽为200
		commKTableModel.setColumnMinWidth(0, 150);
		
		
		
		supplier_gird = new Grid(composite, SWT.BORDER);
		supplier_gird.setHeaderVisible(true);
		supplier_gird.setItemHeaderWidth(1600);
		supplier_gird.setItemHeight(35);
		supplier_gird.isAutoHeight();
		supplier_gird.setAutoHeight(false);
		supplier_gird.setAutoWidth(false);
		GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
	    gridData.horizontalSpan = 2; // 设置宽度占据两列
		supplier_gird.setLayoutData(gridData);
		
		
		
		gridColumn = new GridColumn(supplier_gird, SWT.CENTER);
		gridColumn.setText("序号");
		gridColumn.setWidth(400);

		
		gridColumn1 = new GridColumn(supplier_gird, SWT.NONE);
		gridColumn1.setText("企业名称 ");

		
		gridColumn2 = new GridColumn(supplier_gird, SWT.CENTER);
		gridColumn2.setText("是否为失信被执行人");
		gridColumn2.setWidth(400);

		
		gridColumn3 = new GridColumn(supplier_gird, SWT.RIGHT);
		gridColumn3.setText("查看详情");
		gridColumn3.setWidth(4000);
		
		
		init();
		gridColumn.pack();
		gridColumn1.pack();
		gridColumn2.pack();
		gridColumn3.pack();
		supplier_gird.setFocus();
		supplier_gird.pack();
		supplier_gird.layout();
		container.pack();
		container.layout();
		
		return container;
	}
	
	private void init() {
		getSupplierCredit();
		refreshSupplierCredit();
	}

	private void getSupplierCredit() {
		// TODO Auto-generated method stub
		String ids = supplierList.stream().map(supplier -> {
			String orgCode = supplier.getOrgCode();
			return orgCode;
			}).collect(Collectors.joining(","));
		String url =URL+ids;
		HttpUrl httpUrl = new HttpUrl(url);
		HttpResponse httpResponse = HttpUtils.doPost(httpUrl);
		supplierCreditEntityList = new ArrayList<SupplierCreditEntity>();
		if(null != httpResponse && httpResponse.getStatus()){
			JSONArray supplierCreditListArray = httpResponse.getDataJSONArray("zhiXingSupplierList");
			Object[] supplierCreditJsonList =  supplierCreditListArray.toArray();
			if(null != supplierCreditJsonList && supplierCreditJsonList.length > 0){
				for(Object object : supplierCreditJsonList){
						SupplierCreditEntity supplierCreditEntity =new SupplierCreditEntity();
						JSONObject supplierCreditJson = (JSONObject)object;
						String creditCode = (String)supplierCreditJson.get("code");//社会统一信用代码
						String ifBreakLaw = ((String)supplierCreditJson.get("ifBreakLaw"));//是否失信被执行人(0否 1是)
						String supplierName = ((String)supplierCreditJson.get("name"));
						supplierCreditMap.put(supplierName, ifBreakLaw);
						supplierCreditEntity.setCode(creditCode);
						supplierCreditEntity.setIfBreakLaw(ifBreakLaw);
						supplierCreditEntity.setName(supplierName);
						supplierCreditEntityList.add(supplierCreditEntity);
					}
				}
			}else {
				MessageDialog.openError(getShell(), "调用失信执行人查询错误！");
				return;
			}
	}

	private void refreshSupplierCredit() {
		// TODO Auto-generated method stub
		supplier_gird.removeAll();
		if(null != supplierList && supplierList.size() > 0){
			int index = 1;
			for(Supplier supplier : supplierList){
				GridItem gridItem = new GridItem(supplier_gird,SWT.CENTER);
				setSupplierInfo(gridItem, index, supplier);
				index ++;
			}

		}
		
	}
	

	public void setSupplierInfo(GridItem gridItem, int index, Supplier supplier){
		gridItem.setText(0, index + "");
		gridItem.setText(1, "\t"+ObjectUtils.getObjString(supplier.getSupplierName()+"\t"));
		//gridItem.setText(2, "否");
		//gridItem.setText(1, "1111111111111111111111111111111111111111111111111111111111111111111111111111");
		gridItem.setExpanded(false);
		gridItem.setText(2, supplierCreditMap.get(ObjectUtils.getObjString(supplier.getSupplierName())).equals("1")?"是":"否");
		gridItem.setData("supplier",supplier);
		Button button = new Button(supplier_gird, SWT.NONE);
        button.setText("查看详情 ");
        button.setData("supplier",supplier);
        editor =new GridEditor(supplier_gird);
        editor.grabHorizontal = true;
        editor.setEditor(button, gridItem,3);
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                System.out.println("Button " +  " clicked");     
               Button button = (Button)e.getSource();
               Supplier supplier=(Supplier)button.getData("supplier");
               LookupDetailAction(supplier);
            }
        });

	}

	protected void LookupDetailAction(Supplier supplier) {
		// TODO Auto-generated method stub
		CreditChinaDetailDialog creditChinaDetailDialog = new CreditChinaDetailDialog(getShell(),supplier,supplierCreditEntityList);
		creditChinaDetailDialog.open();
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		ScreenUtils  util = new ScreenUtils();
		int screenHight  = util.getScreenHight();
		return new Point(1144,820);
	//return new Point(1000, 616);
	}

}
