import { observable, action, runInAction } from "mobx";
import * as API from "../apis/HRSyncService";

import { Modal } from "antd";
import { WeaTableNew, WeaForm } from "comsMobx";

const { TableStore } = WeaTableNew;

import { WeaLocaleProvider } from "ecCom";
import {
  getSearchFormHeight,
  checkNonstandard
} from '../util/IntegrationUtil';
import { hrSyncSettingsForm } from "./mock";
import "../style/hrSync.less";

const getLabel = WeaLocaleProvider.getLabel;

export class HrSyncStore {
  @observable hasRight=true;
  @observable rightLoading= true;
  @observable tableStore = new TableStore();
  @observable logTableStore = new TableStore();
  @observable logDetailTableStore = new TableStore();
  @observable searchForm = new WeaForm();
  @observable detailSearchForm = new WeaForm();
  @observable newForm = new WeaForm();
  @observable initialNewForm = new WeaForm();
  @observable dataSyncForm = new WeaForm();
  @observable importForm = new WeaForm();
  @observable logSearchForm = new WeaForm();
  @observable importFormConditions = [];
  @observable syncSetField = { table: null };

  @observable
  status = {
    hasNonStandard: true,
    nonStandLoading: true,
    advanceHeight: 0,
    loading: false,
    syncLoading: false,
    selectedKey: 0,
    newFormFields: [],
    dataSyncFormFields: [],
    newFormHiddenFields: [],
    logSearchFormCondition: [],
    detailSearchFormCondition: [],
    detailDialogTab: [],
    isAlertPage: false,
    showSearchAd: false,
    rightMenus: [],
    rightMenusTab2: [],
    rowSelectIds: [],
    WeaTableEdit_WebserviceParameterSet: [],
    WebserviceMethodParameterDatas: [],
    syncFieldSettingsGroupLabel: {},
    syncFieldRelationShipSummaryLabel: {},
    showImportForm: false,
    canexport: false,
    syncFlag: false,
    detailDialogVisible: false,
    errorInfo:'',
    errorTipDialogVisible: false,
    saveDisabled: false
  };

  @observable
  SyncSetTable = {
    // // 分部同步对应关系设置
    // WeaTableEdit_subSyncFieldSet: [],
    // WeaTableEdit_subSyncFieldData: [],
    // // 部门同步对应关系设置
    // WeaTableEdit_deptSyncFieldSet: [],
    // WeaTableEdit_deptSyncFieldData: [],
    // // 岗位同步对应关系设置
    // WeaTableEdit_jobSyncFieldSet: [],
    // WeaTableEdit_jobSyncFieldData: [],
    // // 人员同步对应关系设置
    // WeaTableEdit_hrmSyncFieldSet: [],
    // WeaTableEdit_hrmSyncFieldData: [],
  }

  @observable
  webserviceParamValuesData = {
    deptWebserviceParamValuesData: [],
    subWebserviceParamValuesData: [],
    jobWebserviceParamValuesData: [],
    hrmWebserviceParamValuesData: [],
  }

  @observable
  syncTable = {
    // jobSyncResultHeaderList: [],
    // jobSyncResultDataList: [],
    // jobTypeSyncResultHeaderList: [],
    // jobTypeSyncResultDataList: [],
    // deptSyncResultHeaderList: [],
    // deptSyncResultDataList: [],
    // subSyncResultHeaderList: [],
    // subSyncResultDataList: [],
    // gwSyncResultHeaderList: [],
    // gwSyncResultDataList: [],
    // hrmSyncResultHeaderList: [],
    // hrmSyncResultDataList: [],
    // hqSyncResultHeaderList: [],
    // hqSyncResultDataList: [],
    // lastLineSyncResultDataList: [],
  }

  @action cleanStore = () => {
    this.hasRight=true;
    this.rightLoading= true;
    this.setState({
      showSearchAd: false,
    })
    this.newForm = new WeaForm();
    this.initialNewForm = new WeaForm();
    this.syncTable = {
      jobSyncResultHeaderList: [],
      jobSyncResultDataList: [],
      jobTypeSyncResultHeaderList: [],
      jobTypeSyncResultDataList: [],
      deptSyncResultHeaderList: [],
      deptSyncResultDataList: [],
      subSyncResultHeaderList: [],
      subSyncResultDataList: [],
      gwSyncResultHeaderList: [],
      gwSyncResultDataList: [],
      hrmSyncResultHeaderList: [],
      hrmSyncResultDataList: [],
      hqSyncResultHeaderList: [],
      hqSyncResultDataList: [],
      lastLineSyncResultDataList: [],
    };
    this.SyncSetTable = {
      // 分部同步对应关系设置
      WeaTableEdit_subSyncFieldSet: [],
      WeaTableEdit_subSyncFieldData: [],
      // 部门同步对应关系设置
      WeaTableEdit_deptSyncFieldSet: [],
      WeaTableEdit_deptSyncFieldData: [],
      // 岗位同步对应关系设置
      WeaTableEdit_jobSyncFieldSet: [],
      WeaTableEdit_jobSyncFieldData: [],
      // 人员同步对应关系设置
      WeaTableEdit_hrmSyncFieldSet: [],
      WeaTableEdit_hrmSyncFieldData: [],
    };
    this. webserviceParamValuesData = {
      deptWebserviceParamValuesData: [],
      subWebserviceParamValuesData: [],
      jobWebserviceParamValuesData: [],
      hrmWebserviceParamValuesData: [],
    };
  }

