import React from 'react'
import { View, Text, Image } from '@tarojs/components'
import Page from '@/layouts/index';
import { useModel } from '@/store';
import MenuIcon from '@/components/MenuIcon';
import { ROLE_CITIZEN, ROLE_INSPECTOR, ROLE_MANAGER, ROLE_ORG_MANAGER, ROLE_ORG_USER, ROLE_QUERY_PERSON } from '@/utils/user';
import { PROCESS_APPLY_DELAY, PROCESS_APPLY_END, PROCESS_APPLY_REJECT, PROCESS_APPLY_VERIFY, PROCESS_ASSIGNED, PROCESS_END, PROCESS_START } from '@/utils/biz';
import { getApplyNum } from '@/services/taissueapply';
import Head from './components/Head';
import Banner from './components/Banner';
import StatCard from './components/StatCard';
import './index.less';

const getRejctApply = () => getApplyNum({ applyType: PROCESS_APPLY_REJECT });
const getDelayApply = () => getApplyNum({ applyType: PROCESS_APPLY_DELAY });
const getVerifyApply = () => getApplyNum({ applyType: PROCESS_APPLY_VERIFY });
const getEdnApply = () => getApplyNum({ applyType: PROCESS_APPLY_END });

const menus = {
  // 督查员
  [ROLE_INSPECTOR]: [
    { icon: 'icon9', text: '我的上报', link: '/pages/issue/list/index?mine=true' },
    { icon: 'icon11', text: '消息通知', link: '/pages/message/list/index' },
    { icon: 'icon10', text: '模拟测评', link: '/pages/check/list/index' },
  ],

  // 平台管理员
  [ROLE_MANAGER]: [
    { icon: 'icon1', text: '待 交 办', link: `/pages/issue/list2/index?title=待交办&bizStatus=${PROCESS_START}` },
    { icon: 'icon2', text: '已 交 办', link: `/pages/issue/list2/index?title=已交办&bizStatus=${PROCESS_ASSIGNED}`, },
    { icon: 'icon3', text: '已 办 结', link: `/pages/issue/list2/index?title=已办结&bizStatus=${PROCESS_END}` },
    { icon: 'icon4', text: '消单申请', request: getEdnApply, link: `/pages/apply/list/index?title=消单申请&applyType=${PROCESS_APPLY_END}` },
    { icon: 'icon5', text: '逾期警告', link: `/pages/issue/list2/index?title=逾期警告&bizStatus=expired` },
    { icon: 'icon6', text: '延期申请', request: getDelayApply, link: `/pages/apply/list/index?title=延期申请&applyType=${PROCESS_APPLY_DELAY}` },
    { icon: 'icon12', text: '驳回申请', request: getRejctApply, link: `/pages/apply/list/index?title=驳回申请&applyType=${PROCESS_APPLY_REJECT}` },
    { icon: 'icon7', text: '统计查询', link: '/subpkg1/pages/statistics/index' },
    { icon: 'icon9', text: '我的上报', link: '/pages/issue/list2/index?title=我的上报&mine=true' },
    { icon: 'icon11', text: '消息通知', link: '/pages/message/list/index' },
  ],

  // 责任单位用户
  [ROLE_ORG_USER]: [
    { icon: 'icon2', text: '处 理 中', link: `/pages/org/issue/list/index?title=处理中&bizStatus=${PROCESS_ASSIGNED}` },
    { icon: 'icon3', text: '已 办 结', link: `/pages/org/issue/list/index?title=已办结&bizStatus=${PROCESS_END}` },
    { icon: 'icon5', text: '已 逾 期', link: '/pages/org/issue/list/index?title=已逾期&bizStatus=expired' },
    { icon: 'icon11', text: '消息通知', link: '/pages/message/list/index'},
    // { icon: 'icon10', text: '我的上报', link: '/pages/reportList/index?title=我的上报'},
  ],

  // 责任交办单位管理员
  [ROLE_ORG_MANAGER]: [
    { icon: 'icon2', text: '处 理 中', link: `/pages/org/issue/list/index?title=处理中&bizStatus=${PROCESS_ASSIGNED}` },
    { icon: 'icon3', text: '已 办 结', link: `/pages/org/issue/list/index?title=已办结&bizStatus=${PROCESS_END}` },
    { icon: 'icon5', text: '已 逾 期', link: '/pages/org/issue/list/index?title=已逾期&bizStatus=expired' },
    { icon: 'icon9', text: '审核申请', request: getVerifyApply, link: `/pages/apply/list/index?title=审核申请&applyType=${PROCESS_APPLY_VERIFY}` },
    { icon: 'icon11', text: '消息通知', link: '/pages/message/list/index' },
    { icon: 'person', text: '人员管理', link: '/pages/user/list/index' },
  ],

  // 查询人员
  [ROLE_QUERY_PERSON]: [
    { icon: 'icon1', text: '未 处 理', link: `/pages/issue/list2/index?title=未处理&bizStatus=${PROCESS_START}` },
    { icon: 'icon2', text: '处 理 中', link: `/pages/issue/list2/index?title=处理中&bizStatus=${PROCESS_ASSIGNED}` },
    { icon: 'icon3', text: '已 办 结', link: `/pages/issue/list2/index?title=已办结&bizStatus=${PROCESS_END}` },
    { icon: 'icon5', text: '逾期警告', link: `/pages/issue/list2/index?title=逾期警告&bizStatus=expired` },
    { icon: 'icon7', text: '统计查询', link: '/subpkg1/pages/statistics/index' },
  ],

  // 市民
  [ROLE_CITIZEN]: [
    { icon: 'icon1', text: '未处理', link: `/pages/feedback/issuelist/index?title=未处理&bizStatus=${PROCESS_START}` },
    { icon: 'icon2', text: '已处理', link: `/pages/feedback/issuelist/index?title=已处理&bizStatus=${PROCESS_ASSIGNED}` },
    { icon: 'icon12', text: '已打回', link: `/pages/feedback/issuelist/index?title=已打回&bizStatus=reject` },
  ],
}

export default (props) => {
  const userModel = useModel('user');
  const { user, duty, signOut, changePwd } = userModel || {};

  const menuArr = React.useMemo(() => {
    if (!duty) return [];

    return menus[duty];
  }, [duty])

  return (
    <Page tabBar="home" className="home-page">
      <Head
        userModel={userModel}
        onChangePwd={changePwd}
        onExit={signOut}
      />
      <Banner duty={duty} />
      <StatCard duty={duty} />

      <View className="menu-icons">
        {
          menuArr.map(x => (
            <MenuIcon key={x.text} {...x} />
          ))
        }
      </View>
    </Page>
  );
}
