import React from 'react';
import { Grid, Toast } from 'antd-mobile-v2';
import 'antd-mobile-v2/dist/antd-mobile.css';
import styles from './SceneMobile.less';
import commonConfig from '../../utils/config';
import * as commonServices from '../../services/services';
import * as commonUtils from '../../utils/utils';
import CommobileBase from '../../mobile/common/CommobileBase';
import CommobileListEvent from './CommobileListEvent';
import CommobileChar from '../../mobile/common/CommobileChar';
import Green from '../../assets/mobile/0.png';
import Blue from '../../assets/mobile/10.png';
import Orange from '../../assets/mobile/20.png';
import Red from '../../assets/mobile/30.png';

class WorkBenchMobile extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      totalData: [],
    };
  }

  async componentWillMount() {
    /* 获取配置 */
    let { sModelType } = this.props;
    sModelType = commonUtils.isEmptyObject(sModelType) ? 'workbench' : sModelType;
    const { token } = this.props.app;
    const configUrl = `${commonConfig.server_host}mobilephone/getMenu/${sModelType}?sModelsId=100`;
    const configReturn = (await commonServices.getService(token, configUrl)).data;
    if (configReturn.code === 1) {
      const returnData = configReturn.dataset.rows;/* 全部数据 */
      if (commonUtils.isNotEmptyArr(returnData)) {
        this.setState({ totalData: returnData });
      }
    } else {
      Toast.success(configReturn.msg);
    }
    this.props.onSaveState({ searchSolution: [] });
  }

  handleGetAllMenuData() {
    const { totalData } = this.state;
    let gridData = [];

    const { token } = this.props.app;

    if (commonUtils.isNotEmptyArr(totalData)) {
      // eslint-disable-next-line array-callback-return
      return totalData.map((item) => {
        if (item.sMenuName !== '图表分析') {
          const firstMenu = {};
          firstMenu.bUnReason = item.bUnReason;
          firstMenu.bUnTask = item.bUnTask;
          firstMenu.bVisible = item.bVisible;
          firstMenu.children = item.children;
          firstMenu.iOrder = item.iOrder;
          firstMenu.sChinese = item.sChinese;
          firstMenu.sMenuName = item.sMenuName;
          firstMenu.sId = item.sId;
          firstMenu.sModelType = item.sModelType;
          firstMenu.sName = item.sName;
          firstMenu.sParentId = item.sParentId;
          firstMenu.sProcName = item.sProcName;
          firstMenu.sTitleLogoPath = item.sTitleLogoPath;
          firstMenu.sUnType = item.sUnType;
          const childrenData = item.children;
          if (firstMenu.sName !== '') {
            const { slaveData } = this.props;
            const slaveDataOld2 = slaveData === undefined ? [] : slaveData;
            gridData = [];
            if (commonUtils.isNotEmptyArr(slaveDataOld2)) {
              slaveDataOld2.forEach((itemChild) => {
                const addStata = {};
                let dataUrl = '';
                addStata.iTodayNo = itemChild.iTodayNo;
                if (commonUtils.convertStrToNumber(addStata.iTodayNo) <= 0) {
                  dataUrl = Green;
                } else if (commonUtils.convertStrToNumber(addStata.iTodayNo) <= 10) {
                  dataUrl = Blue;
                } else if (commonUtils.convertStrToNumber(addStata.iTodayNo) <= 20) {
                  dataUrl = Orange;
                } else {
                  dataUrl = Red;
                }
                addStata.icon = dataUrl;
                addStata.text = itemChild.sMakePerson;
                addStata.url = '/sss';
                addStata.sModelsId = itemChild.sId;
                gridData.push(addStata);
              });
            }
          } else if (commonUtils.isNotEmptyArr(childrenData)) {
            gridData = [];
            childrenData.forEach((itemChild) => {
              const addStata = {};
              const dataUrl = `${commonConfig.file_host}file/download?savePathStr=${itemChild.sTitleLogoPath}&sModelsId=100&token=${token}`;
              addStata.icon = dataUrl;
              addStata.iTodayNo = '';
              addStata.text = itemChild.sMenuName;
              addStata.url = itemChild.sName;
              addStata.sModelsId = itemChild.sId;
              gridData.push(addStata);
            });
          }
          return (

            <div className="GridContent">
              <div className={styles.SubTitle}>{firstMenu.sMenuName}</div>
              <Grid
                data={gridData}
                renderItem={dataItem => (
                  <div style={{ padding: '10px' }}>
                    <div>
                      <img src={dataItem.icon} style={{ width: '30px', height: '30px' }} alt="" />
                      <div style={{ color: '#888', fontSize: '12px', marginTop: '6px' }}>
                        <span>{dataItem.text}</span>
                      </div>
                    </div>
                    {
                      dataItem.iTodayNo !== ''
                        ?
                          <div style={{
                          color: '#fff',
                          fontSize: '10px',
                          marginTop: '-47px',
                        }}
                          >{dataItem.iTodayNo}
                          </div>
                        : null
                    }


                  </div>

                )}
                hasLine={false}
                onClick={this.handleGridClick}
              />
            </div>
          );
        }
      });
    }
  }
  handleGridClick = (el) => {
    const { dispatch } = this.props;
    const { url } = el;/* 菜单对应的路由地址 */
    const sNameUrl = `${commonConfig.server_host}gdsmodule/getGdsmoduleById/${el.sModelsId}?sModelsId=${el.sModelsId}`;
    if (url !== '') {
      dispatch({
        type: 'content/onRouterMobile',
        payload: {
          url, /*   接口地址   */
          urlKey: sNameUrl,
          sModelsId: el.sModelsId,
        },
      });
    }
  }

  render() {
    const { totalData } = this.state;
    let sModelsId = '';
    if (commonUtils.isNotEmptyArr(totalData)) {
      const sModelsIdItem = totalData.findIndex(item => item.sMenuName === '图表分析');
      if (sModelsIdItem > -1) {
        sModelsId = totalData[sModelsIdItem].sId;
      }
    }

    return (
      <div>
        {this.handleGetAllMenuData()}
        {commonUtils.isNotEmptyStr(sModelsId) ? <CommobileChar {...this.props} sModelsId={sModelsId} /> : '' }
      </div>
    );
  }
}
export default CommobileBase(CommobileListEvent(WorkBenchMobile));