  @action
  doInit = () => {
    //this.mock();
    checkNonstandard({num: "058"}, (result) => {
      this.setState({
        hasNonStandard: result.isOpen,
        nonStandLoading: false
      })
      if(result.isOpen) {
        this.cleanStore();
        this.getHrSyncSettingsForm();
        this.getRightMenu();
        this.getHrSyncDataSyncForm();
        this.getHrSyncDataSyncRightMenu();
        this.getHrSyncSettingsImportForm();
      }
    })
    
  };

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

  mock = () => {
    // 暂时干掉级联
    // hrSyncSettingsForm.condition[0].items.splice(6, 1)
    this.newForm.initFormFields(hrSyncSettingsForm.condition);
    this.initialNewForm.initFormFields(hrSyncSettingsForm.condition);
    this.status = {
      ...this.status,
      newFormFields: hrSyncSettingsForm.condition,
      newFormHiddenFields: hrSyncSettingsForm.hiddenFields,
      WeaTableEdit_WebserviceParameterSet: hrSyncSettingsForm.WeaTableEdit_WebserviceParameterSet,
      syncFieldSettingsGroupLabel: hrSyncSettingsForm.syncFieldSettingsGroupLabel,
      syncFieldRelationShipSummaryLabel: hrSyncSettingsForm.syncFieldRelationShipSummaryLabel
    };
    this.SyncSetTable = {
      WeaTableEdit_subSyncFieldSet: hrSyncSettingsForm.WeaTableEdit_subSyncFieldSet,
      WeaTableEdit_subSyncFieldData: hrSyncSettingsForm.WeaTableEdit_subSyncFieldData,
      WeaTableEdit_hrmSyncFieldSet: hrSyncSettingsForm.WeaTableEdit_hrmSyncFieldSet,
      WeaTableEdit_hrmSyncFieldData: hrSyncSettingsForm.WeaTableEdit_hrmSyncFieldData,
      WeaTableEdit_deptSyncFieldSet: hrSyncSettingsForm.WeaTableEdit_deptSyncFieldSet,
      WeaTableEdit_deptSyncFieldData: hrSyncSettingsForm.WeaTableEdit_deptSyncFieldData,
      WeaTableEdit_jobSyncFieldSet: hrSyncSettingsForm.WeaTableEdit_jobSyncFieldSet,
      WeaTableEdit_jobSyncFieldData: hrSyncSettingsForm.WeaTableEdit_jobSyncFieldData,
    }
    this.webserviceParamValuesData = {
      subWebserviceParamValuesData: hrSyncSettingsForm.subWebserviceParamValuesData,
      deptWebserviceParamValuesData: hrSyncSettingsForm.deptWebserviceParamValuesData,
      jobWebserviceParamValuesData: hrSyncSettingsForm.jobWebserviceParamValuesData,
      hrmWebserviceParamValuesData: hrSyncSettingsForm.hrmWebserviceParamValuesData
    }
  }

