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 { getJobList, updateJobInfo } 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.JobListItem>();
  const actionRef = useRef<ActionType>();
  const formRef = useRef<ProFormInstance>();

  const columns: ProColumns<API.JobListItem>[] = [
    {
      title: '职位名称',
      dataIndex: 'title',
      valueType: 'textarea',
    },
    {
      title: '职位描述',
      dataIndex: 'sourceText',
      valueType: 'textarea',
      colSize: 2,
      search: false,
      copyable: true,
      renderText(sourceText: string) {
        return sourceText?.substring(0, 30);
      },
    },
    {
      title: '职位ID',
      dataIndex: 'jobId',
      valueType: 'textarea',
      copyable: true,
    },
    {
      title: '城市',
      dataIndex: 'cityCode',
      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: '所在群名称',
      dataIndex: 'imGroupNick',
      valueType: 'textarea',
      copyable: true,
    },
    {
      title: '群ID',
      dataIndex: 'blGroupId',
      valueType: 'textarea',
      search: false,
      copyable: true,
    },
    {
      title: '发布人昵称',
      dataIndex: 'publisher',
      valueType: 'textarea',
      copyable: true,
    },
    {
      title: '发布人ID',
      dataIndex: 'blPublisherId',
      valueType: 'textarea',
      copyable: true,
    },
    {
      title: '发布人微信账号',
      dataIndex: 'publisherAcctNo',
      valueType: 'textarea',
      copyable: true,
    },
    {
      title: '发布群数量',
      dataIndex: 'relateGroupCount',
      valueType: 'textarea',
      search: false,
    },
    {
      title: '机器人ID',
      dataIndex: 'robotId',
      valueType: 'textarea',
    },
    {
      title: '机器人微信昵称',
      dataIndex: 'robotImNick',
      valueType: 'textarea',
      search: false,
    },
    {
      title: '机器人微信账号',
      dataIndex: 'robotImNo',
      valueType: 'textarea',
      search: false,
    },
    {
      title: '通告状态',
      dataIndex: 'disable',
      valueType: 'textarea',
      renderText(disable: boolean) {
        return disable ? '暂停' : '正常';
      },
      renderFormItem() {
        return <Select showSearch allowClear options={STATUS_OPTIONS} />;
      },
    },
    {
      title: '小程序用户 id',
      dataIndex: 'appUid',
      valueType: 'textarea',
      search: true,
    },
    {
      title: '创建时间',
      dataIndex: 'created',
      valueType: 'dateTime',
      renderText(created: string) {
        return dayjs(Number(created)).format(TIME_FORMAT);
      },
      search: false,
    },
    {
      title: '更新时间',
      dataIndex: 'updated',
      valueType: 'dateTime',
      renderText(created: string) {
        return dayjs(Number(created)).format(TIME_FORMAT);
      },
      search: false,
    },
    {
      title: '操作',
      valueType: 'option',
      fixed: 'right',
      width: 100,
      align: 'center',
      render: (_, record) => (
        <a
          key="config"
          onClick={() => {
            handleUpdateModalOpen(true);
            setCurrentRow(record);
          }}
        >
          修改
        </a>
      ),
    },
  ];
  return (
    <PageContainer>
      <ProTable<API.JobListItem, API.PageParams>
        headerTitle="查询表格"
        actionRef={actionRef}
        rowKey="key"
        search={{ labelWidth: 120, collapsed: false, collapseRender: false }}
        request={getJobList}
        columns={columns}
        scroll={{ x: 'max-content' }}
      />
      <ModalForm
        title="更新通告信息"
        width="400px"
        formRef={formRef}
        open={updateModalOpen}
        onOpenChange={handleUpdateModalOpen}
        onFinish={async formData => {
          const params: API.UpdateJobParams = {
            id: currentRow!.id,
            jobId: currentRow!.jobId,
            disable: Number(formData.disable) !== 0,
          };
          console.log('update confirm', formData, params);
          try {
            await updateJobInfo(params);
            actionRef.current?.reload();
            formRef.current?.resetFields();
          } catch (e) {}
          handleUpdateModalOpen(false);
        }}
      >
        <ProFormSelect name="disable" label="通告状态" options={STATUS_OPTIONS} />
      </ModalForm>
    </PageContainer>
  );
};
export default TableList;
