/**
 * Created by mar105 on 2019-02-27.
 */
/* eslint-disable object-curly-newline,prefer-destructuring */
import React, { Component } from 'react';
import { ExportOutlined, PrinterOutlined, SaveOutlined } from '@ant-design/icons';
import { Row, Col } from 'antd-v4';
import styles from '../Common/ToolBar/index.less';
import ShowType from '../Common/CommonComponent';
import * as commonUtils from '../../utils/utils';/* 通用方法 */
import AffixMenu from '../../routes/common/AffixMenu';
import ProductionPlan1 from '../../assets/ProductionPlan-1.svg';
import ProductionPlan2 from '../../assets/ProductionPlan-2.svg';
import ProductionPlan7 from '../../assets/ProductionPlan-7.svg';
import ProductionPlan8 from '../../assets/ProductionPlan-8.svg';
import ProductionPlan11 from '../../assets/ProductionPlan-11.svg';

export default class productionHeader extends Component {
  /**   构造函数   */
  constructor(props) {
    super(props);
    this.state = {
      enabled: props.enabled,
      masterConfig: props.masterConfig,
      stateValue: props.stateValue,
      masterData: props.masterData,
    };
  }

  /**   渲染前只执行一次   */
  componentWillMount() {
    /*   state和this属性赋值   */
    this.assignmentWillProps(this.props);
  }

  /**   props改变的时候触发   */
  componentWillReceiveProps(nextProps) {
    /*   state和this属性赋值   */
    this.assignmentWillProps(nextProps);
  }

  /**   返回true执行渲染,返回false不渲染   */
  shouldComponentUpdate(nextProps, nextState) {
    const {
      masterConfig, stateValue, enabled, masterData,
    } = this.state;
    const ret = enabled !== nextState.enabled || JSON.stringify(masterConfig) !== JSON.stringify(nextState.masterConfig) ||
      JSON.stringify(masterData) !== JSON.stringify(nextState.masterData) ||
      JSON.stringify(stateValue) !== JSON.stringify(nextState.stateValue);
    return ret;
  }

  assignmentWillProps = (props) => {
    this.setState({
      enabled: props.enabled,
      masterConfig: props.masterConfig,
      masterData: props.masterData,
      stateValue: props.stateValue,
    });
  }
  // 获取header按钮状态 stateValue
  HandlerHeaderButtonShowState = (props) => {
    const { masterData, searchSolution } = props;
    let filterCondition = [];
    let stateValue = 99;
    if (commonUtils.isNotEmptyObject(masterData) && commonUtils.isNotEmptyObject(masterData.sSearchSolutionId)) {
      const iIndex = searchSolution.findIndex(item => item.sId === masterData.sSearchSolutionId);
      if (iIndex > -1) {
        filterCondition = JSON.parse(searchSolution[iIndex].sCondition);
        let state = '';
        let frozen = '';
        filterCondition.forEach((item) => {
          if (item.bFilterName === 'sState') {
            state = item.bFilterValue;
          }
          if (item.bFilterName === 'bFrozen') {
            frozen = item.bFilterValue;
          }
        });

        if (commonUtils.isEmptyObject(frozen) && commonUtils.isNotEmptyObject(state)) {
          if (state === '未排程') {
            stateValue = 0;
          } else if (state === '未下达' || state === '已排程') {
            stateValue = 1;
          } else if (state === '已下达') {
            stateValue = 2;
          } else if (state === '已暂停') {
            stateValue = 3;
          } else if (state === '已完成') {
            stateValue = 5;
          }
        } else if (commonUtils.isNotEmptyObject(frozen)) {
          stateValue = 4;
        } else if (commonUtils.isNotEmptyObject(frozen) && commonUtils.isNotEmptyObject(state)) {
          stateValue = 99;
        }
      }
    }
    return { stateValue };
  }
  handleClick = (iTag) => {
    this.props.onButtonClick(iTag);
  }

