/**
 * Created by baoyinghai on 11/20/16.
 */

import React from 'react';
import { Cascader } from '../../../../../components/mxa';
import staticDisplay from './StaticDisplay';
import { VIEW, EDIT } from '../../constant';
import dic from '../../dic';

const ___AREA = dic.AREA;
const ___ADDRESS = dic.ADDRESS;
const ___INDUSTRYCATEGORY = dic.INDUSTRYCATEGORY;

class MyCascader extends React.Component {

  constructor(props) {
    super(props);
    this.area = [];
    this.state = {
      value: []
    };
  }


  treeSearch = (node = {}, value) => {
    if (node.value === value) {
      this.area.push(node.label);
      return node.label;
    }
    if (!node.children) {
      return null;
    }
    if (node.label !== 'root') {
      this.area.push(node.label);
    }
    const ret = node.children.some(children => this.treeSearch(children, value));
    if (!ret) {
      this.area.pop();
    }
    return ret;
  };

  componentWillReceiveProps(next) {
    if (next.value !== this.state.value) {
      this.state.value = this._changeString2List(next.value);
    }
  }

  _changeList2String = value => {
    const ret = [];

    value.reduce((obj, code) => {
      const tag = obj.filter(item => item.value === code)[0];
      if (tag) {
        ret.push(tag.label);
        return tag.children;
      }
      return [];
    }, this.props.options);
    let res = ret.join(',');
    if (this.props.isIndustryCategory) {
      res = this.props.options.filter(item => item.value === value[0])[0].serialNo + ',' + res;
    }
    return res;
  }

  /* eslint-disable */
  _changeString2List = (value = '') => {
    if (value && value.forEach) {
      return value;
    }
    const ret = [];
    if (value) {
      const splitRes = value.split(',');
      if (this.props.isIndustryCategory) {
        splitRes.shift();
      }
      splitRes.reduce((obj, code) => {
        const tag = obj.filter(item => item.label === code)[0];
        if (tag) {
          ret.push(tag.value);
          return tag.children;
        }
        return [];
      }, this.props.options);
    }
    return ret;
  }

  componentDidMount() {
    setTimeout(() => this.setState({value: this._changeString2List(this.props.value)}));
  }


  _onChange = target => {
    const formValue = this._changeList2String(target);
    if (this.props.isArea) {
      this.area = [];
      /* eslint-disable */
      this.treeSearch({label: 'root', value: '0', children: ___AREA}, target[0]);
      this.props.formHandler(this.props.fieldName.reduce((obj, item, index) => {obj[item] = this.area[index]; return obj;}, {}));
    }
    this.setState({ value: target });
    this.props.onChange && this.props.onChange(formValue);
  }


  render() {
    const bgStyle = { backgroundColor: '#F7F7F7' };
    return (
      <Cascader
        options={this.props.options}
        placeholder={this.props.disabled ? '' : "请选择"}
        disabled={this.props.disabled}
        value={this.state.value}
        onChange={this._onChange}
        style={this.props.disabled ? bgStyle : null}
      />
    );
  }
}

const compRender = config => {
  const { record = {}, props } = config;
  const displayComponent = record.displayComponent || {};
  let options = displayComponent.dictionaryItems;
  let isArea = false;
  let isIndustryCategory = false;
  let fieldName = [];
  if (displayComponent.type === 'ADDRESS') {
    /* eslint-disable */
    options = ___ADDRESS;
    isArea = true;
    fieldName.push(displayComponent.areaName);
    fieldName.push(displayComponent.localName);
  } else if (displayComponent.type === 'INDUSTRYCATEGORY') {
    /* eslint-disable */
    options = ___INDUSTRYCATEGORY;
    isIndustryCategory = true;
  }
  return (
    <MyCascader
      formHandler={props.form && props.form.setFieldsValue}
      fieldName={fieldName}
      isArea={isArea}
      isIndustryCategory={isIndustryCategory}
      options={options}
      placeholder="请选择"
      disabled={record.isReadonly || (props.isModalInputMappingItem && props.isLeadInto && props.modalInputIsReadonly)}
    />
  );
};

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