import { observable, action, toJS } from "mobx";
import { WeaTableNew, WeaForm,WeaLogView } from "comsMobx";
import { WeaCheckbox, WeaSelect, WeaHelpfulTip, WeaLocaleProvider } from 'ecCom'
import { Modal,Button,message } from 'antd';
import { doFormReset } from "../util/index.js";
import * as API from "../apis/WorkflowFlowService";
import isFunction from 'lodash/isFunction';
const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
const { LogStore } = WeaLogView;

export default class RegisterESBStore {
  constructor() {
	this.setState = this.setState.bind(this);
	this.setState1 = this.setState1.bind(this);
	this.setRuleRelationship = this.setRuleRelationship.bind(this);
	this.setFormid = this.setFormid.bind(this);
	this.setESBid = this.setESBid.bind(this);
	this.setESBname = this.setESBname.bind(this);
	this.setVersion = this.setVersion.bind(this);
	this.setOperateSrc = this.setOperateSrc.bind(this);
	this.setCheckList = this.setCheckList.bind(this);
	this.addDatas = this.addDatas.bind(this);
	this.delRows = this.delRows.bind(this);
	this.setIsbill = this.setIsbill.bind(this);
	this.setWorkflowInfo = this.setWorkflowInfo.bind(this);
  }
	
  @observable tableStore1 = new TableStore();
  @observable sessionkey1 = '';
  @observable newRegisterESBForm = new WeaForm();
  @observable checkList = [];
  //规则关系  and或者or
  @observable ruleRelationship = 1;
  //流程表单id
  @observable formid= 0;
  @observable isbill= 0;
  //esbAction id
  @observable esbactionid=0;
  //esb服务id
  @observable esbid= "";
  //esb服务名称
  @observable esbname="";
  //esb版本
  @observable version= 0;
  //当前操作
  @observable operateSrc = "";
  //流程workflowid
  @observable workflowid=0;
  //流程节点id
  @observable nodeid=0;
  //流程出口id
  @observable nodelinkid=0;
  //是否节点后附加操作
  @observable ispreoperator=-1;
  @observable WeaTableEdit_returnValueData= [];
  @observable WeaTableEdit_esbParamData= [];

  @observable
  status = {
	interfaceRegisterESBFormFields: [],
	showRegisterESBDialog: false,
	WeaTableEdit_esbParamSet: [],
	WeaTableEdit_returnValueSet: [],
  WeaTableEdit_returnControlData: [],
  rulesDialogVisible: false,
  };
  
  @action
  resetDatas( params = {}) {
	this.status = {
	  showRegisterESBDialog: false,
	  WeaTableEdit_returnControlData: [],
	};
	this.WeaTableEdit_esbParamData=[];
	this.WeaTableEdit_returnValueData=[];
	this.checkList = [];
	this.ruleRelationship = 1;
	this.formid= 0;
	this.isbill= 0;
	this.esbactionid=0;
	this.esbid= "";
	this.esbname="";
	this.version= 0;
	this.operateSrc = "";
	this.workflowid=0;
	this.nodeid=0;
	this.nodelinkid=0;
	this.ispreoperator=-1;
	this.newRegisterESBForm.reset();
  }
  
  setESBActionid(esbactionid){
	  this.esbactionid = esbactionid;
  }
  
  //esb名称
  setESBname(esbname){
	  this.esbname = esbname;
  }
  
  //规则关系
  setRuleRelationship(ruleRelationship){
    this.ruleRelationship = ruleRelationship;
  }
  
  //流程表单id
  setFormid(formid){
	this.formid = formid;
  }
  
  setIsbill(isbill){
	this.isbill = isbill;
  }
  
  //ESB服务id
  setESBid(esbid){
	this.esbid = esbid;
  }
  
