import React from 'react'
import {inject, observer} from "mobx-react";
import {toJS} from "mobx";
import {WeaDialog, WeaTab, WeaLocaleProvider} from 'ecCom';
import {WeaTableNew} from "comsMobx";
import * as IntegrationUtil from "../../util/IntegrationUtil.js";
import isEmpty from 'lodash/isEmpty';

const {WeaTable} = WeaTableNew;
const {getLabel} = WeaLocaleProvider;

@inject("schedule")
@observer
export default class RunLog extends React.Component {
  state = {
    showRunLogSearchAd: false   
  }

  getTabButtonsAd = () => {
    const {systemAndRunLog} = this.props.schedule;
  
    let resetFun = () => systemAndRunLog.runSearchAdvance.reset();
    let cancelFun = () => this.setState({showRunLogSearchAd: false});

    return IntegrationUtil.getSearchBtns(this.getSearchFun, resetFun, cancelFun, this && this.props && this.props.ecId || '', "integrationScheduleRunLog");
  }

  getSearchFun = () => {
    const {getRunLogList, status} = this.props.schedule;
    const {record} = toJS(status);
    getRunLogList({scheduleid: record.pointid});
    this.setState({showRunLogSearchAd: false});
  }

  getSearchAdvanced = () => {
    const {systemAndRunLog} = this.props.schedule;
    const {runSearchAdvance, runSearchAdvanceCondition} = systemAndRunLog;
    let form = runSearchAdvance;
    let datas = toJS(runSearchAdvanceCondition);
    let index = toJS(runSearchAdvanceCondition)[0] && toJS(runSearchAdvanceCondition)[0].items.findIndex(v => v.domkey[0] === 'scheduleid');
    datas[0] && datas[0].items.splice(index, 1);
  
    return IntegrationUtil.getAdvancedItem(form, datas, this.getSearchFun, false, this && this.props && this.props.ecId || '', "integrationScheduleRunLog");
  };

  getColumnsInfo = (data) => {
    if (!isEmpty(data)) {
      for (let index = 0; index < data.length; index++) {
        if(data[index].dbField == "subcompanyid") {
          data[index].render = (text, record, index) => <a href={`${window.ecologyContentPath || ""}/spa/hrm/engine.html#/hrmengine/organization?showTree=false&isView=1&type=subcompany&id=${record.subcompanyid}`} target="_blank">{record.subcompanyidspan}</a>;
        }
      }
    }
  }

  render() {
    const {runLogTableStore, systemAndRunLog} = this.props.schedule;
    const {showRunLogSearchAd} = this.state;
    return (
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@ji2yvz`}
        icon={IntegrationUtil.GeneralTitleColor.icon}
        iconBgcolor={IntegrationUtil.GeneralTitleColor.iconBgcolor}
        style={{ width: 900, height: 650 }}
        hasScroll={true}
        hasOrder={true}
        title={getLabel(131274, "运行日志")}
        visible={systemAndRunLog.show}
        onCancel={() => systemAndRunLog.show = false}
        moreBtn={{
          datas: [],
          collectParams: [{
            favname: `${getLabel(16539, "计划任务")}`,
            favouritetype: 1,
            objid: 0,
            link: "wui/engine.html#/integration/schedule",
            importantlevel: 1
          }]
        }}
      >
        <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@1j59a9`}
          keyParam="viewcondition" //主键
          countParam="groupid" //数量
          selectedKey={"viewcondition"}
          datas={[]}
          advanceHeight={100}
          searchType={['advanced']}
          buttonsAd={this.getTabButtonsAd()}
          searchsAd={this.getSearchAdvanced()}
          showSearchAd={showRunLogSearchAd}
          setShowSearchAd={v => this.setState({showRunLogSearchAd: v})}
          hideSearchAd={v => this.setState({showRunLogSearchAd: v})}
          onChange={() => {}}
        />
        <div style={{height: 60}}>
          <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@bwe2fe`}
            key={'runLog'}
            comsWeaTableStore={runLogTableStore}
            hasOrder={true}
            getColumns={this.getColumnsInfo}
          />
        </div>
      </WeaDialog>
    )
  }
} 

