import React from "react";
import { inject, observer } from "mobx-react";
import { toJS } from "mobx";
import { Button } from "antd";
import {
  WeaLocaleProvider,
  WeaTop,
  WeaFormItem,
  WeaTools,
  WeaSearchGroup
} from "ecCom";

import { WeaSwitch } from "comsMobx";
const { getKey } = WeaTools;
const getLabel = WeaLocaleProvider.getLabel;

import * as IntegrationUtil from "../../util/IntegrationUtil";
const getFormSearchs = (form, datas, needTigger = true, lableCol, fieldcol) => {
  const { isFormInit } = form;
  let group = [];
  const formParams = form.getFormParams();
  isFormInit &&
    datas &&
    datas.map((c, index) => {
      let items = [];
      c.items.map(fields => {
        items.push({
          com: (
            <WeaFormItem
              label={`${fields.label}`}
              labelCol={{ span: lableCol ? lableCol : `${fields.labelcol}` }}
              wrapperCol={{ span: fieldcol ? fieldcol : `${fields.fieldcol}` }}
              error={form.getError(fields)}
              tipPosition="bottom"
            >
              <WeaSwitch
                fieldConfig={fields}
                form={form}
                formParams={formParams}
              />
            </WeaFormItem>
          ),
          colSpan: 1,
          hide: form.isHide(fields, (keys, allParams) => {
            if (getKey(fields) !== "isUse") {
              return allParams.isUse === "0" || allParams.isUse === "";
            }
          })
        });
      });
      if (needTigger) {
        let title = "";
        const isUse = form.getFormParams().isUse;
        if ((isUse === "" || isUse === "0") && index !== 0) {
          title = "";
        } else {
          title = c.title;
        }
        group.push(
          <WeaSearchGroup
            center
            col={1}
            needTigger={needTigger}
            title={title}
            showGroup={c.defaultshow}
            items={items}
          />
        );
      } else {
        group.push(
          <WeaSearchGroup
            col={1}
            needTigger={needTigger}
            showGroup={c.defaultshow}
            items={items}
          />
        );
      }
    });
  return group;
};

@inject("webseal")
@observer
export default class IMrtx extends React.Component {
  componentDidMount() {
    const { webseal } = this.props;
    webseal.doInit();
  }

  onRightMenuClick = (type, recordId) => {
    const { webseal } = this.props;
    const { save, newBaseForm } = webseal;
    const formParams = newBaseForm.getFormParams();
    switch (type) {
      case "save":
        save(formParams);
        break;
    }
  };

  getBtns = () => {
    const { cas } = this.props;
    let btns = [];
    btns = [
      <Button
        type="primary"
        onClick={() => {
          {
            // this.add(1)
            this.onRightMenuClick("save");
          }
        }}
      >
        {getLabel("31691,82986", "保存")}
      </Button>,
      <Button
        onClick={() => {
          {
            // this.add(3)
            this.onRightMenuClick("test");
          }
        }}
      >
        {getLabel("31691,82988", "完成")}
      </Button>
    ];
    return btns;
  };

  getRightMenu() {
    const { weaseal } = this.props;
    let btnArr = [];
    return btnArr;
  }

  render() {
    const { webseal } = this.props;
    const { newBaseForm, status } = webseal;
    const { newBaseFormFields } = toJS(status);
    return (
      <div>
        <WeaTop
          title={getLabel(128589, "WebSEAL集成")}
          icon={<i className={IntegrationUtil.GeneralTitleColor.icon} />}
          iconBgcolor={IntegrationUtil.GeneralTitleColor.iconBgcolor}
          buttons={this.getBtns()}
          showDropIcon={true}
          dropMenuDatas={this.getRightMenu()}
          dropMenuProps={{
            collectParams: {
              favname: `${getLabel(128589, "WebSEAL集成")}`,
              favouritetype: 1,
              objid: 0,
              link: "wui/engine.html#/inteengine/imrtx",
              importantlevel: 1
            }
          }}
        />
        {getFormSearchs(newBaseForm, newBaseFormFields)}
      </div>
    );
  }
}
