import { Modal } from 'antd';
import { WeaSearchGroup, WeaDropdown, WeaTag, WeaFormItem, WeaTools } from 'ecCom';
import { WeaSwitch } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const { getKey } = WeaTools;

//用于获取高级搜索中的表单控件
export const getSearchs = (form, datas, needTigger = true, col = 1, diffKey="") => {
  const { isFormInit } = form;
  let group = [];
  const formParams = form.getFormParams();
  isFormInit && datas && datas.map(c => {
    let items = [];
    c.items.map(fields => {
      items.push({
        com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@0qcuna@${diffKey}`}
          label={`${fields.label}`}
          labelCol={{ span: `${fields.labelcol}` }}
          wrapperCol={{ span: 9 }}>
          <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@13sdvh@${diffKey}`} fieldConfig={fields} form={form} formParams={formParams} />
        </WeaFormItem>),
        colSpan: 1
      })
    });
    if (needTigger) {
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@p8lyee@${diffKey}`} col={col} needTigger={needTigger} title={c.title} showGroup={c.defaultshow} items={items} />)
    } else {
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@j3ie4n@${diffKey}`} col={col} needTigger={needTigger} showGroup={c.defaultshow} items={items} />)
    }
  });
  return group;
}

//带表单验证的form数据
export const getFormSearchs = (form, datas, needTigger = true, hide, diffKey="") => {
  const { isFormInit } = form;
  let group = [];
  const formParams = form.getFormParams();
  isFormInit && datas && datas.map(c => {
    let items = [];
    c.items.map(fields => {
      // console.log(fields.label, getKey(fields))
      items.push({
        com: (
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@9aq57q@${diffKey}`}
            label={`${fields.label}`}
            labelCol={{ span: fields.labelcol }}
            wrapperCol={{ span: fields.fieldcol }}
            fieldType={fields.conditionType.toUpperCase()}
            error={form.getError(fields)}
            tipPosition="bottom"
          >
            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@u7t1cg@${diffKey}`} fieldConfig={fields} form={form} formParams={formParams} />
          </WeaFormItem>),
        colSpan: 1,
        hide: form.isHide(fields, (keys, allParams) => {
          if (hide) {
            return hide(getKey(fields), keys, allParams)
          }
        })
      })
    });
    if (needTigger) {
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@7tlb4a@${diffKey}`} center col={1} needTigger={needTigger} title={c.title} showGroup={c.defaultshow} items={items} />)
    } else {
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@yel41n@${diffKey}`} center col={1} needTigger={needTigger} showGroup={c.defaultshow} items={items} />)
    }
  });
  return group;
}

//form重置
export const doFormReset = (form, conditioninfo) => {
  if (form && conditioninfo) {
    form.resetForm();
    form.initFormFields(conditioninfo);
  }
}

//删除等信息确认
export const doConfirm = (content, func, val) => {//确认信息， 方法， 方法参数
  Modal.confirm({
    title: `${getLabel(131329, '信息确认')}`,
    content: content,
    okText: `${getLabel(16631, '确定')}`,
    cancelText: `${getLabel(201, '取消')}`,
    onOk:(typeof func === 'function') ? () => func(val) : () => {},
  })
}

//提示
export const setModal = (content) => {
  Modal.warning({
    title: `${getLabel(15172, '系统提示')}`,
    content: content,
  });
}

//selectedRows遍历得到ids
export const getIds = (datas) => {
  let ids = [];
  datas && datas.length !== 0 && datas.map(item => {
    ids.push(Number(item.id))
  })
  return ids.join(',')
}

export const onCheck = (form, func) => {
  form.validateForm().then(f => {
    if (f.isValid) {
      typeof func == 'function' && func()
    } else {
      f.showErrors();
    }
  });
}