  getHrSyncSettingsForm = (params) => {
    API.getHrSyncSettingsForm(params).then(result => {
      if(result.ret == 'noright') {
        this.hasRight = false;
      }else {
        this.hasRight = true;
        // 自动同步时间保存后显示的问题
        // this.newForm = new WeaForm();
        // this.initialNewForm = new WeaForm();
        this.newForm.initFormFields(result.condition);
        this.initialNewForm.initFormFields(result.condition);
      }
      this.rightLoading = false;

      result.WeaTableEdit_deptSyncFieldSet && result.WeaTableEdit_deptSyncFieldSet.find(field => field.dataIndex == 'transql') && (result.WeaTableEdit_deptSyncFieldSet.find(field => field.dataIndex == 'transql').com[0].maxRows = 4);
      result.WeaTableEdit_hrmSyncFieldSet && result.WeaTableEdit_hrmSyncFieldSet.find(field => field.dataIndex == 'transql') && (result.WeaTableEdit_hrmSyncFieldSet.find(field => field.dataIndex == 'transql').com[0].maxRows = 4);
      result.WeaTableEdit_jobSyncFieldSet && result.WeaTableEdit_jobSyncFieldSet.find(field => field.dataIndex == 'transql') && (result.WeaTableEdit_jobSyncFieldSet.find(field => field.dataIndex == 'transql').com[0].maxRows = 4);
      result.WeaTableEdit_subSyncFieldSet && result.WeaTableEdit_subSyncFieldSet.find(field => field.dataIndex == 'transql') && (result.WeaTableEdit_subSyncFieldSet.find(field => field.dataIndex == 'transql').com[0].maxRows = 4);
      this.setState({
        newFormFields: result.condition,
        newFormHiddenFields: result.hiddenFields,
        WeaTableEdit_WebserviceParameterSet: result.WeaTableEdit_WebserviceParameterSet,
        syncFieldSettingsGroupLabel: result.syncFieldSettingsGroupLabel,
        syncFieldRelationShipSummaryLabel: result.syncFieldRelationShipSummaryLabel,
        canexport: result.canexport
      })

      this.SyncSetTable = {
        WeaTableEdit_subSyncFieldSet: result.WeaTableEdit_subSyncFieldSet,
        WeaTableEdit_subSyncFieldData: result.WeaTableEdit_subSyncFieldData,
        WeaTableEdit_hrmSyncFieldSet: result.WeaTableEdit_hrmSyncFieldSet,
        WeaTableEdit_hrmSyncFieldData: result.WeaTableEdit_hrmSyncFieldData,
        WeaTableEdit_deptSyncFieldSet: result.WeaTableEdit_deptSyncFieldSet,
        WeaTableEdit_deptSyncFieldData: result.WeaTableEdit_deptSyncFieldData,
        WeaTableEdit_jobSyncFieldSet: result.WeaTableEdit_jobSyncFieldSet,
        WeaTableEdit_jobSyncFieldData: result.WeaTableEdit_jobSyncFieldData,
      }
      this.webserviceParamValuesData = {
        deptWebserviceParamValuesData: result.deptWebserviceParamValuesData,
        subWebserviceParamValuesData: result.subWebserviceParamValuesData,
        jobWebserviceParamValuesData: result.jobWebserviceParamValuesData,
        hrmWebserviceParamValuesData: result.hrmWebserviceParamValuesData
      }
      const currentFiledName = this.newForm.getFormParams()["subcommothod"];
      // this.getHrSyncSettingsOperation({ operation: "getmethodinfo", methodid: currentFiledName }, res => {
      //   this.setWebserviceParamValuesData(0, res.WebserviceMethodParameterDatas);
      // })
    });
  };

  getRightMenu = (params) => {
    // this.setState({ loading: true });
    API.getHrSyncSettingsRightMenu(params).then(res => {
      // this.setState({ loading: false });
      this.status.rightMenus = res.rightMenus;
    })
  }

  getHrSyncDataSyncRightMenu = (params) => {
    // this.setState({ loading: true });
    API.getHrSyncDataSyncRightMenu(params).then(res => {
      // this.setState({ loading: false });
      this.status.rightMenusTab2 = res.rightMenus;
    })
  }

  getHrSyncDataSyncForm = (params) => {
    // this.setState({ loading: true });
    API.getHrSyncDataSyncForm(params).then(res => {
      // this.setState({ loading: false });
      this.dataSyncForm = new WeaForm();
      this.dataSyncForm.initFormFields(res.condition);
      this.status = {
        ...this.status,
        dataSyncFormFields: res.condition
      }
    })
  }

  getHrSyncDataSyncOperation = (params, cb) => {
    this.setState({ syncLoading: true, syncFlag: true });
    API.getHrSyncDataSyncOperation(params).then(res => {
      this.setState({ syncLoading: false, syncFlag: false });
      cb && typeof cb === "function" && cb(res);
    })
  }

  updateSyncData = res => {
    const transformArray = function (origin) {
      if (origin.length === 0) {
        return [];
      }
      return Object.keys(origin[0]).map(item => {
        return {
          title: origin[0][item],
          dataIndex: item,
          key: item,
          render: (text) => <div dangerouslySetInnerHTML={{__html: text}} />
        }
      })
    }
    this.syncTable.jobSyncResultHeaderList = transformArray(res.jobSyncResultHeaderList);
    this.syncTable.jobSyncResultDataList = res.jobSyncResultDataList;
    this.syncTable.jobTypeSyncResultHeaderList = transformArray(res.jobTypeSyncResultHeaderList);
    this.syncTable.jobTypeSyncResultDataList = res.jobTypeSyncResultDataList;
    this.syncTable.deptSyncResultHeaderList = transformArray(res.deptSyncResultHeaderList);
    this.syncTable.deptSyncResultDataList = res.deptSyncResultDataList;
    this.syncTable.subSyncResultHeaderList = transformArray(res.subSyncResultHeaderList);
    this.syncTable.subSyncResultDataList = res.subSyncResultDataList;
    this.syncTable.gwSyncResultHeaderList = transformArray(res.gwSyncResultHeaderList);
    this.syncTable.gwSyncResultDataList = res.gwSyncResultDataList;
    this.syncTable.hrmSyncResultHeaderList = transformArray(res.hrmSyncResultHeaderList);
    this.syncTable.hrmSyncResultDataList = res.hrmSyncResultDataList;
    this.syncTable.hqSyncResultHeaderList = transformArray(res.hqSyncResultHeaderList);
    this.syncTable.hqSyncResultDataList = res.hqSyncResultDataList;
    this.syncTable.lastLineSyncResultDataList = res.lastLineSyncResultDataList;
  }

