import React from 'react';
import { Grid, Toast } from 'antd-mobile-v2';
import 'antd-mobile-v2/dist/antd-mobile.css';
import styles from './SceneMobile.less';
// eslint-disable-next-line import/first
import { createForm } from 'rc-form';
import commonConfig from '../../utils/config';
import * as commonServices from '../../services/services';
import * as commonUtils from '../../utils/utils';
import CommobileBase from './CommobileBase';
import CommobileBillEvent from './CommobileBillEvent';

class SceneCssMobile extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      totalData: [],
    };
  }
  async componentWillMount() {
    const masterConfig = {};
    const masterData = [];
    this.props.onSaveState({ masterConfig, masterData });
    /* 获取配置 */
    let { sModelType } = this.props;
    sModelType = commonUtils.isEmptyObject(sModelType) ? 'scene' : sModelType;
    const { token } = this.props.app;
    const sLoginType = 'sLoginCss';
    const configUrl = `${commonConfig.server_host}mobilephone/getMenuSrmCss/${sModelType}/${sLoginType}?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);
    }
  }
  handleGetAllMenuData = () => {
    const { totalData } = this.state;
    const { token } = this.props.app;
    if (commonUtils.isNotEmptyArr(totalData)) {
      return totalData.map((item) => {
        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;
        const gridData = [];
        if (commonUtils.isNotEmptyArr(childrenData)) {
          childrenData.forEach((itemChild) => {
            const addStata = {};
            const dataUrl = `${commonConfig.file_host}file/download?savePathStr=${itemChild.sTitleLogoPath}&sModelsId=100&token=${token}`;
            addStata.icon = dataUrl;
            addStata.text = itemChild.sMenuName;
            addStata.url = itemChild.sName.replace('indexMobile', 'indexCssMobile');
            addStata.sModelsId = itemChild.sId;
            addStata.sModelType = itemChild.sModelType;
            addStata.iOrder = itemChild.iOrder;
            gridData.push(addStata);
          });
        }
        gridData.sort((item, item2) => item.iOrder - item2.iOrder);
        return (
          <div className="GridContent">
            <div className={styles.SubTitle}>{firstMenu.sMenuName}</div>
            <Grid data={gridData} hasLine={false} onClick={this.handleGridClick} />
          </div>
        );
      });
    }
  }
  handleGridClick = (el) => {
    const { dispatch } = this.props;
    const { url } = el;/* 菜单对应的路由地址 */
    if (url !== '') {
      const sNameUrl = `${commonConfig.server_host}gdsmodule/getGdsmoduleById/${el.sModelsId}?sModelsId=${el.sModelsId}`;
      dispatch({
        type: 'content/onRouterMobile',
        payload: {
          url, /*   接口地址   */
          urlKey: sNameUrl,
          sModelsId: el.sModelsId,
          sModelType: el.sModelType,
        },
      });
    }
  }

  /** 修改主表数据 */
  handleMasterChange = async (name, sFieldName, changeValue, sId, dropDownData, isWait, masterDataNew) => {
    if (sFieldName === 'sMachineId') {
      const { dispatch } = this.props;
      const returnData = await this.props.onChange(name, sFieldName, changeValue, sId, dropDownData, true, masterDataNew);
      dispatch({
        type: 'app/saveMachineId',
        payload: { sMachineId: returnData.masterData.sMachineId },
      });
      this.props.onSaveState({ ...returnData });
    } else {
      this.props.onChange(name, sFieldName, changeValue, sId, dropDownData, isWait, masterDataNew);
    }
  }


  render() {
    return (
      <div>
        {this.handleGetAllMenuData()}
      </div>
    );
  }
}
const SceneMobileComponent = createForm()(SceneCssMobile);
export default CommobileBase(CommobileBillEvent(SceneMobileComponent));
