import {action, observable} from "mobx";
import * as API from "../apis/AuthenticationSourceService";
import {message} from "antd";
import {WeaForm, WeaLogView, WeaTableNew} from "comsMobx";
import {getSearchFormHeight} from '../util/IntegrationUtil';

const {TableStore} = WeaTableNew;
const {LogStore} = WeaLogView;

export class AuthenticationSourceStore {
  @observable hasRight = true;
  @observable rightLoading= true;

  @observable tableStore = new TableStore();
  @observable logTable = new LogStore();
  @observable tableStore_whitewf = new TableStore();
  @observable tableStore_whitehrm = new TableStore();

  @observable searchForm = new WeaForm();
  @observable newForm = new WeaForm();
  @observable workflowForm = new WeaForm();


  @observable
  status = {
    hasNonStandard: true,
    nonStandLoading: true,
    selectedKey: 0,
    newFormHiddenFields: [],
    workflowFormHiddenFields: [],
    showSearchAd: false,
    rightMenus: [],
    condition: [],
    rowSelectIds_wf: [],
    rowSelectIds_hrm: [],
    showNewFormDialog: false,
    WhiteWorkflowDialogVisible: false,
    WhiteHrmDialogVisible: false,
    advancedSearchHeight: 120,
    paramsColumnsSet:[],
    paramsData:[],
    whiteWorkflowIds:[],
    whiteWorkflowIdsspan:[],
    sysid:[],
  };

  @action cleanStore = () => {
    this.searchForm = new WeaForm();
    this.setState({
      selectedKey: 0,
      advanceHeight: 0,
      newFormHiddenFields: [],
      showSearchAd: false,
      rightMenus: [],
      condition: [],
      rowSelectIds_wf: [],
      rowSelectIds_hrm: [],
      showNewFormDialog: false,
      WhiteWorkflowDialogVisible: false,
      WhiteHrmDialogVisible: false,
      advancedSearchHeight: 120,
      paramsColumnsSet:[],
      paramsData:[],
      whiteWorkflowIds:[],
      whiteWorkflowIdsspan:[],
      sysid:[],
      tabDatas: [{
        title: "LDAP",
        viewcondition: "0"
      }],
    });
  }

  @action
  doInit = () => {
    this.setState({
      nonStandLoading: false
    })
    this.cleanStore();
    this.getAuthenticationSourceList();
    this.getAuthenticationSourceCondition();
    this.getAuthenticationSourceRightMenu();
  };

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


  getAuthenticationSourceForm = (params={}) => {
    this.newForm = new WeaForm();
    this.setState({
      newFormCondition: [],
    })
    if(!params.id) {
      params = Object.assign({}, params, {type_: `${Number(this.status.selectedKey) + 1}`});
    }
    API.getAuthenticationSourceForm(params).then(result => {
      this.newForm.initFormFields(result.condition);
      this.setState({
        newFormHiddenFields: result.hiddenFields,
        paramsColumnsSet: result.paramsColumnsSet,
        paramsData: result.paramsData,
        newFormCondition: result.condition || [],
      })
    });
  };

  getAuthenticationSourceOperation = (params={}) => {
    API.getAuthenticationSourceOperation(params).then(result => {
      if(result.errorMsg) {
        message.error(result.errorMsg);
      } else {
        if (result.errmsgTip) {
          message.success(result.errmsgTip);
        }
        this.getAuthenticationSourceList();
        this.setState({
          showNewFormDialog: false,
        })
      }
    });

  };

  getAuthenticationSourceList = (p={}) => {
    this.tableStore = new TableStore();
    let params = (this.searchForm && this.searchForm.getFormParams()) || {};
    params = Object.assign({}, params, {type_: `${Number(this.status.selectedKey) + 1}`});
    API.getAuthenticationSourceList(params).then(result => {
      if (result.ret && result.ret == "noright") {
        this.hasRight = false;
      } else {
        this.hasRight = true;
      }
      this.rightLoading = false;
      if (result.sessionkey) {
        this.tableStore.getDatas(result.sessionkey, 1);
      } else {
        this.status = {...this.status, isAlertPage: true};
      }
    });
  };

  getAuthenticationSourceRightMenu = (params={}) => {
    API.getAuthenticationSourceRightMenu(params).then(result => {
      this.setState({
        rightMenus: result.rightMenus || [],
      });
    });
  };

  getAuthenticationSourceCondition = (params={}) => {
    this.searchForm = new WeaForm();
    this.setState({
      advanceHeight: 0,
      condition: [],
    })
    params = Object.assign({}, params, {type_: `${Number(this.status.selectedKey) + 1}`});
    API.getAuthenticationSourceCondition(params).then(result => {
      if(result.status == "1") {
        this.searchForm.initFormFields(result.condition);
        let advanceHeight = getSearchFormHeight(result.condition);
        this.setState({
          advanceHeight,
          condition: result.condition || [],
        });
      }
    });
  };
}
