import { router } from 'umi'
import { Button, Badge } from 'antd'
import moment from 'moment'
import OperButton from '@/components/OperButton'
import AuthButton from '@/components/AuthButton'
import withActions from '@/components/ActionList'
import { getImgURL } from '@/utils/image'

export default ({page, onPublish, onDelete}) => [
  // {
  //   title: '编号',
  //   key: '#',
  //   align: 'center',
  //   render: (t, r, index) => (page.current - 1) * page.pageSize + index + 1,
  // },
  {
    title: '楼盘编号',
    key: 'code',
    dataIndex: 'code',
  },
  {
    title: '封面',
    dataIndex: 'buildingListImg',
    key: 'buildingListImg',
    align: 'center',
    render: (_, row) => {
      if (row.buildingListImg && row.buildingListImg[0]) {
        return <img src={getImgURL(row.buildingListImg[0].url)} width={128} height={72} style={{borderRadius: '4px'}} alt="" />
      }
      return null
    }
  },
  {
    title: '楼盘名称',
    dataIndex: 'buildingName',
    key: 'buildingName',
    align: 'center',
    render: (t, row) => (
      <AuthButton name="building.detail" noRight={t}>
        <Button
          type="link"
          onClick={() => router.push({
            pathname: '/building/add',
            query: {
              id: row.buildingId,
            },
          })}
        >{t}</Button>
      </AuthButton>
    )
  },
  {
    title: '销售状态',
    dataIndex: 'marketStatus',
    key: 'marketStatus',
    align: 'center',
  },
  {
    title: '录入时间',
    dataIndex: 'createDate',
    key: 'createDate',
    align: 'center',
    render: (t) => moment(t).format('YYYY-MM-DD HH:mm')
  },
  {
    title: '状态',
    dataIndex: 'status',
    key: 'status',
    align: 'center',
    render: (t) => t === 1 ? <Badge status="success" text="已发布" /> : <Badge status="processing" text="未发布" />
  },
  {
    title: '操作',
    key: 'options',
    align: 'center',
    render: withActions((_, row) => [
      <AuthButton name="building.publish" noRight={null}>
        <OperButton onClick={() => onPublish(row)}>{row.status === 1 ? '取消发布' : '发布'}</OperButton>
      </AuthButton>,
      <AuthButton name="building.delete" noRight={null}>
        <OperButton.Confirm
          title="确认删除?"
          content="删除之后不可恢复"
          onClick={() => onDelete(row)}
        >删除</OperButton.Confirm>
      </AuthButton>
    ], {noMargin: true})
  },
]