package com.huigou.uasp.builder.jsbutton;

import java.util.ArrayList;
import java.util.List;

import com.google.common.base.CaseFormat;
import com.huigou.uasp.builder.ButtonTypeInter;
import com.huigou.uasp.builder.vo.ButtonAttributeVO;

import com.huigou.uasp.util.Utils;
import com.huigou.uasp.util.Constant;

public class DeleteRowButtonResult implements ButtonTypeInter {
	
	public List<String> processOutResult(StringBuffer sb,ButtonAttributeVO btnAttr){
		List <String> list=new ArrayList<String>();
		   	    
   	  //  sb.append(" //删除行数据方法").append(Constant.SYSMBOL_LINE);
		sb.append("function deleteRow").append(Utils.toUpperCaseFirstOne(btnAttr.getEntityName())).append("(){");

	//	sb.append(" var url = \"").append(btnAttr.getEntityName()).append("/deleteMdmDataRecordDetail").append(".ajax\";");
		
		if (btnAttr.getIsForm()==1){  //是表单生成器 则调用统一的添加方法
				sb.append("   var url = '/dataManage/deleteDataRecordDetail.ajax")
				  // .append(Utils.toUpperCaseFirstOne(btnAttr.getParentEntityName())).append("Detail.ajax")
				   //.append("?maiTab=").append(btnAttr.getParentEntityName())
				   //.append("&subTab=").append(btnAttr.getEntityName())
				   .append("?maiTab=").append(btnAttr.getParentEntityName())
				   .append("&subTab=").append(btnAttr.getEntityName())
				   .append("&id='+"+"$(\"#id\").val()");  
				   sb.append(";");
		} else {
		 sb.append("   var url = '/").append(btnAttr.getParentEntityName()).append("/deleteRow")
		   .append(Utils.toUpperCaseFirstOne(btnAttr.getParentEntityName())).append("Detail.ajax")
		   .append("?maiTab=").append(btnAttr.getParentEntityName())
		   .append("&subTab=").append(btnAttr.getEntityName())
		   //.append("&modelId='+"+"$(\"#modelId\").val()")
		   .append("+'&id='+"+"$(\"#id\").val()");
		   sb.append(";");
		   
		}
		   sb.append(" var rows =").append(btnAttr.getEntityName()).append("GridManager.getSelectedRows();");
		   sb.append(" var ids='';")
			 .append("if (rows){")
			 .append("	  for(var i=0;i<rows.length;i++){")
			 .append("     if (rows[i].id==\"\"){")
			 .append(btnAttr.getEntityName()).append("GridManager.deleteRow(rows[i]);")
			 .append("     } else {")
			 .append(" if (rows[i].status==1) {  Public.tip(\"").append(Constant.NOT_DELETE_ENBABLE_DATA)
			 .append("\");  return false; }")
			 .append("	    ids+=rows[i].id;")
			 .append("	    ids+=',';}")
			 .append("	  }");
		     sb.append("    if (ids!=''){")
			 .append(" 	    param={ids:ids};")
			.append(" 	  $('##submitForm').ajaxSubmit({ ")
			.append(" 	        url: web_app.name + (url),")
	        .append(" 	        param:param, ")
		    .append(" 	        success: function (data) {")
            .append("	            refreshFlag = true;")
            //避免明细表修改后删除后提示 数据已经改变，关闭后数据丢失的提示
            .append("             ").append(btnAttr.getEntityName()).append("GridManager.isDataChanged=false;")
    		.append("            reload").append(Utils.toUpperCaseFirstOne(btnAttr.getEntityName())).append("Grid();")
    	    .append("	 	        }")
            .append("	 	    });").append("}")
            .append("	    } else {")
            .append("	 	     Public.tip(\"").append(Constant.SELECT_DETAIL_DATA).append("\");")
            .append("	    } ");
		   
	/*	   //删除单行
		sb.append("  var row = UICtrl.checkSelectedRows(").append(btnAttr.getEntityName()).append("GridManager);");
		sb.append("  DataUtil.del({");
		sb.append("     action: url,");
		sb.append("     idFieldName: 'id',");
		sb.append("     gridManager: ").append(btnAttr.getEntityName()).append("GridManager,");
		sb.append("     onSuccess: reload").append(btnAttr.getEntityName()).append("Grid ");
		sb.append("  })");
		*/
		sb.append("	}");
		sb.append(Constant.SYSMBOL_LINE);
		
		  list.add(sb.toString());
		 // list.add(Integer.toString(fieldAttributes.getCurrCols()));
		  return list;
	}



}