  //存流程相关数据
  setWorkflowInfo(workflowinfo){
	 this.operateSrc=workflowinfo.src;
	 this.esbactionid=workflowinfo.actionid;
	 this.workflowid=workflowinfo.workflowid;
	 this.nodeid=workflowinfo.nodeid;
	 this.nodelinkid=workflowinfo.nodelinkid;
	 this.ispreoperator=workflowinfo.ispreoperator;
  }
  
  //操作类型  新增或编辑
  setOperateSrc(setOperateSrc){
	  this.operateSrc = setOperateSrc;
  }
  
  //ESB服务版本
  setVersion(version){
	this.version = version;
  }
  
  setState = (params = {}) => {
      let status = {...this.status};
      Object.keys(params).forEach(key => {
          status[key] = params[key];
      });
      this.status = status;
      this.getInterfaceRegisterESBAddForm();
  };
  
  setState1 = (params = {}) => {
      let status = {...this.status};
      Object.keys(params).forEach(key => {
          status[key] = params[key];
      });
      this.status = status;
  };
  
  setDatas(datas) {
      this.WeaTableEdit_returnValueData = datas;
  }
  
  setESBDatas(datas) {
      this.WeaTableEdit_esbParamData = datas;
  }
  
  setCheckList(list) {
      this.checkList = list;
  }
  