  handleSwitchChange = (value) => {
    this.props.onSwitchChange(value);
  }
  rowReturnType = (child, i) => {
    const { enabled, stateValue } = this.state;
    const sMemo = child.sName.toLowerCase().endsWith('memo');
    const iColValue = sMemo ? 21 : child.iColValue * 2;
    const iOrder = sMemo ? 100 : child.iOrder > 100 ? 100 : child.iOrder;
    const enabledNew = (enabled && !child.bReadonly && !child.specialControl);
    const type = commonUtils.isNotEmptyObject(child.sControlName) && child.sControlName.toLowerCase().startsWith('btn');

    let showTypeProps = {};
    if (!type) {
      const switchBtn = child.sControlName.toLowerCase().startsWith('switch');
      if (!switchBtn) {
        showTypeProps = {
          name: 'master',
          form: this.props.form,
          formId: this.props.sModelsId,
          getSqlDropDownData: this.props.getSqlDropDownData,
          getSqlCondition: this.props.getSqlCondition,
          handleSqlDropDownNewRecord: this.props.handleSqlDropDownNewRecord,
          getFloatNum: this.props.getFloatNum,
          getDateFormat: this.props.getDateFormat,
          onChange: this.props.onChange,
          showConfig: child,
          textArea: sMemo,
          enabled: enabledNew,
          dataValue: commonUtils.isNotEmptyObject(this.props.masterData) ? this.props.masterData[child.sName] : '',
          bTable: false,
        };
        return (
          <Col key={i} span={iColValue} order={iOrder} style={{ margin: 1, backgroundColor: 'rgb(100, 100, 100)', color: '#fff' }}>
            <ShowType {...showTypeProps} style={{ height: 20, lineHeight: '20px' }} />
          </Col>
        );
      } else {
        return '';
      }
    } else {
      let displayFlag = false;
      let disabledFlag = true;
      if ((stateValue === '0' && child.iTag === 3) || (stateValue === '1' && child.iTag === 5)
        || (stateValue === '0' && child.iTag === 5) || (stateValue === '2' && child.iTag === 4)
        || (stateValue === '2' && child.iTag === 5)
        || (stateValue === '2' && child.iTag === 17)
        || (stateValue === '3' && child.iTag === 6)
        || (child.iTag === 1) || (child.iTag === 2)
        || (child.iTag === 7) || (child.iTag === 8) || (child.iTag === 11)
        || (child.iTag === 12) || (child.iTag === 13) || (child.iTag === 14) || (child.iTag === 15) || (child.iTag === 16)
      ) {
        displayFlag = true;
      }
      if ((child.iTag === 1 && (stateValue === '0' || stateValue === '1')) ||
          (child.iTag === 2 && (stateValue === '0' || stateValue === '1' || stateValue === '2')) ||
          (child.iTag === 3 && (stateValue === '0' || stateValue === '1')) ||
          (child.iTag === 4 && stateValue === '2') ||
          (child.iTag === 5 && stateValue === '2') ||
          (child.iTag === 6 && stateValue === '3') ||
          (child.iTag === 7 && stateValue === '2') ||
          (child.iTag === 8 && stateValue === '2') ||
          (child.iTag === 11 && (stateValue === '0' || stateValue === '1' || stateValue === '2')) ||
          ((child.iTag === 12 || child.iTag === 13 || child.iTag === 14) && stateValue !== '99')
      ) {
        disabledFlag = false;
      }
      // console.log(child.showName, '  disabledFlag:', disabledFlag, 'displayFlag:', displayFlag);
      return displayFlag ? (
        <span key={i} order={iOrder} style={{ margin: '0 15px', float: 'left', display: displayFlag ? 'inline-block' : 'none', color: '#fff', lineHeight: '30px' }} disabled={disabledFlag}onClick={this.handleClick.bind(this, child.iTag)} >
          {(() => {
                switch (child.iTag) {
                  case 1: return <img src={ProductionPlan1} disabled={disabledFlag} alt="" />;
                  case 2: return <img src={ProductionPlan2} disabled={disabledFlag} style={{ width: '13px', height: '13px' }} alt="" />;
                  case 7: return <img src={ProductionPlan7} disabled={disabledFlag} alt="" />;
                  case 8: return <img src={ProductionPlan8} disabled={disabledFlag} alt="" />;
                  case 11: return <img src={ProductionPlan11} disabled={disabledFlag} style={{ width: '15px', height: '15px' }} alt="" />;
                  case 12: return <SaveOutlined disabled={disabledFlag} />;
                  case 13: return <PrinterOutlined disabled={disabledFlag} />;
                  case 14: return <ExportOutlined disabled={disabledFlag} />;
                  default: return null;
                }
              }
            )()}
          {child.showName}
        </span>
      ) : '';
    }
  }

  render() {
    const { masterConfig } = this.state;
    const teamMachineProps = masterConfig === undefined ? undefined : masterConfig.gdsconfigformslave;
    return (
      <div className={styles.toolBar}>
        <Row style={{ backgroundColor: 'rgb(100, 100, 100)', margin: '0 10px', height: 30 }} id="product-info-header">
          {
          commonUtils.isNotEmptyObject(teamMachineProps) ? teamMachineProps.map((child, i) => {
            return this.rowReturnType(child, i);
          }) : ''
        }
        </Row>
        <AffixMenu />
      </div>
    );
  }
}
