import { observable } from "mobx";
import * as API from "../apis/webseal";
import { message } from "antd";
import { WeaTableNew, WeaForm } from "comsMobx";
import { WeaLocaleProvider } from "ecCom";
import {checkNonstandard, setModalInfo} from '../util/IntegrationUtil';

const { TableStore } = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;

export default class Weaseal {
  @observable hasRight=false;
  @observable rightLoading= true;
  @observable tableBaseInfo = new TableStore(); //基本信息
  @observable tableSyncLog = new TableStore(); //同步日志
  @observable searchForm = new WeaForm(); //高级查询的表单
  @observable newBaseForm = new WeaForm(); // 新建表单store

  @observable excludeTableEdit = null;
  @observable ruleTableEdit = null;
  @observable
  status = {
    nonStandLoading: true,
    hasNonStandard: true,
    selectedKey: 0,
    showSearchAd: false,
    showNewBaseFormDialog: false,
    rowSelectIds: [], //check选择行id
    newBaseFormFields: [], // 新建表单
    res: {},
    tableDatas: [],
    tableColumns: [],
    tableDatas2: [],
    tableColumns2: [],
  };

  doInit = () => {
    this.setState({
      nonStandLoading: true
    })
    checkNonstandard({num: "091"}, (result) => {
      this.setState({
        hasNonStandard: result.isOpen,
        nonStandLoading: false
      })
      if(result.isOpen) {
        this.getForm();
      }
    })
  };

  setState = (params = {}) => {
    let status = { ...this.status };
    Object.keys(params).forEach(key => {
      status[key] = params[key];
    });
    this.status = status;
  };

  getForm = params => {
    API.getForm(params).then(res => {
      if(res.status&&res.status=="false"){
        this.hasRight=false;
      }else{
        this.hasRight=true;
      }
      this.rightLoading = false;
      res.condition && res.condition[2] && res.condition[2].items && (res.condition[2].items.find(field => field.domkey[0] == 'accountType').selectLinkageDatas[7].maxRows = 4);
      this.newBaseForm.initFormFields(res.condition);

      this.status = {
        ...this.status,
        newBaseFormFields: res.condition,
        res: res,
        tableDatas: res.datas,
        tableColumns: res.columns,
        tableDatas2: res.datas2,
        tableColumns2: res.columns2,
      };
    });
  };

  save = () => {
    this.newBaseForm.validateForm().then(f=>{
      if(f.isValid){
        //自定义SQL必填验证
        let accountType = this.newBaseForm.getFormParams().accountType;
        let customsql = this.newBaseForm.getFormParams().customsql;
        if(accountType==7&&(customsql==""||customsql==null)) {
          this.newBaseForm.showError('accountType', getLabel("126462,21423","自定义SQL未填写"))
          return;
        }

        const params = this.newBaseForm.getFormParams();
        params.list = JSON.stringify(this.status.tableDatas);
        params.list2 = JSON.stringify(this.status.tableDatas2);
        delete params.description;
        API.save(params).then(res => {
          if (res.msg) {
            message.success(res.msg);
          }
        });
      }else{
        f.showErrors();
      }
    });
  };

  complete = () => {
    API.complete().then(result => {
      let data = {};
      if(result.syncResult) {
        try {
          if(typeof(result.syncResult) === "string") {
            data = JSON.parse(result.syncResult) || {};
          } else if (typeof(result.syncResult) === "object") {
            data = result.syncResult;
          }
        } catch(er) {}
      }

      if(data.status === "failure") {
        if(data.errorNode && data.noValNode) {
          setModalInfo(<div>
            {getLabel('522113','集群部署失败， 有节点异常导致不能进行部署， 请检查异常节点， 并重新进行部署。')}
            <div>{getLabel('522114','配置失败的节点为')}: {data.errorNode}</div>
            <div>{getLabel('522115','验证失败的节点为')}: {data.noValNode}</div>
          </div>);
        } else if(data.errorNode) {
          setModalInfo(<div>
            {getLabel('522113','集群部署失败， 有节点异常导致不能进行部署， 请检查异常节点， 并重新进行部署。')}
            <div>{getLabel('522114','配置失败的节点为')}: {data.errorNode}</div>
          </div>);
        } else if(data.noValNode) {
          setModalInfo(<div>
            {getLabel('522113','集群部署失败， 有节点异常导致不能进行部署， 请检查异常节点， 并重新进行部署。')}
            <div>{getLabel('522115','验证失败的节点为')}: {data.noValNode}</div>
          </div>);
        }
      } else {
        if (result.msg) {
          message.success(result.msg);
        }
      }
    });
  };
}
