// 车辆导入 人员导入 socket处理
import React from 'react';
import { notification } from 'antd';
import './index.less';
import { cache, uuid } from '@zenview/micro-utils';

class VehicleEntrySocket extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: {},
    };
    console.log('是否有history', this.props);
  }

  // componentDidMount() {
  //   SocketEmitter.subscribe('importVillage', this.importVillage);
  // }
  // componentWillUnmount() {
  //   SocketEmitter.unsubscribe('importVillage', this.importVillage);
  // }

  importVillage = (res = {}) => {
    const { message = {} } = res;
    let { totalCount = 0, wrongInfoCount = 0, noPicCount = 0, successCount = 0, otherCount = 0, failCount = 0, picWithoutFeatureCount = 0, type = 'PEOPLE', info = '', communityUnitErrors = [] } = message;
    if (type === 'HOUSE') {
      try {
        let mess = JSON.parse(message.message);
        successCount = mess.successCount;
        failCount = mess.failCount;
        totalCount = mess.totalCount;
        info = mess.info;
      } catch (error) {}
    }
    let successNum = 0;
    let failNum = 0;
    let name = '';
    switch (type) {
      case 'PEOPLE':
        successNum = successCount + '人';
        failNum = failCount + '人';
        name = '人员';
        break;
      case 'HOUSE':
        successNum = successCount + '间';
        failNum = failCount + '间';
        name = '房屋';
        break;
      case 'VEHICLE':
        successNum = totalCount - wrongInfoCount + '车';
        failNum = wrongInfoCount + '车';
        name = '车辆';
        break;
      case 'community_unit':
        successNum = successCount + '单位';
        failNum = Array.isArray(communityUnitErrors) ? `${communityUnitErrors.length}单位` : '0单位';
        name = '单位管理';
        break;
      default:
        break;
    }
    const content = res.message && (
      <div className="community-upload-modal">
        <div className="title">
          <span className="value">导入成功：</span>
          <div className="info">{successNum}</div>
        </div>
        <div className="title">
          <span className="value">导入失败：</span>
          <div className="info">{failNum}</div>
        </div>
        {type === 'PEOPLE' && (
          <div className="title">
            <span className="value">失败原因：</span>
            <div className="value-box">
              <p className="box-title">
                <span className="box-span">照片无特征值 </span> {picWithoutFeatureCount || 0}人
              </p>
              <p className="box-title">
                <span className="box-span">暂无照片 </span> {noPicCount || 0}人
              </p>
              <p className="box-title">
                <span className="box-span">信息填写错误 </span> {wrongInfoCount || 0}人
              </p>
              <p className="box-title">
                <span className="box-span">其他 </span> {otherCount || 0}人
              </p>
            </div>
          </div>
        )}
        {type === 'community_unit' && (
          <div className="title">
            <span className="value">失败原因：</span>
            <div className="value-box">
              {communityUnitErrors.length &&
                communityUnitErrors.map((item, i) => {
                  return (
                    <p className="box-title" key={i}>
                      <span className="box-span">{`第${item.position}行因${item.msg}`}</span>
                    </p>
                  );
                })}
            </div>
          </div>
        )}
        {type === 'HOUSE' && (
          <div className="title">
            <span className="value">失败原因：</span>
            <div className="value-box">{info}</div>
          </div>
        )}
        <div style={{ textAlign: 'right' }}>
          <span style={{ cursor: 'pointer' }} className="ok" onClick={() => this.goLibPage(res)}>
            进入小区{name}信息界面
          </span>
        </div>
      </div>
    );
    return content;
  };

  getTopModuleKey = (moduleName) => {
    const cacheKey = cache.getUserCache('menu_cache_key', 'session') || {};
    if (cacheKey[moduleName]) {
      return cacheKey[moduleName];
    } else {
      //   const key = BaseStore.tab.createTabKey();
      const key = uuid();
      cacheKey[moduleName] = key;
      cache.setUserCache('menu_cache_key', cacheKey, 'session');
      return key;
    }
  };
  // 跳转页面
  goLibPage = (res) => {
    // let { history } = this.props;
    const { message = {} } = res;
    const { type, villageId } = message;
    // const { BaseStore } = window;
    let villageTitle = '';

    let pageType = 'communityPersonManagement';
    switch (type) {
      case 'PEOPLE':
        pageType = 'villageManagement/communityPersonManagement';
        villageTitle = '人员';
        break;
      case 'HOUSE':
        pageType = 'villageManagement/communityHourseManagement';
        villageTitle = '房屋';
        break;
      case 'VEHICLE':
        pageType = 'villageManagement/communityVehicleManagement';
        villageTitle = '车辆';
        break;
      case 'community_unit':
        pageType = 'villageManagement/communityUnitManagement';
        villageTitle = '单位';
        break;
      default:
        break;
    }

    console.log('pageType, villageId, type, this.props.history', pageType, villageId, type, this.props.history);

    notification.destroy();
    // let tabs = BaseStore.tab.findTabsByModuleName(pageType);
    // let item = tabs.find((v) => +queryFormat(v.location.search).id === +villageId);
    // if (item) {
    //   BaseStore.tab.closeTabForStoreId(item.id);
    // }
    // const tabKey = BaseStore.tab.createTabKey();

    const newLocation = {
      //   pathname: `/communityManagement/${this.getTopModuleKey('communityManagement')}/${pageType}/${tabKey}`,
      pathname: `/${pageType}`,
      search: `?dbId=${villageId}&&key=${uuid()}`,
      state: { pageState: { tabTitle: `${villageTitle}信息` } },
    };
    this.props.history && this.props.history.push(newLocation);

    // browserHistory.push(`/${pageType}?id=${villageId}`);
    // history.push(newLocation);
  };

  render() {
    const { data } = this.props;
    return this.importVillage(data);
  }
}

export default VehicleEntrySocket;
