import React, { useMemo } from 'react'
import styles from './index.module.scss'
import { authorityFu } from '@/utils/authority'
import { selectObj, getDictFu } from '@/utils/dataChange'
import history, { openLink } from '@/utils/history'
import TableList from '@/pages/Zother/TableList'
import { useSelector } from 'react-redux'
import { RootState } from '@/store'
import { API_getGoodsList } from '@/store/action/Cledger/C1ledger'
import { baseFormData } from '@/pages/Zother/data'
import { goodsSonTableC } from '@/utils/tableData'
import { Button, Dropdown } from 'antd'

const C1topSearch = [
  {
    type: 'input',
    key: 'searchKey',
    placeholder: '搜索藏品登记号、藏品名称'
  },
  {
    type: 'input',
    key: 'searchTagName',
    placeholder: `搜索藏品标签`
  },
  {
    type: 'select',
    key: 'level',
    placeholder: '级别',
    options: selectObj['藏品级别']
  },
  {
    type: 'cascader',
    key: 'typeDictId',
    placeholder: '类别',
    options: getDictFu('藏品类别')
  },
  {
    type: 'cascader',
    key: 'ageDictId',
    placeholder: '年代',
    options: getDictFu('藏品年代')
  },
  {
    type: 'cascader',
    key: 'textureDictId',
    placeholder: '质地',
    options: getDictFu('质地')
  },
  {
    type: 'select',
    key: 'tornLevel',
    placeholder: '完残程度',
    options: selectObj['完残程度']
  },
  // TODO
  {
    type: 'select',
    key: 'status',
    placeholder: '入藏状态',
    options: selectObj['藏品入藏状态']
  },
  {
    type: 'select',
    key: 'xxx',
    placeholder: '库存状态',
    options: selectObj['藏品库存状态']
  },
  {
    type: 'select',
    key: 'xxxx',
    placeholder: `搜索库房位置`
  }
]

const C1baseFormData = {
  ...baseFormData,
  typeDictId: '',
  ageDictId: '',
  textureDictId: ''
}

const columnsTempAdd = [
  ...goodsSonTableC(false),
  ['select', '入藏状态', 'status', selectObj['藏品入藏状态']],
  ['txt', '库存状态', 'xxx'],
  ['txt', '库房位置', 'xxx']
]

function C1ledger() {
  const tableInfo = useSelector((state: RootState) => state.C1ledger.tableInfo)

  const rightBtnOptions = useMemo(() => {
    return {
      dangan: [
        {
          key: '1',
          label: '登记',
          onClick: () =>
            authorityFu(410, '您没有藏品登记权限！', () => history.push(`/register_edit/1/null`))
        },
        {
          key: '2',
          label: '注销',
          onClick: () =>
            authorityFu(430, '您没有藏品注销权限！', () => history.push(`/writeOff_edit/1/null`))
        }
      ],
      kufang: [
        {
          key: '1',
          label: '入库',
          onClick: () =>
            authorityFu(1010, '您没有藏品入库权限！', () => history.push(`/inStorage_edit/1/null`))
        },
        {
          key: '2',
          label: '移库',
          onClick: () =>
            authorityFu(1020, '您没有藏品移库权限！', () =>
              history.push(`/moveStorage_edit/1/null`)
            )
        },
        {
          key: '3',
          label: '出库',
          onClick: () =>
            authorityFu(1030, '您没有藏品出库权限！', () => history.push(`/outStorage_edit/1/null`))
        }
      ],
      weihu: [
        {
          key: '1',
          label: '鉴定登记',
          onClick: () =>
            authorityFu(230, '您没有藏品鉴定登记权限！', () => history.push(`/auth_edit/1/null`))
        },
        {
          key: '2',
          label: '事故登记',
          onClick: () =>
            authorityFu(510, '您没有藏品事故登记权限！', () =>
              history.push(`/accident_edit/1/null`)
            )
        },
        {
          key: '3',
          label: '现状登记',
          onClick: () =>
            authorityFu(540, '您没有藏品现状登记权限！', () =>
              history.push(`/actuality_edit/1/null`)
            )
        },
        {
          key: '4',
          label: '修复登记',
          onClick: () =>
            authorityFu(550, '您没有藏品修复登记权限！', () => history.push(`/repair_edit/1/null`))
        }
      ]
    }
  }, [])

  const dataExport = () => {
    console.log('数据导出了')
  }

  // 故事管理/藏品总账定制右侧内容
  const storyTableListToprr = ({
    clickSearch,
    resetSelectFu
  }: {
    clickSearch: () => void
    resetSelectFu: () => void
  }) => {
    return (
      <>
        <Dropdown menu={{ items: rightBtnOptions['dangan'] }} trigger={['click']}>
          <Button type='primary'>档案管理</Button>
        </Dropdown>
        <Dropdown menu={{ items: rightBtnOptions['kufang'] }} trigger={['click']}>
          <Button type='primary'>库房管理</Button>
        </Dropdown>
        <Dropdown menu={{ items: rightBtnOptions['weihu'] }} trigger={['click']}>
          <Button type='primary'>藏品维护</Button>
        </Dropdown>
        <Button type='primary' onClick={dataExport}>
          数据导入
        </Button>
        <Button type='primary' onClick={dataExport}>
          数据导出
        </Button>
        <Button type='primary' onClick={clickSearch}>
          查询
        </Button>
        <Button onClick={resetSelectFu}>重置</Button>
      </>
    )
  }

  // 故事管理/藏品总账定制右侧操作按钮
  const storyTableLastBtn = useMemo(() => {
    return [
      {
        title: '操作',
        render: (item: any) => (
          <Button size='small' type='text' onClick={() => openLink(`/goodsLook/${item.id}`)}>
            查看
          </Button>
        )
      }
    ]
  }, [])

  return (
    <div className={styles.C1ledger}>
      <div className='pageTitle'>藏品总账</div>

      <TableList
        baseFormData={C1baseFormData}
        getListAPI={API_getGoodsList}
        pageKey='ledger'
        tableInfo={tableInfo}
        columnsTemp={columnsTempAdd}
        rightBtnWidth={350}
        leftRowWidth={'20%'}
        yHeight={592}
        searchDom={C1topSearch}
        storyTableListToprr={storyTableListToprr}
        storyTableLastBtn={storyTableLastBtn}
      />
    </div>
  )
}

const MemoC1ledger = React.memo(C1ledger)

export default MemoC1ledger