  // 接口注册ESB表单
  getInterfaceRegisterESBAddForm = () => {
    API.getInterfaceRegisterESBAddForm({esbactionid:this.esbactionid,operateSrc:this.operateSrc,workflowid:this.workflowid}).then(result => {
    	let returnValueSet = result.WeaTableEdit_returnValueSet;
        returnValueSet.WeaTableEdit[3].com[0].type='custom';
        returnValueSet.WeaTableEdit[3].com[0].render=(text, record, index, onEdit)=>{
        	return <div style={{width:'100%',paddingTop:'5px'}}>
        	<div className="ant-select-selection ant-select-selection--multiple" style={{float: 'left', width: '75%', borderRight: 'none'}}><a>{text}</a></div>
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@o4lf18`} icon="search" 
          style={{height: 28, borderLeft:'none'}}
        	onClick={() => {
        		this.getESBReturnParamsBrowser(index);
        		}
        	}
        	/>
    		</div>;
        }
      this.status = {
		...this.status,
		interfaceRegisterESBFormFields: result.condition,
		WeaTableEdit_esbParamSet: result.WeaTableEdit_esbParamSet,
		WeaTableEdit_returnValueSet: returnValueSet,
      };
      this.sessionkey1 = result.sessionkey;
	  this.tableStore1.getDatas(result.sessionkey);
	  this.setFormid(result.hiddenFields.formid);
  	  this.setIsbill(result.hiddenFields.isbill);
	  if("edit"==this.operateSrc){
		let errmsg = result.errmsg;
		let title="";
		if("1"==errmsg){
		  title=getLabel(385545,"您配置的服务已被删除，请重新配置");
		}
		if("2"==errmsg){
		  title=getLabel(385556,"ESB服务已更新，需重新配置");
		}
	    if(""!=title){
	    	Modal.warning({
                title: getLabel(15172, "系统提示"),
                content: title
            });
	    	this.setDatas([]);
	    	this.setESBDatas([]);
	    }else{
	    	this.setESBname(result.hiddenFields.esbname);
	    	this.setESBid(result.hiddenFields.esbid);
	    	this.setVersion(result.hiddenFields.version);
	    	this.setRuleRelationship(result.hiddenFields.ruleRelationship);
	    	this.WeaTableEdit_returnValueData = result.WeaTableEdit_returnValueData;
	    	this.getWeaTableEdit_esbParamData();
	    }
	  }
      this.newRegisterESBForm.initFormFields(result.condition);
    });
  }
  
  //根据传入的esbid和版本获取对应的参数列
  getWeaTableEdit_esbParamData = () => {
	API.getESBPublishParam({esbid:this.esbid,version:this.version,formid:this.formid,isbill:this.isbill,esbactionid:this.esbactionid}).then(result => {
		this.WeaTableEdit_esbParamData = result.WeaTableEdit_esbParamData;
		this.getESBReturnContralDatas();
	});
  }
  
  addDatas(type) {
      let data = {
                  id: -2,
                  esbreturnassignfieldname: "",
                  esbreturnassignfieldtype: "",
                  esbreturnassignoperation: "=",
                  esbreturnassignreturnparamname:"",
              };
      this.WeaTableEdit_returnValueData.push(data);
  }
  
  delRows() {
      this.WeaTableEdit_returnValueData = this.WeaTableEdit_returnValueData.filter((o, i) => {
          if (this.checkList.indexOf(i) === -1)
              return true
          if (o.id > 0) {
              this.deleteIds += "," + (o.id - 100);
          }

      })
      this.checkList = [];
  }
  
  getESBReturnParamsBrowser(index){
	  let dialog = new window.top.Dialog();
      dialog.currentWindow = window;
	  var url = "/workflow/action/ESBReturnParamsBrowser.jsp?esbid="+this.esbid;
	    dialog.Title = getLabel(18214,"请选择");
	    dialog.Width = 900;
	    dialog.Height = 568;
	    dialog.Drag = true;
	    dialog.maxiumnable = true;
	    dialog.URL = url;
	    dialog.callback = (data) => {
	    	 dialog.close();
	      let id = data.id;
	      let returnValueData = toJS(this.WeaTableEdit_returnValueData);
	      returnValueData[index].esbreturnassignreturnparamname=id;
	      this.setDatas(returnValueData);
	    }
	    dialog.show();
  }
  
  //编辑
  onEdit(ruleid){
	  let dialog = new window.top.Dialog();
      dialog.currentWindow = window;
	  var url = "/workflow/action/ESBShowConditionContent.jsp?ruleid="+ruleid+"&esbid="+this.esbid;
	    dialog.Title = getLabel(84552,"编辑规则");
	    dialog.Width = 900;
	    dialog.Height = 568;
	    dialog.Drag = true;
	    dialog.maxiumnable = true;
	    dialog.URL = url;
	    dialog.callback = (data) => {
          this.getESBReturnContralDatas();
	    }
	    dialog.show();
  }
  
  //删除
  onDelete(ruleid){
	API.deleteESBReturnContralData({ruleid:ruleid,esbid:this.esbid,version:this.version,actionid:this.esbactionid}).then(result => {
	  this.sessionkey1 = result.sessionkey;
	  this.tableStore1.getDatas(result.sessionkey);
	})
  }
  
  /**
   * 打开浏览按钮E8
   */
  onShowBrowser4port() {
      let dialog = new window.top.Dialog();
      dialog.currentWindow = window;
      var url = "/workflow/action/ESBShowConditionContent.jsp?isnew=1&setid=" + this.esbactionid +
          "&esbid=" + this.esbid + "&version="+ this.version;
      dialog.Title = getLabel(84553,"新增规则");
      dialog.Width = 900;
      dialog.Height = 568;
      dialog.Drag = true;
      dialog.maxiumnable = true;
      dialog.URL = url;
      dialog.callback = (data) => {
         this.getESBReturnContralDatas();
      }
      dialog.show();
      
  }
  
  //根据传入的esbid和版本以及actionid获取规则数据
  getESBReturnContralDatas = () => {
	API.getESBReturnContralDatas({esbid:this.esbid,version:this.version,actionid:this.esbactionid}).then(result => {
		this.sessionkey1 = result.sessionkey;
		this.tableStore1.getDatas(result.sessionkey);
    });
  }
  
  //增删改数据
  getInterfaceRegisterESBOperation= (params) => {
	  return API.getInterfaceRegisterESBOperation(params);
  }
  
  //获取下拉框
  getlinkageSelect = (params, callback) => {
    API.getlinkageSelectForESB(params).then(result => {
	  isFunction(callback)&& callback(result.linkageSelectForESB);
    });
  }
}
