import {DictionaryData} from '@models';
import {Button} from 'antd';
import {ColumnsType} from 'antd/es/table';
import {
  MATERIAL_CODE_COL_WIDTH,
  MATERIAL_NAME_COL_WIDTH,
  MATERIAL_TYPE_COL_WIDTH,
  MIDDLE_TABLE_WIDTH,
  NORMAL_TABLE_WIDTH,
  SMALL_TABLE_WIDTH,
} from '@utils';
import {useTableModalEvent} from '@hooks';

export type MaterialType = DictionaryData & {count: number};

const tableColumns: ColumnsType<DictionaryData> = [
  {
    title: '物料编号',
    dataIndex: 'code',
    key: 'code',
    width: MATERIAL_CODE_COL_WIDTH,
  },
  {
    title: '物料名称',
    dataIndex: 'name',
    key: 'name',
    width: MATERIAL_NAME_COL_WIDTH,
  },
  {
    title: '物料类别',
    dataIndex: 'partType',
    key: 'partType',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '物料类型',
    dataIndex: 'wllbClass',
    key: 'wllbClass',
    width: MATERIAL_TYPE_COL_WIDTH,
  },
  {
    title: '存储容量',
    dataIndex: 'size',
    key: 'size',
    width: SMALL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '最大存储容量',
    dataIndex: 'maxNum',
    key: 'maxNum',
    width: NORMAL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '最小存储容量',
    dataIndex: 'minNum',
    key: 'minNum',
    width: NORMAL_TABLE_WIDTH,
    align: 'right',
  },
  {
    title: '是否混合存储',
    dataIndex: 'isNotDisable',
    key: 'isNotDisable',
    render(_, {isNotDisable}) {
      return isNotDisable === '1' ? '是' : '否';
    },
    width: NORMAL_TABLE_WIDTH,
    align: 'center',
  },
  {
    title: '是否推荐库位',
    dataIndex: 'isRecommend',
    key: 'isRecommend',
    render(_, {isRecommend}) {
      return isRecommend === '1' ? '是' : '否';
    },
    width: NORMAL_TABLE_WIDTH,
    align: 'center',
  },
  {
    title: '修改人',
    dataIndex: 'modifyUser',
    key: 'modifyUser',
    width: NORMAL_TABLE_WIDTH,
  },
  {
    title: '修改时间',
    dataIndex: 'modifyTime',
    key: 'modifyTime',
    width: MIDDLE_TABLE_WIDTH,
  },
];

export function useHandle() {
  const [{visible, editId}, {onClose, onEdit}] = useTableModalEvent();

  const columns: ColumnsType<DictionaryData> = [
    ...tableColumns,
    {
      title: '操作',
      dataIndex: 'tldId',
      key: 'tldId',
      width: SMALL_TABLE_WIDTH,
      fixed: 'right',
      render(_, {tldId}) {
        return (
          <>
            <Button
              type='text'
              className='ant-text-btn-color-primary'
              onClick={onEdit(tldId)}
            >
              修改
            </Button>
          </>
        );
      },
    },
  ];

  return [{columns, visible, editId}, {onClose}] as const;
}
