import React from 'react';
import Taro from '@tarojs/taro';
import { View, Button, Image } from '@tarojs/components';
import { logout } from '@/actions/auth';
import { getLogoutUrl } from '@/services/api';
import cfg from '../../config';
import { getState } from '../../store/globalState';
import './index.scss';
import headImg from '../../static/img/my/head.png';
import moreImg from '../../static/img/my/more.svg';
import pswdImg from '../../static/img/my/pswd.svg';
import reportImg from '../../static/img/my/report.svg';

import Common from '../../components/echartForWx/common'; // 如果这行删掉微信小程序编译就一直死循环，待解决

const { webUrl } = cfg;

const Index = ({ ...props }) => {
  const userInfo = Taro.getStorageSync('userInfo') || {};
  const isSuperAdmin = userInfo && userInfo.username === 'SuperAdmin' ? true : false
  const isAdmin = userInfo && userInfo.isAdmin == true ? true : false

  const changePassword = () => {
    Taro.navigateTo({ url: '/packages/changePassword/index' })
  }

  const toMyReport = (kind) => {
    Taro.navigateTo({
      url: `/packages/patrolView/index?filter=my&kind=${kind}`
    })
  }

  const toPatrolReport = (kind) => {
    Taro.navigateTo({
      url: `/packages/patrol/index?type=edit&kind=${kind}`
    })
  }

  const onLogout = () => {
    Taro.showModal({
      title: '提示',
      content: '确定退出登录吗',
      success: function (res) {
        let token = getState('token') || Taro.getStorageSync('token')
        if (res.confirm) {
          logout(getLogoutUrl(), { token }).then(() => {
            Taro.reLaunch({
              url: '/pages/auth/login/login'
            });
          });
        }
      }
    })
  }

  function judgeRight(code) {
    return userInfo && userInfo.userResources && userInfo.userResources.some(item => item.resourceId === code)
  }

  return (
    <View className='page'>
      <View className='myBox'>
        <View className='my-top'>
          <Image className='my-portrait' src={headImg} />
          <View className='my-item'>
            <View className='my-username'>{userInfo.name}</View>
            <View className='my-phone'>{userInfo.phone}</View>
          </View>
        </View>
      </View>

      {
        judgeRight('WXPATROLREPORT') ?
          <View className='box' onClick={isSuperAdmin || isAdmin ? () => toPatrolReport('patrol') : () => toMyReport('patrol')}>
            <Image className='box-img' src={reportImg} />
            <View className='box-txt'>
              {isSuperAdmin || isAdmin ? '巡查上报' : '巡查上报'}
            </View>
            <Image className='img' src={moreImg} />
          </View> : ''
      }
      {
        judgeRight('WXMAINTENANCEREPORT') ?
          <View className='box' onClick={isSuperAdmin || isAdmin ? () => toPatrolReport('conserve') : () => toMyReport('conserve')}>
            <Image className='box-img' src={reportImg} />
            <View className='box-txt'>
              {isSuperAdmin || isAdmin ? '养护上报' : '养护上报'}
            </View>
            <Image className='img' src={moreImg} />
          </View>
          : ''
      }
      {
        judgeRight('WXBUILDINGROAD') ?
          <View className='box' onClick={isSuperAdmin || isAdmin ? () => toPatrolReport('road') : () => toMyReport('road')}>
            <Image className='box-img' src={reportImg} />
            <View className='box-txt'>
              {isSuperAdmin || isAdmin ? '在建项目上报' : '在建项目'}
            </View>
            <Image className='img' src={moreImg} />
          </View> : ''
      }
      {
        judgeRight('WXFEEDBACKMANAGE') ?
          <View className='box' onClick={isSuperAdmin || isAdmin ? () => toPatrolReport('anomaly') : () => toMyReport('anomaly')}>
            <Image className='box-img' src={reportImg} />
            <View className='box-txt'>
              {isSuperAdmin || isAdmin ? '异常反馈上报' : '异常反馈'}
            </View>
            <Image className='img' src={moreImg} />
          </View>
          : ''
      }
      {
        judgeRight('WXTODOANDONE') ?
          <View className='box' onClick={() => toMyReport('handle')}>
            <Image className='box-img' src={reportImg} />
            <View className='box-txt'>
              已办事项
            </View>
            <Image className='img' src={moreImg} />
          </View>
          : ''
      }
      <View className='box' onClick={changePassword} style={{ marginTop: '2rpx' }}>
        <Image className='box-img' src={pswdImg} />
        <View className='box-txt'>修改密码</View>
        <Image className='img' src={moreImg} />
      </View>

      <View className='logout' onClick={onLogout}>退出登录</View>

    </View>
  );
}
export default Index;
