import React from 'react';
import Taro from '@tarojs/taro';
import { View } from '@tarojs/components';
import { Cell } from '@antmjs/vantui';
import { getDtStr } from '@/utils/date';
import { ROLE_ORG_MANAGER, ROLE_ORG_USER } from '@/utils/user';
import { getTaIssueApplyById } from '@/services/taissueapply';
import style from './card.module.less';

export default (props) => {
  const { item, duty } = props;

  const onClick = () => {
    if (item.targetType == 'apply') {
      getTaIssueApplyById(item.targetId).then(res => naviTo(res.issueId));
    } else if (item.targetType == 'issue') {
      naviTo(item.targetId);
    }
  };

  const naviTo = (issueId) => {
    if (duty == ROLE_ORG_USER || duty == ROLE_ORG_MANAGER) {
      Taro.navigateTo({
        url: `/pages/org/issue/detail/index?id=${issueId}`
      })
    } else {
      Taro.navigateTo({
        url: `/pages/issue/edit/index?id=${issueId}`
      })
    }
  }
  
  return (
    <View className={style['msg-card-wrapper']}>
      <Cell
        isLink
        onClick={onClick}
        renderTitle={(
          <View style={{ display: 'flex', justifyContent: 'space-between' }}>
            <View>
              {item?.title}
            </View>
            <View>
              {getDtStr(item?.createDate)}
            </View>
          </View>
        )}
        renderLabel={(
          <View style={{ width: '100%' }}>
            <View>
              {item?.content}
            </View>
          </View>
        )}
      />
    </View>
  )
}
