import React from "react";
import { WeaFormItem, WeaSearchGroup, WeaTools } from "ecCom";
import { WeaSwitch } from "comsMobx";
import CustomTreeSelect from "./components/PDDialog/customTreeSelect";
import CustomBrowser from "../../components/CustomBrowser";

const getKey = WeaTools.getKey;
export const formRender = (form, conditions, params) => {
  const { isFormInit } = form;
  const formParams = form.getFormParams();
  let group = [];
  isFormInit && conditions && conditions.map(c => {
    let items = [];
    c.items.map(fields => {
      items.push({
        com: (
          <WeaFormItem label={fields.label} labelCol={{ span: `${fields.labelcol}` }}
                       wrapperCol={{ span: `${fields.fieldcol}` }} error={form.getError(fields)}
                       tipPosition="bottom">
            {
              getKey(fields) === "item" ?
                <React.Fragment>
                  <CustomTreeSelect
                    detail={params} fieldConfig={fields} form={form} formParams={formParams}/>
                  {
                    _.isEmpty(formParams.item) &&
                    <span className="wea-required-e9" style={{ verticalAlign: "middle" }}>
                        <img src="/images/BacoError_wev9.png" alt=""/>
                      </span>
                  }
                </React.Fragment>
                :
                getKey(fields) === "modeName" ?
                  <CustomBrowser fieldConfig={fields} form={form} formParams={formParams}
                                 onCustomChange={(v) => !!_.values(v)[0] && form.updateFields({
                                   tableName: _.values(v)[0].subname,
                                   modeId: _.values(v)[0].domid
                                 })}/>
                  : <WeaSwitch fieldConfig={fields} form={form} formParams={formParams}/>
            }
          </WeaFormItem>),
        colSpan: 1,
        hide: fields.hide
      });
    });
    !_.isEmpty(items) && group.push(
      <WeaSearchGroup col={c.col || 1} needTigger={true} showGroup={c.defaultshow} items={items} center={false}
                      title={c.title}/>);
  });
  return group;
};