  getHrSyncSettingsOperation = (params, cb) => { 
    if(!params.operation) {
      this.setState({saveDisabled: true});
    }
    API.getHrSyncSettingsOperation(params).then(res => {
      if(!params.operation) {
        this.setState({saveDisabled: false});
      }
      if (res.status == false) {
        return;
      }
      cb && cb(res);
    }).catch(res => {
      this.setState({ loading: false });
      if(!params.operation) {
        this.setState({saveDisabled: false});
      }
    })
  }

  //确认
  confirmModal = (content, func, val) => {
    //确认信息， 方法， 方法参数
    Modal.confirm({
      title: `${getLabel(131329, '信息确认')}`,
      content: content,
      okText: `${getLabel(16631, '确定')}`,
      cancelText: `${getLabel(201, '取消')}`,
      onOk: () => func(val),
    })
  };

  setWebserviceParamValuesData = (tabIndex, datas) => {
    switch (Number(tabIndex)) {
      case 0:
        this.webserviceParamValuesData.subWebserviceParamValuesData = datas;
        break;
      case 1:
        this.webserviceParamValuesData.deptWebserviceParamValuesData = datas;
        break;
      case 2:
        this.webserviceParamValuesData.jobWebserviceParamValuesData = datas;
        break;
      case 3:
        this.webserviceParamValuesData.hrmWebserviceParamValuesData = datas;
        break;
    }
  }

  setSyncSetTable = (tabIndex, datas) => {
    switch (Number(tabIndex)) {
      case 0:
        this.SyncSetTable.WeaTableEdit_subSyncFieldData = datas;
        break;
      case 1:
        this.SyncSetTable.WeaTableEdit_deptSyncFieldData = datas;
        break;
      case 2:
        this.SyncSetTable.WeaTableEdit_jobSyncFieldData = datas;
        break;
      case 3:
        this.SyncSetTable.WeaTableEdit_hrmSyncFieldData = datas;
        break;
    }
  }

  getHrSyncSettingsImportForm = params => {
    /*API.getHrSyncSettingsImportForm(params).then(res => {
      runInAction(() => {
        this.importForm = new WeaForm();
        this.importForm.setCondition(res.condition);
        this.importFormConditions = res.condition;
      })
    })*/
  }

  // 同步日志搜索
  getListConditions = () => {
    API.getListConditions().then(result => {
      if(result.status) {
        if(result.condition) {
          this.logSearchForm = new WeaForm();
          this.logSearchForm.initFormFields(result.condition);
          let advanceHeight = getSearchFormHeight(result.condition);
          this.setState({
            advanceHeight,
            logSearchFormCondition: result.condition
          });
        }
      }
    })
  }

  @action getLogList = (params) => {
    params = params || {};
    API.getLogList(params).then(result => {
      if(result.status) {
        this.logTableStore = new TableStore();
        this.logTableStore.getDatas(result.sessionkey, 1);
      }
    })
  }

  @action getLogDetailList = (params) => {
    params = params || {};
    API.getLogDetailList(params).then(result => {
      if(result.status) {
        this.logDetailTableStore = new TableStore();
        this.logDetailTableStore.getDatas(result.sessionkey, 1);
      }
    })
  }

  @action getLogDetailListConditions = (params) => {
    params = params || {};
    API.getLogDetailListConditions(params).then(result => {
      if(result.status) {
        this.detailSearchForm = new WeaForm();
        result.condition && this.detailSearchForm.initFormFields(result.condition);
        result.condition && this.setState({detailSearchFormCondition: result.condition});
      }
    })
  }

  @action getLogTab = (params) => {
    params = params || {};
    API.getLogTab(params).then(result => {
      if(result.status) {
        result.datas && this.setState({detailDialogTab: result.datas});
      }
    })
  }

  @action getRealError = (params) => {
    params = params || {};
    API.getRealError(params).then(result => {
      if(result.status) {
        result.datas && this.setState({errorInfo: result.datas});
        this.setState({errorTipDialogVisible: true});
      }
    })
  }

}
