// 异构系统基础信息页面
import React from "react";
import { inject, observer } from "mobx-react";
import { toJS } from "mobx";
import { WeaLocaleProvider, WeaFormItem, WeaSearchGroup, WeaHelpfulTip } from "ecCom";
import { WeaSwitch } from "comsMobx";
import * as IntegrationUtil from "../../util/IntegrationUtil";
import HRMNetworkIPList from '../../public/HRMNetworkIPList';

const getLabel = WeaLocaleProvider.getLabel;

@inject("todoCenterStore")
@observer
export default class OfsInfoBaseInfoContent extends React.Component {
  /**
   * 短信提醒
   */
  handleMessageTemplate = () => {
    const { todoCenterStore } = this.props;
    todoCenterStore.getSmstemplate();
    todoCenterStore.setState({ smsTemplateVisible: true });
  }

  getAutoLoginFlagDom = (form) => {
    const {
      todoCenterStore,
    } = this.props, {
      setHRMNetworkListVisible,
      status,
    } = todoCenterStore, {
      automaticLogin = [],
    } = status;
    const formParams = form.getFormParams(),
      temp = (formParams.autologinflag == '1') ? automaticLogin : automaticLogin.slice(0, 1);

    return temp.map((field, index) => {
      return (
        <div style={{ float: 'left' }}>
          <div style={{ width: (index == 1 ? 218 : 50), float: 'left' }}>
            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@31dk2z@integrationTodoCenterAddFormButton${field.domkey[0]}`}
              fieldConfig={field}
              form={form}
            />
          </div>
          {index == '1' && <div style={{ float: 'left', marginLeft: 5 }}>
            <a style={{ marginLeft: 5, marginTop: 6, display: 'inline-block' }} onClick={() => setHRMNetworkListVisible(true)}>{getLabel(83726, '网段策略')}</a>
            <WeaHelpfulTip ecId={`${this && this.props && this.props.ecId || ''}_WeaHelpfulTip@3o6yyn@integrationTodoCenterAddFormButton${field.domkey[0]}`}
              width={225}
              style={{ marginLeft: 5 }}
              title={`${getLabel(18890, '点击')}[${getLabel(83726, '网段策略')}]${getLabel(125421, '可进入网段策略设置页面')}`}
            />
          </div>}
        </div>
      )
    })
  }

  getDialogContent = (form, condition = []) => {
    const {
      isFormInit,
    } = form;
    let group = [],
      formParams = form.getFormParams();

    isFormInit && condition && condition.forEach(c => {
      let items = [];
      c && c.items && c.items.forEach(fields => {
        let key = fields.domkey[0];
        if ("ipaddress" != key) {
          if (key === 'pcouterfixurl' && formParams.autologinflag == '1') {
            fields.viewAttr = 3;
          }

          items.push({
            com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@oxpbay@${key}`}
              label={fields.label}
              labelCol={{ span: fields.labelcol }}
              wrapperCol={{ span: fields.fieldcol }}
              error={form.getError(fields)}
              tipPosition="bottom"
            >
              <nobr>
                {
                  'autologinflag' === key
                    ? this.getAutoLoginFlagDom(form)
                    : <WeaSwitch
                      ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@sdc5s6@${key}`}
                      fieldConfig={fields}
                      form={form}
                      onChange={() => {
                        if (['syscode', "sysshortname", "sysfullname"].includes(key)) {
                          let tipCode = { "syscode": "2", "sysshortname": "5", "sysfullname": "6" };
                          IntegrationUtil.checkUnique({
                            tableName: 'ofs_sysinfo',
                            fieldName: key,
                            fieldValue: form.getFormParams()[key].replace(/^(\s*)|(\s*)$/g, ''),
                            idName: 'id',
                            idValue: ''
                          }, form, key, tipCode[key]);
                        }
                      }}
                    />
                }
                {
                  key === "isshortmsg" && formParams.isshortmsg == 1
                    ? <span style={{ marginLeft: 20, color: "#2db7f5", cursor: "pointer" }} onClick={this.handleMessageTemplate}>{getLabel('514719', '短信提醒模板')}</span>
                    : null
                }
              </nobr>
            </WeaFormItem>),
            colSpan: 1,
            hide: form.isHide(fields, (fieldKey, formParams) => {
              let hide = false;
              key === "inneripselecct" && formParams.autologinflag != "1" && (hide = true);
              key === 'customclass' && formParams.iscustom != '1' && (hide = true);
              ['customclass', 'iscustom', 'ismsgcenter', 'isim', 'isshortmsg'].includes(key) && formParams.isuse != '1' && (hide = true);
              ['status', 'warningmode', 'reminder'].includes(key) && formParams.ismonitor != '1' && (hide = true);

              return hide;
            })
          });
        }
      });

      group.push(
        <WeaSearchGroup
          ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@tzcd6r`}
          center={true}
          col={1}
          needTigger={true}
          title={c.title}
          showGroup={c.defaultshow}
          items={items}
        />
      )
    })

    return group;
  }

  render () {
    const {
      todoCenterStore,
    } = this.props, {
      status,
      ofsInfoAddForm,
      setHRMNetworkListVisible,
    } = todoCenterStore, {
      ofsInfoAddFormCondition = [],
      HRMNetworkIPListVisible,
    } = status;

    return <div>
      {this.getDialogContent(ofsInfoAddForm, toJS(ofsInfoAddFormCondition))}
      <HRMNetworkIPList ecId={`${this && this.props && this.props.ecId || ''}_HRMNetworkIPList@blanh6`}
        visible={HRMNetworkIPListVisible}
        onCancel={() => setHRMNetworkListVisible(false)}
      />
    </div>
  }
}
