import type { ActionType, ProColumns, ProFormInstance } from '@ant-design/pro-components';
import { ModalForm, PageContainer, ProFormSelect, ProTable } from '@ant-design/pro-components';
import '@umijs/max';
import { Select } from 'antd';
import dayjs from 'dayjs';
import React, { useRef, useState } from 'react';

import { CITY_CODE_TO_NAME_MAP, CITY_OPTIONS } from '@/constants/city';
import { TIME_FORMAT } from '@/constants/global';
import { getAnchorList, updateAnchorInfo } from '@/services/list';

const STATUS_OPTIONS = [
  { label: '正常', value: 0 },
  { label: '封禁', value: 1 },
];

const TableList: React.FC = () => {
  const [updateModalOpen, handleUpdateModalOpen] = useState<boolean>(false);
  const [currentRow, setCurrentRow] = useState<API.AnchorListItem>();
  const actionRef = useRef<ActionType>();
  const formRef = useRef<ProFormInstance>();

  const columns: ProColumns<API.AnchorListItem>[] = [
    {
      title: '主播昵称',
      dataIndex: 'nickName',
      valueType: 'textarea',
    },
    {
      title: '主播ID',
      dataIndex: 'userId',
      valueType: 'textarea',
      copyable: true,
    },
    {
      title: '主播手机号',
      dataIndex: 'userPhone',
      valueType: 'textarea',
      copyable: true,
    },
    {
      title: '注册时间',
      dataIndex: 'created',
      valueType: 'dateTime',
      renderText(created: string) {
        return dayjs(Number(created)).format(TIME_FORMAT);
      },
      search: false,
      // search: {
      //   transform: (created: string) => dayjs(created).valueOf().toString(),
      // },
    },
    {
      title: '最后登录时间',
      dataIndex: 'lastLoginDate',
      valueType: 'textarea',
      search: false,
      renderText(created: string) {
        return dayjs(Number(created)).format(TIME_FORMAT);
      },
    },
    {
      title: '账号状态',
      dataIndex: 'status',
      valueType: 'textarea',
      renderText(status: number) {
        return STATUS_OPTIONS.find(option => option.value === status)?.label;
      },
      renderFormItem() {
        return <Select showSearch allowClear options={STATUS_OPTIONS} />;
      },
    },
    {
      title: '城市',
      dataIndex: 'city',
      valueType: 'textarea',
      renderText(cityCode: string) {
        return CITY_CODE_TO_NAME_MAP.get(cityCode);
      },
      renderFormItem() {
        return (
          <Select
            showSearch
            allowClear
            options={CITY_OPTIONS}
            filterOption={(input, option) => (option?.label ?? '').toLowerCase().includes(input.toLowerCase())}
          />
        );
      },
    },
    {
      title: '操作',
      valueType: 'option',
      render: (_, record) => (
        <a
          key="config"
          onClick={() => {
            handleUpdateModalOpen(true);
            setCurrentRow(record);
          }}
        >
          修改
        </a>
      ),
    },
  ];
  return (
    <PageContainer>
      <ProTable<API.AnchorListItem, API.PageParams>
        headerTitle="查询表格"
        actionRef={actionRef}
        rowKey="key"
        search={{ labelWidth: 120, collapsed: false, collapseRender: false }}
        request={getAnchorList}
        columns={columns}
      />
      <ModalForm
        title="更新主播信息"
        width="400px"
        formRef={formRef}
        open={updateModalOpen}
        onOpenChange={handleUpdateModalOpen}
        onFinish={async data => {
          const params: API.UpdateAnchorParams = {
            userId: currentRow!.userId,
            status: data.status,
          };
          console.log('update confirm', data, params);
          try {
            await updateAnchorInfo(params);
            actionRef.current?.reload();
            formRef.current?.resetFields();
          } catch (e) {}
          handleUpdateModalOpen(false);
        }}
      >
        <ProFormSelect
          name="status"
          label="账号状态"
          options={STATUS_OPTIONS}
          rules={[{ required: true, message: '必填项' }]}
        />
      </ModalForm>
    </PageContainer>
  );
};
export default TableList;
