/**
 * Created by baoyinghai on 12/7/16.
 */
import React from 'react';
import staticDisplay from './StaticDisplay';
import { ModalInput } from '../../../../../components';
import Compose from '../../../../utils/Compose';
import AsyncDecorator from '../../../../pageContainer/AsyncDecorator';
import InitDecorator from '../../../../pageContainer/InitDecorator';
import { VIEW, EDIT } from '../../constant';
import { isEmpty } from '../../../../utils/EmptyUtils';
import { getMappings } from '../../../../utils/ModalInputUtils';
import { getTemplateFields } from '../../../../utils/StringUtils';
import { Modal } from '../../../../../components/mxa';

const compRender = config => {
  const { record, props, changeInitValue } = config;
  const TempPage = Compose(AsyncDecorator, InitDecorator)();
  // 模板中调用this.props.callback可以执行回调
  const modalData = (record && record.modalInput) || {};
  // const mappingFields = record.modalInput.mappingFields;
  const mappingFields = getMappings(modalData, props);
  let mapper;
  if (mappingFields.length > 0) {
    mapper = value => value[mappingFields[0].remoteFieldName];
  }

  const resetValue = (value, selectType) => {
    console.log(value);
    const newValue = {};
    if (selectType) {
      mappingFields.map(item => {
        item.type = props.dataSource.fields.filter((item1, index1) => item1.name === item.fieldName)[0].type;
        return item;
      });
    }
    mappingFields && mappingFields.forEach(m => {
      if (m.type && m.type === 'String') {
        newValue[m.fieldName] = value[m.remoteFieldName] !== undefined ? value[m.remoteFieldName] + '' : null;
      } else {
        newValue[m.fieldName] = value[m.remoteFieldName] !== undefined ? value[m.remoteFieldName] : null;
      }
    });
    changeInitValue(value, newValue);
  };

  const getDescription = name => {
    const res = props.dataSource.fields && props.dataSource.fields.filter(f => f.name === name);
    return res && res[0] && res[0].description;
  };

  const getParams = () => {
    let formValues = {};
    if (props && props.form) {
      formValues = props.form.getFieldsValue();
    }
    let ret = {};
    const isError = record.modalInput && record.modalInput.bindParameters && record.modalInput.bindParameters.some(bindP => {
      if (bindP.name && bindP && !bindP.allowNull) {
        if (!isEmpty(bindP.value)) {
          ret[bindP.name] = bindP.value;
        } else if (formValues[bindP.name]) {
          ret[bindP.name] = formValues[bindP.name];
        } else if (props.query && props.query[bindP.name]) {
          ret[bindP.name] = props.query[bindP.name];
        } else {
          const content = record.modalInput.promptDesc && record.modalInput.promptDesc;
          Modal.error({
            title: '提示',
            content: (content) || '请先输入' + getDescription(bindP.name) + '!',
          });
          ret = false;
          return true;
        }
      }
      return false;
    });
    if (!isError) {
      const templateFields = getTemplateFields(record.modalInput.actionLink);
      if (templateFields.length > 0) {
        templateFields.some(item => {
          if (!formValues[item]) {
            const content = record.modalInput.promptDesc && record.modalInput.promptDesc;
            Modal.error({
              title: '提示',
              content: (content) || '请先输入' + getDescription(item) + '!',
            });
            ret = false;
            return true;
          }
          return false;
        });
      }
    }
    return ret;
  };

  return (
    <ModalInput
      {...props}
      getParams={record.modalInput && record.modalInput.bindParameters && getParams}
      mapper={mapper} resetValue={resetValue}
      disabled={modalData.disabled || (!record.isReadonly && props.isLeadInto)}
      isReadonly={modalData.disabled || record.isReadonly}
      domainType={modalData.domainType || modalData.templateType || modalData.subDomain}
      domainLink={modalData.domainLink || modalData.actionLink}
      modalData={modalData}
      params={props.query}
      selectType={modalData.selectType}
      record={record}
    />
  );
};

export default {
  [VIEW]: staticDisplay,
  [EDIT]: compRender
};
